我們對(duì)控件進(jìn)行分組的原因不外乎三個(gè)
創(chuàng)新互聯(lián)是一家專(zhuān)注于成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)與策劃設(shè)計(jì),內(nèi)鄉(xiāng)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:內(nèi)鄉(xiāng)等地區(qū)。內(nèi)鄉(xiāng)做網(wǎng)站價(jià)格咨詢:13518219792
為了獲得清晰的用戶界面而將相關(guān)的窗體元素進(jìn)行可視化分搭鄭組
編程分組 如對(duì)單選按鈕進(jìn)行分組
為了在設(shè)計(jì)時(shí)將多個(gè)控件作為一個(gè)單元來(lái)移動(dòng)
在中 有GroupBox Panel TabControl這三個(gè)控件可以實(shí)現(xiàn)上面所提到的三個(gè)分組目的 所以我們稱它們?yōu)榉纸M控件
這三個(gè)控件在功用上十分的相似 特別是GroupBox和Panel控件 只存在一點(diǎn)細(xì)微的差別而已(這個(gè)差別是 只有GroupBox控件可以顯示標(biāo)題 而只有Panel控件可以有滾動(dòng)條) 這里我們就先來(lái)了解GroupBox控件的使用
GroupBox(控件組)控件一般是作為其他控件的組的容器的形式存在的 這樣有利于用戶識(shí)別 使界面變得更加友好(GroupBox控件相當(dāng)于Visual Basic以前版本的Frame控件) 使用控件組控件可以將一個(gè)窗體中的各種功能進(jìn)一步進(jìn)行分類(lèi) 例如 將各種選項(xiàng)按鈕控件分隔開(kāi)
當(dāng)移動(dòng)單個(gè)GroupBox控件時(shí) 它所包含的所有控件也將一起移動(dòng)
在大多數(shù)情況下 對(duì)控件組控件沒(méi)有實(shí)際的操作 我們用它對(duì)控件進(jìn)行分組 通常沒(méi)有必要響應(yīng)它的事件 不過(guò) 它的Name Text和Font等屬性可能會(huì)經(jīng)常被修改 以適應(yīng)應(yīng)用程序在不同階段的要求
GroupBox控件在工具箱中的圖標(biāo)如圖所示
一 GroupBox控件的常用屬性
Anchor和Dock 這兩個(gè)屬性是所有有用戶界面的控件都有的定位屬性 這里就不啰嗦了
Name屬性 標(biāo)識(shí)控件的對(duì)象名稱
Text屬性 顯示在GroupBox控件右上方的標(biāo)題文字 可以用來(lái)標(biāo)識(shí)該控件組的描述
Font和ForeColor屬性 用于改變GroupBox控件的文字大小以及文字的顏色 需要注意的時(shí)候 它不單改變GroupBox控件的Text屬性的文字外觀 同時(shí)也改變其內(nèi)部控件的顯示的Text屬性的文字外觀
二 創(chuàng)建一組控件
在窗體上放置GroupBox控件 從工具箱中拖放一個(gè)GroupBox控件到窗體上的合適位置 調(diào)整大小滾神
在屬性窗口中改變GroupBox控件的Text屬性 作為它的標(biāo)題
在GroupBox控件內(nèi)拖放其它需要的控件 例如RadioButton控件
設(shè)置示例 如圖一所示
圖一 用控件組控件對(duì)單選按鈕分組
我們?cè)谕蟿?dòng)單個(gè)GroupBox控件的時(shí)候 它內(nèi)部的控件也會(huì)隨著移動(dòng) 以保持和GroupBox的相對(duì)位置不變 同理 刪除GroupBox控件時(shí) 它所包含的所有控件也會(huì)被刪除掉
當(dāng)我們調(diào)整GroupBox控件所包含的控件的Anchor和Dock屬性的時(shí)候 其參照物將不是Form窗體 而是GroupBox控件了
三 編程添加GroupBox控件以及它所包含的控件
雖然GroupBox控件是在設(shè)計(jì)時(shí)用視圖設(shè)計(jì)布局效果最好 但是無(wú)可避免地 很多特殊情況下也是需要在運(yùn)行做添加控件到控件組中的 這里我們就用代碼來(lái)完成上圖一界面的繪制
動(dòng)態(tài)添加控件一般需要經(jīng)過(guò)下面三個(gè)步驟
創(chuàng)知備頌建要添加的控件實(shí)例
設(shè)置新控件的屬性
將控件添加到父控件的 Controls 集合
在Form 代碼的任意位置增加初始化控件的過(guò)程InitializeControl() 代碼如下所示
Sub InitializeControl()
首先添加Label和TextBox控件
Dim Label As New System Windows Forms Label
Dim TextBox As New System Windows Forms TextBox
Label
Label Location = New System Drawing Point( )
Label Name = Label
Label Size = New System Drawing Size( )
Label TabIndex =
Label Text = 戶主姓名
TextBox
TextBox Location = New System Drawing Point( )
TextBox Name = TextBox
TextBox Size = New System Drawing Size( )
TextBox TabIndex =
TextBox Text =
把它們添加到父控件Form 的Controls集合中
Me Controls Add(TextBox )
Me Controls Add(Label )
添加三個(gè)GroupBox控件
Dim GroupBox As New System Windows Forms GroupBox
Dim GroupBox As New System Windows Forms GroupBox
Dim GroupBox As New System Windows Forms GroupBox
GroupBox
GroupBox BackColor = System Drawing SystemColors Control
GroupBox Location = New System Drawing Point( )
GroupBox Name = GroupBox
GroupBox Size = New System Drawing Size( )
GroupBox TabIndex =
GroupBox TabStop = False
GroupBox Text = 性別
GroupBox
GroupBox Location = New System Drawing Point( )
GroupBox Name = GroupBox
GroupBox Size = New System Drawing Size( )
GroupBox TabIndex =
GroupBox TabStop = False
GroupBox Text = 單元
GroupBox
GroupBox Location = New System Drawing Point( )
GroupBox Name = GroupBox
GroupBox Size = New System Drawing Size( )
GroupBox TabIndex =
GroupBox TabStop = False
GroupBox Text = 樓層
把它們添加到父控件Form 的Controls集合中
Me Controls Add(GroupBox )
Me Controls Add(GroupBox )
Me Controls Add(GroupBox )
添加RadioButton控件并分別繪制在GroupBox控件內(nèi)
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
Dim RadioButton As New System Windows Forms RadioButton
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 男性
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 女性
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 二單元
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 三單元
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 一單元
RadioButton
RadioButton BackColor = System Drawing SystemColors Control
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 四單元
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 二樓
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 三樓
RadioButton
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 一樓
RadioButton
RadioButton BackColor = System Drawing SystemColors Control
RadioButton Location = New System Drawing Point( )
RadioButton Name = RadioButton
RadioButton Size = New System Drawing Size( )
RadioButton TabIndex =
RadioButton Text = 四樓
分別把它們添加到父控件GroupBox的Controls集合中
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
GroupBox Controls Add(RadioButton )
End Sub
把上一頁(yè)的代碼復(fù)制添加后 把控件初始化過(guò)程InitializeControl()過(guò)程添加到Form 的New構(gòu)造函數(shù)中 如下圖二所示
圖二 在New構(gòu)造函數(shù)中添加過(guò)程InitializeControl()
現(xiàn)在按F 運(yùn)行 Form 的窗體控件布局(如下圖三所示)是不是和我們手工布局的圖一的布局是一樣的呢?
lishixinzhi/Article/program/ASP/201311/21749
這唯培并樣循環(huán)
%
Dim numi As Integer
For numi = 1 To TextBox1.Text
%
asp:RadioButton ID="RadioButton中檔%=numi%" runat="server" GroupName="leibie%=numi%" /br /指跡
% next %
添加一個(gè)OPTION控件虛陸,復(fù)差滲頃制-粘貼,建立 控件組
比如喊培復(fù)制6個(gè)
代碼如下
Private Sub Command1_Click()
For i = 0 To 5
If Option1(i).Value = True Then MsgBox Option1(i).Caption "被選中"
Next
End Sub