资讯

精准传达 • 有效沟通

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

vb.net窗体控件 vba窗体控件

VB.net,在模块中怎样引用到窗体中的控件

在模块中引用到窗体中的控件,必须在该控件名之前由相应的窗体名作为前导,例如:

我们提供的服务有:网站设计、网站制作、微信公众号开发、网站优化、网站认证、额尔古纳ssl等。为近千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的额尔古纳网站制作公司

1,要引用窗体Form1中的控件TextBox1

MsgBox(Form1.TextBox1.Text)

2,要引用窗体Form2中的控件TextBox1

MsgBox(Form2.TextBox1.Text)

使用VB.NET的五个技巧之窗体嵌套

NET框架组件太大了 比任何以前所写的封装功能的库都要大 这样有好处 因为它大幅削减了建立应用程序所需编写的代码 但是也使我们不可能完全了解该框架组件 但是我们很容易从中找到一些技巧

窗体嵌套

经验丰富的Visual Basic开发者知道多文档界面(MDI)应用程序能够包含子窗体 那些子窗体由MDI父窗体管理 但是如果没有MDI的能力你怎样实现包含嵌套窗体?例如一个MDI子窗体也可能需要包含另一个窗体

有时能够使用用户控件(UserControl)实现这种功能 但是如果你真的需要把一个窗体嵌套进另一个窗体 有多种方法可以实现 窗体衍生自Control类 这意味着它能被放入另一个窗体的控件集合中 使用如下的逻辑

Dim f As New frmEmbed ()Me Controls Add(f)f Show()

但是很不幸 这段代码将会导致一个运行时(runtime)异常(见图 )

图 试图把一个窗体添加到另一个窗体的控件集合时出现的运行时错误

为了避免这种异常 该窗体的TopLevel属性必须设置为False(见下面的代码)

Dim f As New frmEmbed () f TopLevel = False Me Controls Add(f) f Show()

图 显示的是使用上面的逻辑实现的一个窗体嵌入另一个窗体 嵌入的窗体有一个标题条(它的颜色是未激活的系统颜色) 因此该嵌入窗体能在容器窗体内四处拖动 在图 中 该窗体从它的开始位置(左上角)拖到了右下角

图 在容器窗体中有一个嵌入的窗体 嵌入的窗体能在容器窗体中拖动

通常在显示嵌入的窗体前先设置它的位置 这只需要简单的设置嵌入窗体的Left和Top属性 嵌入窗体的位置与容器窗体是相对的

与MDI子窗体不同 嵌入窗体能覆盖容器窗体上的控件 图 显示了它们的不同

图 嵌入窗体(左)可以覆盖容器窗体上的控件 MDI子窗体(右)不能覆盖MDI父窗体上的控件

在右边的MDI例子中 没有办法使按钮隐藏在子窗体的后面 但是在左边该按钮被嵌入窗体覆盖了

当窗体第一次被嵌入时 它将显示在容器窗体上的已存在的控件的后面 当它被点击时 它走向前台并停留在那儿 这会打扰用户 但是能通过插入下面的代码防止这种情况发生

f BringToFront()

嵌入的窗体可以包含其它的嵌入窗体 没有实际的限制 图 显示了一个本身包含嵌入的窗体的嵌入窗体

lishixinzhi/Article/program/net/201311/12398

vb.net 怎样将窗体和所有控件根据电脑分辨率固定?

使用SplitContainer1控件不就行了,SplitContainer1控件的Fixedpanel属性设置为Panel1\x0d\x0aSplitContainer1的Panel2里面再放置一个SplitContainer2\x0d\x0aSplitContainer2控件的Fixedpanel属性设置为Panel2\x0d\x0a二个SplitContainer设置为水平折为器方向\x0d\x0a至于播放器控件就放在SplitContainer2控件Panel1里面\x0d\x0a播放器控件DOCK设置为FILL\x0d\x0a\x0d\x0a这样不管放大缩小界面,播放器控件总是自适应窗体大小而菜单按钮总是在最边缘

vb.net入门之分组控件: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.net窗体控件 vba窗体控件
文章位置:http://cdkjz.cn/article/dddhooj.html
多年建站经验

多一份参考,总有益处

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

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

大客户专线   成都:13518219792   座机:028-86922220