我們對(duì)控件進(jìn)行分組的原因不外乎三個(gè):
1、為了獲得清晰的用戶界面而將相關(guān)的窗體元素進(jìn)行可視化分組。
2、編程分組,如對(duì)單選按鈕進(jìn)行分組。
3、為了在設(shè)計(jì)時(shí)將多個(gè)控件作為一個(gè)單元來(lái)移動(dòng)。
在vb.net中,有GroupBox、Panel、TabControl這三個(gè)控件可以實(shí)現(xiàn)上面所提到的三個(gè)分組目的,所以我們稱它們?yōu)榉纸M控件。
前面我們了解了GroupBox(控件組)控件(vb.net入門——分組控件:GroupBox控件的使用)的使用,這里我們將來(lái)看看下怎么使用Panel(也稱面板)控件。實(shí)際上,Panel很類似于GroupBox,其區(qū)別是:只有GroupBox控件可以顯示標(biāo)題,而只有Panel控件可以有滾動(dòng)條。
Panel控件在工具箱中的圖標(biāo)如圖所示:。
一、Panel控件的常用屬性
1、Anchor和Dock:這兩個(gè)屬性是所有有用戶界面的控件都有的定位屬性。
2、Name屬性:標(biāo)識(shí)控件的對(duì)象名稱
3、BorderStyle屬性:指示Panel控件的邊框樣式,共有三個(gè)枚舉值:
BorderStyle.None(默認(rèn))—無(wú)邊框。
BorderStyle.Fixed3D—三維邊框
BorderStyle.FixedSingle—單行邊框
此外還可以通過BackColor、BackgroundImage屬性來(lái)改變Panel控件的外觀。
4、Font和ForeColor屬性,用于改變Panel控件內(nèi)部文字的大小與文字的顏色,需要注意的時(shí)候,這里改變的是其內(nèi)部控件的顯示的Text屬性的文字外觀。
5、AutoScroll屬性:該屬性指示當(dāng)控件超出Panel顯示的區(qū)域時(shí),是否自動(dòng)出現(xiàn)滾動(dòng)條,默認(rèn)為False。
二、創(chuàng)建一組控件
1、在窗體上放置Panel控件。從工具箱中拖放一個(gè)Panel控件到窗體上的合適位置,調(diào)整大小。
2、因?yàn)镻anel控件沒有Text屬性來(lái)標(biāo)記自己,所以我們一般可以在它的上面添加一個(gè)Label控件來(lái)標(biāo)記它。
3、在Panel控件內(nèi)拖放其它需要的控件,例如RadioButton控件。
4、設(shè)置Panel控件的外觀屬性。
4、設(shè)置示例
在窗體上設(shè)置兩個(gè)Panel控件,分別用2個(gè)Label控件來(lái)標(biāo)記它們,每個(gè)Panel控件中放置所需的RadioButton控件。如圖一所示:
注意:兩個(gè)Panel控件的AutoScroll屬性都設(shè)置為True了。
5、我們?cè)谕蟿?dòng)單個(gè)Panel控件的時(shí)候,它內(nèi)部的控件也會(huì)隨著移動(dòng),以保持和Panel的相對(duì)位置不變。同理,刪除Panel控件時(shí),它所包含的所有控件也會(huì)被刪除掉。
6、當(dāng)我們調(diào)整Panel控件所包含的控件的Anchor和Dock屬性的時(shí)候,其參照物將不是Form窗體,而是Panel控件了。
7、當(dāng)AutoScroll 屬性為 True 的時(shí)候,在設(shè)計(jì)界面中我們也可以拉動(dòng)出現(xiàn)的滾動(dòng)條。
三、編程添加Panel控件以及它所包含的控件
動(dòng)態(tài)添加控件一般需要經(jīng)過下面三個(gè)步驟:
1、創(chuàng)建要添加的控件實(shí)例
2、設(shè)置新控件的屬性。
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
把上一頁(yè)的代碼復(fù)制添加后,把控件初始化過程InitializeControl()過程添加到Form1的New構(gòu)造函數(shù)中,如下圖二所示:現(xiàn)在按F5運(yùn)行,F(xiàn)orm1的窗體控件布局(如下圖三所示)是不是和我們手工布局的圖一的布局是一樣的呢?
聯(lián)系客服