如何让form1上的控件执行form2上控件的代码?
比如form1上有按钮button_form1,form2上有按钮button_form2,button_form2_click下有代码:
msgbox("button_form2")
我想在点击button_form1时去执行button_form2的单击代码,请问如何做?
是同一个程序的两个不同的窗体之间。
[解决办法]
两个窗体都被打开了吧
关系是什么呢 是form1打开的form2 还是2打开的1
[解决办法]
最简单的办法,将form2的按钮设置为public的。
然后form2.button1.PerformClick();
[解决办法]
Public Class Form1
Private Sub Form_Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form_Button1.Click
Dim frm2 As New Form2
frm2.Show()
frm2.Form2_Button1.PerformClick()
End Sub
End Class
Public Class Form2
Private Sub Form2_Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2_Button1.Click
MsgBox(Me.ToString)
End Sub
End Class