189 8069 5689

vb.net动态添加代码,vb 动态添加控件

vb.net 如何动态添加N个一样的textbox控件?(马上采纳)

自己在窗体上加一个按钮,以下是详细代码(整个类)

创新互联成立10年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供网站建设、成都网站制作、网站策划、网页设计、国际域名空间、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,创新互联通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。

Public Class Form1

Private N As Integer '用来记添加要加入textbox的个数

Private PL As Integer = 10 'textbox相对于窗体的Left

Private PT As Integer = 10 'textbox相对于窗体的Top

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.AutoScroll = True '窗体自动显示滚动条

N = 10 '初始化N为10

For i = 1 To N

Dim newtextbox As New TextBox

newtextbox.Left = PL

newtextbox.Top = PT

Me.Controls.Add(newtextbox)

PT += newtextbox.Height + 10 '各 newtextbox上下间隔10-------PL不变是希望左对齐

Next

End Sub

End Class

vb.net动态添加控件问题

你还要把过程与控件事件绑定

AddHandler 控件.事件名,addressof 事件过程

RemoveHandler 这个是取消绑定

没发现自动生存的事件过程后面还有一个Hander button1.Click之类的,这就是所谓事件句柄。反而跟过程名没关系,改成阿猫阿狗也可以。

例外也可以像 Private WithEvents obj as ControlClass 这么声明控件变量,估计像vb6那样会在下拉列表中列出事件系列。

哎呀,说了半天跑题了。 ff不存在嘛多半不是它的作用域范围内,应该把ff变量定义在类中,而不是类中的某个过程中。

最好把代码添多一点,把ff部分也添出来看看。

VB.NET 用二维数组的方式动态加控制 例如在窗体上动态添加GroupBox,然后再在GroupBox里动态添加控件

下面这段代码完成,在窗体上用语句添加2个 GroupBox控件,且在每个GroupBox控件中添加4个 RadioButton 控件。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim i As Integer

'添加2个GroupBox

Dim MyGroupBox(2) As GroupBox

For i = 1 To 2

'将一个GroupBox控件加入到Form上

MyGroupBox(i) = New GroupBox

Me.Controls.Add(MyGroupBox(i))

'设置该GroupBox控件的属性

MyGroupBox(i).Height = 240

MyGroupBox(i).Width = 600

MyGroupBox(i).Top = (i - 1) * (240 + 20) + 20

MyGroupBox(i).Left = 20

'修改新加入控件的Text值

MyGroupBox(i).Text = "GroupBox" CStr(i)

Next

'每个GroupBox中添加4个单选按钮

Dim MyRadioButton1(4) As RadioButton

Dim MyRadioButton2(4) As RadioButton

For i = 1 To 4

MyRadioButton1(i) = New RadioButton

Me.Controls.Add(MyRadioButton1(i))

MyRadioButton1(i).Parent = MyGroupBox(1)

'设置该GroupBox控件的属性

MyRadioButton1(i).Height = 20

MyRadioButton1(i).Width = 120

MyRadioButton1(i).Top = (i - 1) * (20 + 20) + 40

MyRadioButton1(i).Left = 20

'修改新加入控件的Text值

MyRadioButton1(i).Text = "RadioButton1_" CStr(i)

Next

For i = 1 To 4

MyRadioButton2(i) = New RadioButton

Me.Controls.Add(MyRadioButton2(i))

MyRadioButton2(i).Parent = MyGroupBox(2)

'设置该GroupBox控件的属性

MyRadioButton2(i).Height = 20

MyRadioButton2(i).Width = 120

MyRadioButton2(i).Top = (i - 1) * (20 + 20) + 40

MyRadioButton2(i).Left = 20

'修改新加入控件的Text值

MyRadioButton2(i).Text = "RadioButton2_" CStr(i)

Next

End Sub


当前名称:vb.net动态添加代码,vb 动态添加控件
转载注明:http://cdxtjz.cn/article/hcoisg.html

其他资讯