
正文
vb.net动态添加代码,vb 动态添加控件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
vb.net 如何动态添加N个一样的textbox控件?(马上采纳)
自己在窗体上加一个按钮,以下是详细代码(整个类)
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
相关问答
Q1: vb.net动态添加控件问题
你还要把过程与控件事件绑定
AddHandler 控件.事件名,addressof 事件过程
RemoveHandler 这个是取消绑定
没发现自动生存的事件过程后面还有一个Hander button1.Click之类的,这就是所谓事件句柄。反而跟过程名没关系,改成阿猫阿狗也可以。
例外也可以像 Private WithEvents obj as ControlClass 这么声明控件变量,估计像vb6那样会在下拉列表中列出事件系列。
哎呀,说了半天跑题了。 ff不存在嘛多半不是它的作用域范围内,应该把ff变量定义在类中,而不是类中的某个过程中。
最好把代码添多一点,把ff部分也添出来看看。
Q2: 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







