189 8069 5689

vbnet分组网格 vbnet数组

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

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

创新互联主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、成都全网营销推广、网站程序开发、HTML5响应式网站建设公司成都手机网站制作、微商城、网站托管及成都网站改版、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为成都凿毛机行业客户提供了网站营销推广服务。

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

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

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

在中 有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点虐 的dbgrid控件的网格线该如何设置

1.DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示 Recordset 对象的记录和字段。可以使用 DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序。DataGrid 控件可以在设计时快速进行配置,只需少量代码或无需代码。当在设计时设置了DataGrid 控件的 DataSource 属性后,就会用数据源的记录集来自动填充该控件,以及自动设置该控件的列标头。然后您就可以编辑该网格的列;删除、重新安排、添加列标头、或者调整任意一列的宽度。

2.在运行时,可以在程序中切换 DataSource 来察看不同的表,或者可以修改当前数据库的查询,以返回一个不同的记录集合。

注意 DataGrid 控件与 Visual Basic 5.0中的 DBGrid 是代码兼容的,除了一个例外:DataGrid 控件不支持 DBGrid 的“解除绑定模式”概念。DBGrid 控件包括在 Visual Basic 的 Tools 目录中。

可能的用法

查看和编辑在远程或本地数据库中的数据。

与另一个数据绑定的控件(诸如 DataList 控件)联合使用,使用DataGrid控件来显示一个表的记录,这个表通过一个公共字段链接到由第二个数据绑定控件所显示的表。

3.使用 DataGrid 控件的设计时特性

可以不编写任何代码,只通过使用 DataGrid 控件的设计时特性来创建一个数据库应用程序。下面的说明概要地说明了在实现 DataGrid 控件的典型应用时的一般步骤。完整的循序渐进的指示,请参阅主题“DataGrid 方案1: 使用 DataGrid 控件创建一个简单数据库应用程序”。

要在设计时实现一个 DataGrid 控件

1. 为要访问的数据库创建一个 Microsoft 数据链接 (.MDL) 文件。请参阅“创建 Northwind OLE DB 数据链接”主题,以获得一个示例。

2. 在窗体上放置一个 ADO Data 控件,并将其 ConnectionString 属性设置为在第 1 步中所创建的OLE DB 数据源。

3. 在这个 Ado Data 控件的 RecordSource 属性中输入一条将返回一个记

录集的 SQL 语句。例如,Select * From MyTableName Where CustID = 12

4. 在窗体上放置一个 DataGrid 控件,并将其 DataSource 属性设置为这个 ADO Data 控件。

5. 右键单击该 DataGrid 控件,然后单击“检索字段”。

6. 右键单击该 DataGrid 控件,然后单击“编辑”。

7. 重新设置该网格的大小、删除或添加网格的列。

8. 右键单击该 DataGrid 控件,然后单击“属性”。

9. 使用“属性页”对话框来设置该控件的适当的属性,将该网格配置为所需的外观和行为。

在运行时更改显示的数据

在创建了一个使用设计时特性的网格后,也可以在运行时动态地更改该网格的数据源。下面介绍实现这一功能的通常方法。

更改 DataSource 的RecordSource

更改所显示的数据的最通常方法是改变该 DataSource 的查询。例如,如果DataGrid 控件使用一个ADO Data控件作为其 DataSource,则重写RecordSource和刷新该ADO Data 控件都将改变所显示的数据。

' ADO Data 控件连接的是 Northwind 数据库的' Products 表。新查询查找所有

' SupplierID = 12 的记录。

Dim strQuery As String

strQuery = "SELECT * FROM Suppliers WHERE SupplierID = 12"

Adodc1.RecordSource = strQuery

Adodc1.Refresh

更改 DataSource

在运行时,可以将 DataSource 属性重新设置为一个不同的数据源。例如,您可能具有若干个 ADO Data 控件,每个控件连接不同的数据库,或设置为不同的 RecordSource 属性。可以简单地将 DataSource 从一个 ADO Data控件重新设置为另一个 ADO Data 控件:

' 将 DataSource 重新设置为一个连接到 Pubs 数据库的、

' 使用 Authors 表的 ADO Data 控件。

Set DataGrid1.DataSource = adoPubsAuthors

重新绑定 DataSource

当将 DataGrid 控件用于一个远程数据库,诸如 SQLServer 时,可以改变表的结构。例如,可以给这个表添加一个字段。在这种情形下,可以调用Rebind 方法根据新的结构来重新创建该网格。注意,如果已经在设计时改变了这个列的布局,DataGrid 控件将会试图重新创建当前的布局,包括任何空的列。不过,通过首先调用 ClearFields 方法,可以强制该网格重新设置所有的列。

从 DataGrid 返回值

在 DataGrid 被连接到一个数据库后,可能想要监视用户单击了哪一个单元。可以使用 RowColChange 事件——而不是 Click 事件。如下所示:

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)

' 显示用户所单击的单元的文字、行和列的信息。

Debug.Print DataGrid1.Text; DataGrid1.Row; DataGrid1.Col

End Sub

使用 CellText 和 CellValue 方法

当一个列使用 NumberFormat 属性设置格式后,CellText 和 CellValue 属性是很有用的。NumberFormat 属性不必更改实际的数据格式就可以更改任何包含数字的列的格式。例如,给定一个网格,其中包含一个名为 ProductID的、包含整数的列。下面的代码将使 DataGrid 以"P-0000" 的格式来显示数据。换句话说,尽管在 ProductID 字段中所包含的实际数值为 "3",但该网格所显示的值将是 "P-0003"。

Private Sub Form_Load()

DataGrid1.Columns("ProductID").NumberFormat = "P-0000"

End Sub

要返回数据库中所包含的实际值,应使用 CellValue 方法,如下所示:

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)

Debug.Print _

DataGrid1.Columns("ProductID").CellValue(DataGrid1.Bookmark)

End Sub

VB点虐 绘制可缩放的网格坐标的相关问题

你用CreateGraphics方法的吧,这是临时的Graphics,只要你把窗口拉出屏幕或者有其他的对话框遮挡,里面的内容就会立即重画,而CreateGraphics是临时的,所以不会再为你画了。有两种方法,第一是重写重画事件Paint,第二当作图片放入pictureBox里(其实pictureBox也是重写Paint事件的)

很清楚可以看到,蓝色的是DrawLines,红色的和绿色的是画椭圆,至于捕获,是重写了MouseMove事件的,在这个事件里进行算法计算,算出对应的点,然后激活Paint事件。


当前文章:vbnet分组网格 vbnet数组
浏览路径:http://cdxtjz.cn/article/ddjpsgc.html

其他资讯