VB.Net - 基本控制

对象是通过使用工具箱控件在Visual Basic窗体上创建的一种用户界面元素。 事实上,在Visual Basic中,表单本身是一个对象。 每个Visual Basic控件由三个重要元素组成:

  • Properties:描述对象的属性,

控制属性

所有Visual Basic对象可以通过设置其属性来移动,调整大小或自定义。 属性是由Visual Basic对象(例如Caption或Fore Color)持有的值或特性。

属性可以在设计时通过使用属性窗口或在运行时通过使用程序代码中的语句来设置。

Object. Property = Value

  • Object is the name of the object you're customizing.  Object是您要定制的对象的名称。

例如,

Form1.Caption = "Hello"

您可以使用属性窗口设置任何窗体属性。 大多数属性可以在应用程序执行期间设置或读取。 您可以参考Microsoft文档中与应用于它们的不同控件和限制相关的属性的完整列表。

控制方法

方法是作为类的成员创建的过程,它们使对象做某事。 方法用于访问或操纵对象或变量的特性。 您将在类中使用的主要有两类方法:

1、如果您使用的工具箱提供的控件之一,您可以调用其任何公共方法。 这种方法的要求取决于所使用的类。

2、如果没有现有方法可以执行所需的任务,则可以向类添加一个方法。

例如,MessageBox控件有一个名为Show的方法,它在下面的代码片段中调用:

Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("Hello, World") End Sub End Class

控制事件

事件是通知应用程序已发生重要事件的信号。 例如,当用户单击表单上的控件时,表单可以引发Click事件并调用处理事件的过程。 有与窗体相关联的各种类型的事件,如点击,双击,关闭,加载,调整大小等。

以下是表单Load事件处理程序子例程的默认结构。 您可以通过双击代码来查看此代码,这将为您提供与表单控件相关联的所有事件的完整列表:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'event handler code goes here End Sub

这里,Handles MyBase.Load表示Form1_Load()子例程处理Load事件。 类似的方式,你可以检查存根代码点击,双击。 如果你想初始化一些变量,如属性等,那么你将这样的代码保存在Form1_Load()子程序中。 这里,重要的一点是事件处理程序的名称,默认情况下是Form1_Load,但您可以根据您在应用程序编程中使用的命名约定更改此名称。

基本控制

VB.Net提供了各种各样的控件,帮助您创建丰富的用户界面。 所有这些控制的功能在相应的控制类中定义。 控制类在System.Windows.Forms命名空间中定义。

下表列出了一些常用的控件:

S.N.小部件和说明
1Forms 形式The container for all the controls that make up the user interface.用于构成用户界面的所有控件的容器。
2TextBox 文本框It represents a Windows text box control.它代表一个Windows文本框控件。
3Label 标签It represents a standard Windows label.它代表一个标准的Windows标签。
4Button 按钮It represents a Windows button control.它代表一个Windows按钮控件。
5ListBox  列表框It represents a Windows control to display a list of items.它代表一个显示项目列表的Windows控件。
6ComboBox 组合框It represents a Windows combo box control.它代表一个Windows组合框控件。
7RadioButton 单选按钮It enables the user to select a single option from a group of choices when paired with other RadioButton controls.它使用户能够在与其他RadioButton控件配对时从一组选项中选择一个选项。
8CheckBox 复选框It represents a Windows CheckBox.它代表一个Windows复选框。
9PictureBox 图片框It represents a Windows picture box control for displaying an image.它表示用于显示图像的Windows画面框控件。
10ProgressBar 进度条It represents a Windows progress bar control.它代表一个Windows进度条控件。
11ScrollBar 滚动条It Implements the basic functionality of a scroll bar control.它实现滚动条控件的基本功能。
12DateTimePicker 日期输入框It represents a Windows control that allows the user to select a date and a time and to display the date and time with a specified format.它代表一个Windows控件,允许用户选择日期和时间,并以指定的格式显示日期和时间。
13TreeView 树状图It displays a hierarchical collection of labeled items, each represented by a TreeNode.它显示标签项的分层集合,每个由树节点表示。
14ListView 列表显示It represents a Windows list view control, which displays a collection of items that can be displayed using one of four different views.它代表一个Windows列表视图控件,它显示可以使用四个不同视图之一显示的项目集合。