关于VB.net动态控件问题
动态创建一个窗体怎样在这个窗体中创建动态控件(比如按钮)又怎样写这些控件的事件窗体是动态添加的,2楼的只有动态控件的写法啊。我的意思是本来工程里面有一个可见的窗体F1,然...
动态创建一个窗体怎样在这个窗体中创建动态控件(比如按钮)又怎样写这些控件的事件
窗体是动态添加的,2楼的只有动态控件的写法啊。我的意思是本来工程里面有一个可见的窗体F1,然后再动态创建一个窗体F2…再在这个动态窗体上创建动态控件,用addcontrols前面应该用F2还是F1? 展开
窗体是动态添加的,2楼的只有动态控件的写法啊。我的意思是本来工程里面有一个可见的窗体F1,然后再动态创建一个窗体F2…再在这个动态窗体上创建动态控件,用addcontrols前面应该用F2还是F1? 展开
2个回答
展开全部
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bt As New Button
Me.Controls.Add(bt)
AddHandler bt.Click, AddressOf bt_Click
End Sub
手动添加如下 也可以是其他事件 :
Private Sub bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("asasdsadad")
End Sub
'-----------------------------
原理是一样的,Button1为动态创建F2
Dim F2 As Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
F2 = New Form
F2.Owner = Me
F2.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim bt As New Button
F2.Controls.Add(bt)
AddHandler bt.Click, AddressOf bt_Click
End Sub
Private Sub bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("asasdsadad")
End Sub
Dim bt As New Button
Me.Controls.Add(bt)
AddHandler bt.Click, AddressOf bt_Click
End Sub
手动添加如下 也可以是其他事件 :
Private Sub bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("asasdsadad")
End Sub
'-----------------------------
原理是一样的,Button1为动态创建F2
Dim F2 As Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
F2 = New Form
F2.Owner = Me
F2.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim bt As New Button
F2.Controls.Add(bt)
AddHandler bt.Click, AddressOf bt_Click
End Sub
Private Sub bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("asasdsadad")
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询