如何将Button1从UserControl1导入Form1

时间:2018-05-01 08:04:28

标签: vb.net

我试过了,但它没有用。

我还尝试了从Button1.Click输入到Startpage(UserControl1)的{​​{1}}。

我的代码:

Form1

1 个答案:

答案 0 :(得分:1)

我认为这是最不可取的选择:

Private WithEvents StartpageButton1 As Button

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    StartpageButton1 = Startpage.Button1
End Sub

Private Sub StartpageButton1_Click(sender As Object, e As EventArgs) Handles StartpageButton1.Click
    '...
End Sub

我认为这是一个更好的选择:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AddHandler Startpage.Button1, AddressOf StartpageButton1
End Sub

Private Sub StartpageButton1_Click(sender As Object, e As EventArgs)
    '...
End Sub

Private Sub Form1_FormClosing(sender As Object, e As CancelEventArgs) Handles MyBase.FormClosing
    RemoveHandler Startpage.Button1, AddressOf StartpageButton1
End Sub

我认为这是最好的选择:

Public Event Button1Click As EventHandler

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RaiseEvent Button1Click(Me, EventArgs.Empty)
End Sub

该部分将进入用户控件,然后将采用以下形式:

Private Sub Startpage_Button1Click(sender As Object, e As EventArgs) Handles Startpage.Button1Click
    '...
End Sub