vb.net入门——分组控件:GroupBox控件的使用


我们对控件进行分组的原因不外乎三个:

1、为了获得清晰的用户界面而将相关的窗体元素进行可视化分组。

2、编程分组,如对单选按钮进行分组。

3、为了在设计时将多个控件作为一个单元来移动。

在vb.net中,有GroupBox、Panel、TabControl这三个控件可以实现上面所提到的三个分组目的,所以我们称它们为分组控件。

这三个控件在功用上十分的相似,特别是GroupBox和Panel控件,只存在一点细微的差别而已(这个差别是:只有GroupBox控件可以显示标题,而只有Panel控件可以有滚动条)。这里我们就先来了解GroupBox控件的使用。

GroupBox(控件组)控件一般是作为其他控件的组的容器的形式存在的,这样有利于用户识别,使界面变得更加友好(GroupBox控件相当于Visual Basic以前版本的Frame控件)。使用控件组控件可以将一个窗体中的各种功能进一步进行分类,例如,将各种选项按钮控件分隔开。

当移动单个GroupBox控件时,它所包含的所有控件也将一起移动。

在大多数情况下,对控件组控件没有实际的操作。我们用它对控件进行分组,通常没有必要响应它的事件。不过,它的Name、Text和Font等属性可能会经常被修改,以适应应用程序在不同阶段的要求。

GroupBox控件在工具箱中的图标如图所示:。

一、GroupBox控件的常用属性

1、Anchor和Dock:这两个属性是所有有用户界面的控件都有的定位属性,这里就不啰嗦了。

2、Name属性:标识控件的对象名称。

3、Text属性:显示在GroupBox控件右上方的标题文字,可以用来标识该控件组的描述。

4、Font和ForeColor属性,用于改变GroupBox控件的文字大小以及文字的颜色,需要注意的时候,它不单改变GroupBox控件的Text属性的文字外观,同时也改变其内部控件的显示的Text属性的文字外观。

二、创建一组控件

1、在窗体上放置GroupBox控件。从工具箱中拖放一个GroupBox控件到窗体上的合适位置,调整大小。

2、在属性窗口中改变GroupBox控件的Text属性,作为它的标题。

3、在GroupBox控件内拖放其它需要的控件,例如RadioButton控件。

4、设置示例,如图一所示:

5、我们在拖动单个GroupBox控件的时候,它内部的控件也会随着移动,以保持和GroupBox的相对位置不变。同理,删除GroupBox控件时,它所包含的所有控件也会被删除掉。

6、当我们调整GroupBox控件所包含的控件的Anchor和Dock属性的时候,其参照物将不是Form窗体,而是GroupBox控件了。

三、编程添加GroupBox控件以及它所包含的控件

虽然GroupBox控件是在设计时用视图设计布局效果最好,但是无可避免地,很多特殊情况下也是需要在运行做添加控件到控件组中的,这里我们就用代码来完成上图一界面的绘制。

动态添加控件一般需要经过下面三个步骤:

1、创建要添加的控件实例

2、设置新控件的属性。

3、将控件添加到父控件的 Controls 集合。

在Form1代码的任意位置增加初始化控件的过程InitializeControl(),代码如下所示:

Sub InitializeControl()

'首先添加Label和TextBox控件

Dim Label1 As New System.Windows.Forms.Label

Dim TextBox1 As New System.Windows.Forms.TextBox

'Label1

Label1.Location = New System.Drawing.Point(8, 8)

Label1.Name = "Label1"

Label1.Size = New System.Drawing.Size(64, 16)

Label1.TabIndex = 1

Label1.Text = "户主姓名"

'TextBox1

TextBox1.Location = New System.Drawing.Point(72, 7)

TextBox1.Name = "TextBox1"

TextBox1.Size = New System.Drawing.Size(128, 21)

TextBox1.TabIndex = 2

TextBox1.Text = ""

'把它们添加到父控件Form1的Controls集合中

Me.Controls.Add(TextBox1)

Me.Controls.Add(Label1)

'添加三个GroupBox控件

Dim GroupBox1 As New System.Windows.Forms.GroupBox

Dim GroupBox2 As New System.Windows.Forms.GroupBox

Dim GroupBox3 As New System.Windows.Forms.GroupBox

'GroupBox1

GroupBox1.BackColor = System.Drawing.SystemColors.Control

GroupBox1.Location = New System.Drawing.Point(8, 40)

GroupBox1.Name = "GroupBox1"

GroupBox1.Size = New System.Drawing.Size(96, 104)

GroupBox1.TabIndex = 3

GroupBox1.TabStop = False

GroupBox1.Text = "性别"

'GroupBox2

GroupBox2.Location = New System.Drawing.Point(103, 40)

GroupBox2.Name = "GroupBox2"

GroupBox2.Size = New System.Drawing.Size(96, 104)

GroupBox2.TabIndex = 4

GroupBox2.TabStop = False

GroupBox2.Text = "单元"

'GroupBox3

GroupBox3.Location = New System.Drawing.Point(197, 40)

GroupBox3.Name = "GroupBox3"

GroupBox3.Size = New System.Drawing.Size(96, 104)

GroupBox3.TabIndex = 5

GroupBox3.TabStop = False

GroupBox3.Text = "楼层"

'把它们添加到父控件Form1的Controls集合中

Me.Controls.Add(GroupBox1)

Me.Controls.Add(GroupBox2)

Me.Controls.Add(GroupBox3)

'添加RadioButton控件并分别绘制在GroupBox控件内

Dim RadioButton1 As New System.Windows.Forms.RadioButton

Dim RadioButton2 As New System.Windows.Forms.RadioButton

Dim RadioButton3 As New System.Windows.Forms.RadioButton

Dim RadioButton5 As New System.Windows.Forms.RadioButton

Dim RadioButton4 As New System.Windows.Forms.RadioButton

Dim RadioButton6 As New System.Windows.Forms.RadioButton

Dim RadioButton7 As New System.Windows.Forms.RadioButton

Dim RadioButton8 As New System.Windows.Forms.RadioButton

Dim RadioButton9 As New System.Windows.Forms.RadioButton

Dim RadioButton10 As New System.Windows.Forms.RadioButton

'RadioButton1

RadioButton1.Location = New System.Drawing.Point(8, 24)

RadioButton1.Name = "RadioButton1"

RadioButton1.Size = New System.Drawing.Size(80, 24)

RadioButton1.TabIndex = 0

RadioButton1.Text = "男性"

'RadioButton2

RadioButton2.Location = New System.Drawing.Point(8, 63)

RadioButton2.Name = "RadioButton2"

RadioButton2.Size = New System.Drawing.Size(80, 24)

RadioButton2.TabIndex = 1

RadioButton2.Text = "女性"

'RadioButton3

RadioButton3.Location = New System.Drawing.Point(8, 32)

RadioButton3.Name = "RadioButton3"

RadioButton3.Size = New System.Drawing.Size(80, 24)

RadioButton3.TabIndex = 1

RadioButton3.Text = "二单元"

'RadioButton5

RadioButton5.Location = New System.Drawing.Point(8, 53)

RadioButton5.Name = "RadioButton5"

RadioButton5.Size = New System.Drawing.Size(80, 24)

RadioButton5.TabIndex = 2

RadioButton5.Text = "三单元"

'RadioButton4

RadioButton4.Location = New System.Drawing.Point(8, 13)

RadioButton4.Name = "RadioButton4"

RadioButton4.Size = New System.Drawing.Size(80, 24)

RadioButton4.TabIndex = 0

RadioButton4.Text = "一单元"

'RadioButton6

RadioButton6.BackColor = System.Drawing.SystemColors.Control

RadioButton6.Location = New System.Drawing.Point(8, 75)

RadioButton6.Name = "RadioButton6"

RadioButton6.Size = New System.Drawing.Size(80, 24)

RadioButton6.TabIndex = 3

RadioButton6.Text = "四单元"

'RadioButton7

RadioButton7.Location = New System.Drawing.Point(8, 32)

RadioButton7.Name = "RadioButton7"

RadioButton7.Size = New System.Drawing.Size(80, 24)

RadioButton7.TabIndex = 1

RadioButton7.Text = "二楼"

'RadioButton8

RadioButton8.Location = New System.Drawing.Point(8, 53)

RadioButton8.Name = "RadioButton8"

RadioButton8.Size = New System.Drawing.Size(80, 24)

RadioButton8.TabIndex = 2

RadioButton8.Text = "三楼"

'RadioButton9

RadioButton9.Location = New System.Drawing.Point(8, 13)

RadioButton9.Name = "RadioButton9"

RadioButton9.Size = New System.Drawing.Size(80, 24)

RadioButton9.TabIndex = 0

RadioButton9.Text = "一楼"

'RadioButton10

RadioButton10.BackColor = System.Drawing.SystemColors.Control

RadioButton10.Location = New System.Drawing.Point(8, 75)

RadioButton10.Name = "RadioButton10"

RadioButton10.Size = New System.Drawing.Size(80, 24)

RadioButton10.TabIndex = 3

RadioButton10.Text = "四楼"

'分别把它们添加到父控件GroupBox的Controls集合中

GroupBox1.Controls.Add(RadioButton1)

GroupBox1.Controls.Add(RadioButton2)

GroupBox2.Controls.Add(RadioButton3)

GroupBox2.Controls.Add(RadioButton4)

GroupBox2.Controls.Add(RadioButton5)

GroupBox2.Controls.Add(RadioButton6)

GroupBox3.Controls.Add(RadioButton7)

GroupBox3.Controls.Add(RadioButton8)

GroupBox3.Controls.Add(RadioButton9)

GroupBox3.Controls.Add(RadioButton10)

End Sub

把上一页的代码复制添加后,把控件初始化过程InitializeControl()过程添加到Form1的New构造函数中,如下图二所示:

图二:在New构造函数中添加过程InitializeControl()

现在按F5运行,Form1的窗体控件布局(如下图三所示)是不是和我们手工布局的图一的布局是一样的呢?

 

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3