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


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

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

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

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

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

前面我们了解了GroupBox(控件组)控件(vb.net入门——分组控件:GroupBox控件的使用)的使用,这里我们将来看看下怎么使用Panel(也称面板)控件。实际上,Panel很类似于GroupBox,其区别是:只有GroupBox控件可以显示标题,而只有Panel控件可以有滚动条。

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

一、Panel控件的常用属性

1、Anchor和Dock:这两个属性是所有有用户界面的控件都有的定位属性。

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

3、BorderStyle属性:指示Panel控件的边框样式,共有三个枚举值:

BorderStyle.None(默认)—无边框。

BorderStyle.Fixed3D—三维边框

BorderStyle.FixedSingle—单行边框

此外还可以通过BackColor、BackgroundImage属性来改变Panel控件的外观。

4、Font和ForeColor属性,用于改变Panel控件内部文字的大小与文字的颜色,需要注意的时候,这里改变的是其内部控件的显示的Text属性的文字外观。

5、AutoScroll属性:该属性指示当控件超出Panel显示的区域时,是否自动出现滚动条,默认为False。

二、创建一组控件

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

2、因为Panel控件没有Text属性来标记自己,所以我们一般可以在它的上面添加一个Label控件来标记它。

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

4、设置Panel控件的外观属性。

4、设置示例

在窗体上设置两个Panel控件,分别用2个Label控件来标记它们,每个Panel控件中放置所需的RadioButton控件。如图一所示:

注意:两个Panel控件的AutoScroll属性都设置为True了。

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

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

7、当AutoScroll 属性为 True 的时候,在设计界面中我们也可以拉动出现的滚动条。

 

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

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

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

2、设置新控件的属性。

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

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

Sub InitializeControl()

Dim Panel1 As New System.Windows.Forms.Panel

Dim Label1 As New System.Windows.Forms.Label

Dim Label2 As New System.Windows.Forms.Label

Dim Panel2 As New System.Windows.Forms.Panel

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 RadioButton4 As New System.Windows.Forms.RadioButton

Dim RadioButton5 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

'Panel1

Panel1.AutoScroll = True

Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D

Panel1.Controls.Add(RadioButton4)

Panel1.Controls.Add(RadioButton3)

Panel1.Controls.Add(RadioButton2)

Panel1.Controls.Add(RadioButton1)

Panel1.Location = New System.Drawing.Point(16, 32)

Panel1.Name = "Panel1"

Panel1.Size = New System.Drawing.Size(112, 104)

Panel1.TabIndex = 0

'Label1

Label1.BackColor = System.Drawing.Color.White

Label1.Font = New System.Drawing.Font("宋体", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))

Label1.ForeColor = System.Drawing.Color.Red

Label1.Location = New System.Drawing.Point(16, 15)

Label1.Name = "Label1"

Label1.Size = New System.Drawing.Size(112, 17)

Label1.TabIndex = 1

Label1.Text = "一单元"

Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'Label2

Label2.BackColor = System.Drawing.Color.White

Label2.Font = New System.Drawing.Font("宋体", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))

Label2.ForeColor = System.Drawing.Color.Red

Label2.Location = New System.Drawing.Point(136, 14)

Label2.Name = "Label2"

Label2.Size = New System.Drawing.Size(112, 18)

Label2.TabIndex = 2

Label2.Text = "二单元"

Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

'Panel2

Panel2.AutoScroll = True

Panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D

Panel2.Controls.Add(RadioButton5)

Panel2.Controls.Add(RadioButton6)

Panel2.Controls.Add(RadioButton7)

Panel2.Controls.Add(RadioButton8)

Panel2.Location = New System.Drawing.Point(136, 32)

Panel2.Name = "Panel2"

Panel2.Size = New System.Drawing.Size(112, 104)

Panel2.TabIndex = 3

'RadioButton1

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

RadioButton1.Name = "RadioButton1"

RadioButton1.Size = New System.Drawing.Size(48, 16)

RadioButton1.TabIndex = 0

RadioButton1.Text = "101"

'RadioButton2

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

RadioButton2.Name = "RadioButton2"

RadioButton2.Size = New System.Drawing.Size(48, 16)

RadioButton2.TabIndex = 1

RadioButton2.Text = "102"

'RadioButton3

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

RadioButton3.Name = "RadioButton3"

RadioButton3.Size = New System.Drawing.Size(48, 16)

RadioButton3.TabIndex = 2

RadioButton3.Text = "103"

'RadioButton4

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

RadioButton4.Name = "RadioButton4"

RadioButton4.Size = New System.Drawing.Size(48, 16)

RadioButton4.TabIndex = 3

RadioButton4.Text = "104"

'RadioButton5

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

RadioButton5.Name = "RadioButton5"

RadioButton5.Size = New System.Drawing.Size(48, 16)

RadioButton5.TabIndex = 7

RadioButton5.Text = "404"

'RadioButton6

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

RadioButton6.Name = "RadioButton6"

RadioButton6.Size = New System.Drawing.Size(48, 16)

RadioButton6.TabIndex = 6

RadioButton6.Text = "303"

'RadioButton7

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

RadioButton7.Name = "RadioButton7"

RadioButton7.Size = New System.Drawing.Size(48, 16)

RadioButton7.TabIndex = 5

RadioButton7.Text = "202"

'RadioButton8

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

RadioButton8.Name = "RadioButton8"

RadioButton8.Size = New System.Drawing.Size(48, 16)

RadioButton8.TabIndex = 4

RadioButton8.Text = "201"

'Form1

AutoScaleBaseSize = New System.Drawing.Size(6, 14)

ClientSize = New System.Drawing.Size(280, 165)

Controls.Add(Panel2)

Controls.Add(Panel1)

Controls.Add(Label2)

Controls.Add(Label1)

End Sub

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

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

 

本文作者:
« 
» 
快速导航

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