电竞比分网-中国电竞赛事及体育赛事平台

分享

vb.net入門——分組控件:Panel控件的使用

 nxhujiee 2010-04-20

vb.net入門——分組控件:Panel控件的使用

【字體大小: 2008-02-18 19:56 來源: 作者:  

我們對控件進行分組的原因不外乎三個:

1、為了獲得清晰的用戶界面而將相關(guān)的窗體元素進行可視化分組。

2、編程分組,如對單選按鈕進行分組。

3、為了在設(shè)計時將多個控件作為一個單元來移動。

在vb.net中,有GroupBox、Panel、TabControl這三個控件可以實現(xiàn)上面所提到的三個分組目的,所以我們稱它們?yōu)榉纸M控件。

前面我們了解了GroupBox(控件組)控件(vb.net入門——分組控件:GroupBox控件的使用)的使用,這里我們將來看看下怎么使用Panel(也稱面板)控件。實際上,Panel很類似于GroupBox,其區(qū)別是:只有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控件內(nèi)部文字的大小與文字的顏色,需要注意的時候,這里改變的是其內(nèi)部控件的顯示的Text屬性的文字外觀。

5、AutoScroll屬性:該屬性指示當控件超出Panel顯示的區(qū)域時,是否自動出現(xiàn)滾動條,默認為False。

二、創(chuàng)建一組控件

1、在窗體上放置Panel控件。從工具箱中拖放一個Panel控件到窗體上的合適位置,調(diào)整大小。

2、因為Panel控件沒有Text屬性來標記自己,所以我們一般可以在它的上面添加一個Label控件來標記它。

3、在Panel控件內(nèi)拖放其它需要的控件,例如RadioButton控件。

4、設(shè)置Panel控件的外觀屬性。

4、設(shè)置示例

在窗體上設(shè)置兩個Panel控件,分別用2個Label控件來標記它們,每個Panel控件中放置所需的RadioButton控件。如圖一所示:

vb.net入門——分組控件:Panel控件的使用(圖二)

注意:兩個Panel控件的AutoScroll屬性都設(shè)置為True了。

5、我們在拖動單個Panel控件的時候,它內(nèi)部的控件也會隨著移動,以保持和Panel的相對位置不變。同理,刪除Panel控件時,它所包含的所有控件也會被刪除掉。

6、當我們調(diào)整Panel控件所包含的控件的Anchor和Dock屬性的時候,其參照物將不是Form窗體,而是Panel控件了。

7、當AutoScroll 屬性為 True 的時候,在設(shè)計界面中我們也可以拉動出現(xiàn)的滾動條。

 

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

動態(tài)添加控件一般需要經(jīng)過下面三個步驟:

1、創(chuàng)建要添加的控件實例

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

把上一頁的代碼復制添加后,把控件初始化過程InitializeControl()過程添加到Form1的New構(gòu)造函數(shù)中,如下圖二所示:

vb.net入門——分組控件:Panel控件的使用(圖三)

現(xiàn)在按F5運行,F(xiàn)orm1的窗體控件布局(如下圖三所示)是不是和我們手工布局的圖一的布局是一樣的呢?

vb.net入門——分組控件:Panel控件的使用(圖二)

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多