资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb点虐 的代码入门 vbnet ide

VB.NET是什么?怎么才能入门

所谓VB.NET就是VB7.0,现在已经到了VB8.0,你要是初学的,我建议你去买清华大学出版社的VB.NET程序设计与上机指导这本书,我刚学完挺不错的。

成都创新互联公司主要从事成都网站设计、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务润州,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

vb点虐 入门之分组控件:GroupBox控件

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

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

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

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

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

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

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

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

在大多数情况下 对控件组控件没有实际的操作 我们用它对控件进行分组 通常没有必要响应它的事件 不过 它的Name Text和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 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控件内

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

把上一页的代码复制添加后 把控件初始化过程InitializeControl()过程添加到Form 的New构造函数中 如下图二所示

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

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

lishixinzhi/Article/program/ASP/201311/21749

vb点虐 入门——DomainUpDown 控件的使用

DomainUpDown 控件在外观上都是由一个文本框和一对可以上下移动列表的箭头组成 如下图一所示

图一 控件外观

其功能类似于组合框 您可以将任何类型的数据放入列表 同时在文本框中显示单个字符串值 让用户可以通过单击上箭头和下箭头来浏览数据

在工具箱中 它的图标显示如下图二所示

图二 工具箱中的DomainUpDown 控件

DomainUpDown 控件的主要属性为 Items ReadOnly Wrap

一 改变文字显示与上下箭头的方向

我们可以通过 TextAlign 属性来改变控件中的文本对齐方式 通过 UpDownAlign 属性改变它的上下按钮对齐方式

示例 在DomainUpDown 控件 Text 属性中输入 即文本框显示的字符 把 TextAlign 属性 设置为Center UpDownAlign 属性设置为Left 显示效果如下图三所示

图三 改变DomainUpDown 控件显示外观

二 ReadOnly 属性

ReadOnly 属性用于设置用户是否只能使用向上或向下按钮更改文本 默认值为False 如果为False 则用户可以在文本框中进行输入 如果为True 则文本输入框变为灰色 用户只能通过Up Down按钮来选择列表 且当用户使用键盘键入的时候 控件自动完成匹配 如假设列表中存在下面三个字符串的项 aa b 控件ReadOnly 属性为True 则当我们在文本框中键入 时候 控件将自动匹配字符串 如下图四所示

图四 DomainUpDown 自动匹配用户输入

三 为DomainUpDown 控件添加\删除列表项

通过 字符串集合编辑器 编辑

我们可以在设计视图中为 DomainUpDown 控件添加或者删除列表项 步骤如下

在 属性 窗口中找到 Items 属性 单击属性后的 按钮 弹出 字符串集合编辑器 然后进行编辑 注意每行为一个项

使用代码添加\删除

可以使用 Items 集合的 Add 方法将项添加到项列表的末尾 使用 Insert 方法将项插入到列表中的指定位置

可以使用 Items 集合的 Remove 方法按照名称移除项 使用 RemoveAt 方法按照项的位置移除项

示例代码如下

在控件列表尾部添加一个字符串 cccc

DomainUpDown Items Add( cccc )

把字符串 cccc 插入到索引位置 (提示 索引是从 开始)

DomainUpDown Items Insert( cccc )

删除列表中的字符串为 cccc 的项

DomainUpDown Items Remove( cccc )

删除列表中索引位置为 的项

DomainUpDown Items RemoveAt( )

使用Clear方法清除DomainUpDown 控件的所有项

DomainUpDown Items Clear()

四 DomainUpDown 控件的其它属性

Sorted 属性 指示项集合是否排序 当 Sorted 设置为 true 时 将按字母顺序对集合排序 默认为False

Wrap 属性 当 Wrap 设置为 true 时 如果达到了集合中的最后一项并继续滚动 则列表将从第一项重新开始 看起来像是连续的

lishixinzhi/Article/program/net/201311/11362


分享标题:vb点虐 的代码入门 vbnet ide
转载来源:http://cdkjz.cn/article/ddsgjci.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220