熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

vb.net入門之分組控件:GroupBox控件

2022-06-13   來源: ASP編程 

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

  為了獲得清晰的用戶界面而將相關的窗體元素進行可視化分組

  編程分組如對單選按鈕進行分組

  為了在設計時將多個控件作為一個單元來移動

  在中有GroupBoxPanelTabControl這三個控件可以實現上面所提到的三個分組目的所以我們稱它們為分組控件

  這三個控件在功用上十分的相似特別是GroupBox和Panel控件只存在一點細微的差別而已(這個差別是只有GroupBox控件可以顯示標題而只有Panel控件可以有滾動條)這裡我們就先來了解GroupBox控件的使用

  GroupBox(控件組)控件一般是作為其他控件的組的容器的形式存在的這樣有利於用戶識別使界面變得更加友好(GroupBox控件相當於Visual Basic以前版本的Frame控件)使用控件組控件可以將一個窗體中的各種功能進一步進行分類例如將各種選項按鈕控件分隔開

  當移動單個GroupBox控件時它所包含的所有控件也將一起移動

  在大多數情況下對控件組控件沒有實際的操作我們用它對控件進行分組通常沒有必要響應它的事件不過它的NameText和Font等屬性可能會經常被修改以適應應用程序在不同階段的要求

  GroupBox控件在工具箱中的圖標如圖所示入門——分組控件:GroupBox控件的使用(圖一)

  一GroupBox控件的常用屬性

  Anchor和Dock這兩個屬性是所有有用戶界面的控件都有的定位屬性這裡就不啰嗦了

  Name屬性標識控件的對象名稱

  Text屬性顯示在GroupBox控件右上方的標題文字可以用來標識該控件組的描述

  Font和ForeColor屬性用於改變GroupBox控件的文字大小以及文字的顏色需要注意的時候它不單改變GroupBox控件的Text屬性的文字外觀同時也改變其內部控件的顯示的Text屬性的文字外觀

  二創建一組控件

  在窗體上放置GroupBox控件從工具箱中拖放一個GroupBox控件到窗體上的合適位置調整大小

  在屬性窗口中改變GroupBox控件的Text屬性作為它的標題

  在GroupBox控件內拖放其它需要的控件例如RadioButton控件

  設置示例如圖一所示

  入門——分組控件:GroupBox控件的使用(圖二)

  圖一用控件組控件對單選按鈕分組

  我們在拖動單個GroupBox控件的時候它內部的控件也會隨著移動以保持和GroupBox的相對位置不變同理刪除GroupBox控件時它所包含的所有控件也會被刪除掉

  當我們調整GroupBox控件所包含的控件的Anchor和Dock屬性的時候其參照物將不是Form窗體而是GroupBox控件了

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

  雖然GroupBox控件是在設計時用視圖設計布局效果最好但是無可避免地很多特殊情況下也是需要在運行做添加控件到控件組中的這裡我們就用代碼來完成上圖一界面的繪制

  動態添加控件一般需要經過下面三個步驟

  創建要添加的控件實例

  設置新控件的屬性

  將控件添加到父控件的 Controls 集合

  在Form代碼的任意位置增加初始化控件的過程InitializeControl()代碼如下所示

  Sub InitializeControl()

  首先添加Label和TextBox控件

  Dim Label As New SystemWindowsFormsLabel

  Dim TextBox As New SystemWindowsFormsTextBox

  Label

  LabelLocation = New SystemDrawingPoint(

  LabelName = Label

  LabelSize = New SystemDrawingSize(

  LabelTabIndex =

  LabelText = 戶主姓名

  TextBox

  TextBoxLocation = New SystemDrawingPoint(

  TextBoxName = TextBox

  TextBoxSize = New SystemDrawingSize(

  TextBoxTabIndex =

  TextBoxText =

  把它們添加到父控件Form的Controls集合中

  MeControlsAdd(TextBox

  MeControlsAdd(Label

  添加三個GroupBox控件

  Dim GroupBox As New SystemWindowsFormsGroupBox

  Dim GroupBox As New SystemWindowsFormsGroupBox

  Dim GroupBox As New SystemWindowsFormsGroupBox

  GroupBox

  GroupBoxBackColor = SystemDrawingSystemColorsControl

  GroupBoxLocation = New SystemDrawingPoint(

  GroupBoxName = GroupBox

  GroupBoxSize = New SystemDrawingSize(

  GroupBoxTabIndex =

  GroupBoxTabStop = False

  GroupBoxText = 性別

  GroupBox

  GroupBoxLocation = New SystemDrawingPoint(

  GroupBoxName = GroupBox

  GroupBoxSize = New SystemDrawingSize(

  GroupBoxTabIndex =

  GroupBoxTabStop = False

  GroupBoxText = 單元

  GroupBox

  GroupBoxLocation = New SystemDrawingPoint(

  GroupBoxName = GroupBox

  GroupBoxSize = New SystemDrawingSize(

  GroupBoxTabIndex =

  GroupBoxTabStop = False

  GroupBoxText = 樓層

  把它們添加到父控件Form的Controls集合中

  MeControlsAdd(GroupBox

  MeControlsAdd(GroupBox

  MeControlsAdd(GroupBox

  添加RadioButton控件並分別繪制在GroupBox控件內

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  Dim RadioButton As New SystemWindowsFormsRadioButton

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 男性

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 女性

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 二單元

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 三單元

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 一單元

  RadioButton

  RadioButtonBackColor = SystemDrawingSystemColorsControl

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 四單元

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 二樓

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 三樓

  RadioButton

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 一樓

  RadioButton

  RadioButtonBackColor = SystemDrawingSystemColorsControl

  RadioButtonLocation = New SystemDrawingPoint(

  RadioButtonName = RadioButton

  RadioButtonSize = New SystemDrawingSize(

  RadioButtonTabIndex =

  RadioButtonText = 四樓

  分別把它們添加到父控件GroupBox的Controls集合中

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  GroupBoxControlsAdd(RadioButton

  End Sub

  把上一頁的代碼復制添加後把控件初始化過程InitializeControl()過程添加到Form的New構造函數中如下圖二所示

  入門——分組控件:GroupBox控件的使用(圖三)

  圖二在New構造函數中添加過程InitializeControl()

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

  入門——分組控件:GroupBox控件的使用(圖二)

  圖三使用代碼動態生成的控件布局圖


From:http://tw.wingwit.com/Article/program/ASP/201311/21749.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.