
標簽框的作用:可以顯示(輸出)文本信息,但不能輸入文本。文本框的作用:可以輸入文本的顯示區(qū)域,既可以輸入也可以輸出文本,并可對文本進行編輯。操作方法如下:

成都創(chuàng)新互聯(lián)公司服務(wù)項目包括德化網(wǎng)站建設(shè)、德化網(wǎng)站制作、德化網(wǎng)頁制作以及德化網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,德化網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到德化省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
1、首先新建一個工程,新建一個標準Exe工程,如下圖。
2、找到工具箱中標簽框,找到后可以直接雙擊控件,即可成功繪制。
3、或者可以使用鼠標選中它,在頁面上繪制。
4、如果對它的外觀不滿意,可以在屬性窗口中
5、鼠標找到并雙擊控件,即可進入代碼窗口,就可以在這個根據(jù)自己的需要進行添加修改了。
Private?ctrName?As?String?'控件名稱
Private?isClick?As?Boolean?'鼠標點擊狀態(tài)
'注:如果已知點擊目標控件的父控件,ctrParent變量可以不要。
Private?WithEvents?ctrParent?As?Control?'父控件
Private?Sub?ControlAMouseDown(sender?As?Object,?e?As?MouseEventArgs)?_
Handles?Button1.MouseDown
isClick?=?(e.Button?=?MouseButtons.Left?_
Or?e.Button?=?MouseButtons.Right)?'左鍵或右鍵按下
If?isClick?Then
Dim?ctr?As?Control?=?CType(sender,?Control)?'轉(zhuǎn)換Object為控件類型
ctrName?=?ctr.Name?'獲取控件名稱
ctrParent?=?ctr.Parent?'獲取控件的父控件
End?If
End?Sub
'增加這個父控件事件,是為了正確判別鼠標彈起時是否已進入指定目標
Private?Sub?ParentMouseMove(sender?As?Object,?e?As?EventArgs)?_
Handles?ctrParent.MouseMove?'如果已取消ctrParent變量,改為相應(yīng)的父控件
If?isClick?Then?isClick?=?False?'點擊狀態(tài)關(guān)閉
End?Sub
Private?Sub?ControlBMouseUp(sender?As?Object,?e?As?EventArgs)?_
Handles?Button2.MouseEnter
If?isClick?Then?'如果點擊狀態(tài)為打開
Dim?ctr?As?Control?=?CType(sender,?Control)?'轉(zhuǎn)換Object為控件類型
MsgBox(ctrName??"?|?"??ctr.Name)?'彈出消息顯示結(jié)果
End?If
End?Sub
我們對控件進行分組的原因不外乎三個
為了獲得清晰的用戶界面而將相關(guān)的窗體元素進行可視化分組
編程分組 如對單選按鈕進行分組
為了在設(shè)計時將多個控件作為一個單元來移動
在中 有GroupBox Panel TabControl這三個控件可以實現(xiàn)上面所提到的三個分組目的 所以我們稱它們?yōu)榉纸M控件
這三個控件在功用上十分的相似 特別是GroupBox和Panel控件 只存在一點細微的差別而已(這個差別是 只有GroupBox控件可以顯示標題 而只有Panel控件可以有滾動條) 這里我們就先來了解GroupBox控件的使用
GroupBox(控件組)控件一般是作為其他控件的組的容器的形式存在的 這樣有利于用戶識別 使界面變得更加友好(GroupBox控件相當于Visual Basic以前版本的Frame控件) 使用控件組控件可以將一個窗體中的各種功能進一步進行分類 例如 將各種選項按鈕控件分隔開
當移動單個GroupBox控件時 它所包含的所有控件也將一起移動
在大多數(shù)情況下 對控件組控件沒有實際的操作 我們用它對控件進行分組 通常沒有必要響應(yīng)它的事件 不過 它的Name Text和Font等屬性可能會經(jīng)常被修改 以適應(yīng)應(yīng)用程序在不同階段的要求
GroupBox控件在工具箱中的圖標如圖所示
一 GroupBox控件的常用屬性
Anchor和Dock 這兩個屬性是所有有用戶界面的控件都有的定位屬性 這里就不啰嗦了
Name屬性 標識控件的對象名稱
Text屬性 顯示在GroupBox控件右上方的標題文字 可以用來標識該控件組的描述
Font和ForeColor屬性 用于改變GroupBox控件的文字大小以及文字的顏色 需要注意的時候 它不單改變GroupBox控件的Text屬性的文字外觀 同時也改變其內(nèi)部控件的顯示的Text屬性的文字外觀
二 創(chuàng)建一組控件
在窗體上放置GroupBox控件 從工具箱中拖放一個GroupBox控件到窗體上的合適位置 調(diào)整大小
在屬性窗口中改變GroupBox控件的Text屬性 作為它的標題
在GroupBox控件內(nèi)拖放其它需要的控件 例如RadioButton控件
設(shè)置示例 如圖一所示
圖一 用控件組控件對單選按鈕分組
我們在拖動單個GroupBox控件的時候 它內(nèi)部的控件也會隨著移動 以保持和GroupBox的相對位置不變 同理 刪除GroupBox控件時 它所包含的所有控件也會被刪除掉
當我們調(diào)整GroupBox控件所包含的控件的Anchor和Dock屬性的時候 其參照物將不是Form窗體 而是GroupBox控件了
三 編程添加GroupBox控件以及它所包含的控件
雖然GroupBox控件是在設(shè)計時用視圖設(shè)計布局效果最好 但是無可避免地 很多特殊情況下也是需要在運行做添加控件到控件組中的 這里我們就用代碼來完成上圖一界面的繪制
動態(tài)添加控件一般需要經(jīng)過下面三個步驟
創(chuàng)建要添加的控件實例
設(shè)置新控件的屬性
將控件添加到父控件的 Controls 集合
在Form 代碼的任意位置增加初始化控件的過程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 )
添加三個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
把上一頁的代碼復(fù)制添加后 把控件初始化過程InitializeControl()過程添加到Form 的New構(gòu)造函數(shù)中 如下圖二所示
圖二 在New構(gòu)造函數(shù)中添加過程InitializeControl()
現(xiàn)在按F 運行 Form 的窗體控件布局(如下圖三所示)是不是和我們手工布局的圖一的布局是一樣的呢?
lishixinzhi/Article/program/ASP/201311/21749