vb.net

位置:IT落伍者 >> vb.net >> 浏览文章

vb.net入门之分组控件:GroupBox控件


发布日期:2022年02月07日
 
vb.net入门之分组控件:GroupBox控件

我们对控件进行分组的原因不外乎三个

为了获得清晰的用户界面而将相关的窗体元素进行可视化分组

编程分组如对单选按钮进行分组

为了在设计时将多个控件作为一个单元来移动

在中有GroupBoxPanelTabControl这三个控件可以实现上面所提到的三个分组目的所以我们称它们为分组控件

这三个控件在功用上十分的相似特别是GroupBox和Panel控件只存在一点细微的差别而已(这个差别是只有GroupBox控件可以显示标题而只有Panel控件可以有滚动条)这里我们就先来了解GroupBox控件的使用

GroupBox(控件组)控件一般是作为其他控件的组的容器的形式存在的这样有利于用户识别使界面变得更加友好(GroupBox控件相当于Visual Basic以前版本的Frame控件)使用控件组控件可以将一个窗体中的各种功能进一步进行分类例如将各种选项按钮控件分隔开

当移动单个GroupBox控件时它所包含的所有控件也将一起移动

在大多数情况下对控件组控件没有实际的操作我们用它对控件进行分组通常没有必要响应它的事件不过它的NameText和Font等属性可能会经常被修改以适应应用程序在不同阶段的要求

GroupBox控件在工具箱中的图标如图所示

GroupBox控件的常用属性

Anchor和Dock这两个属性是所有有用户界面的控件都有的定位属性这里就不啰嗦了

Name属性标识控件的对象名称

Text属性显示在GroupBox控件右上方的标题文字可以用来标识该控件组的描述

Font和ForeColor属性用于改变GroupBox控件的文字大小以及文字的颜色需要注意的时候它不单改变GroupBox控件的Text属性的文字外观同时也改变其内部控件的显示的Text属性的文字外观

创建一组控件

在窗体上放置GroupBox控件从工具箱中拖放一个GroupBox控件到窗体上的合适位置调整大小

在属性窗口中改变GroupBox控件的Text属性作为它的标题

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

设置示例如图一所示

图一用控件组控件对单选按钮分组

我们在拖动单个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构造函数中如下图二所示

图二在New构造函数中添加过程InitializeControl()

现在按F运行Form的窗体控件布局(如下图三所示)是不是和我们手工布局的图一的布局是一样的呢?

图三使用代码动态生成的控件布局图

上一篇:VB.NET 拖动无边框窗体编程实例

下一篇:VB.NET中的动态代码生成技巧