vb.net 处理表单中用户控件控件的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14603960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:06:07  来源:igfitidea点击:

Handle event of a User Control's control in a form

vb.netwinformseventscontrols

提问by Brij

I have a Button in UserControl1. I am using UserControl1in Form1. I want to handle Button's Click event in Form1.

我在UserControl1. 我正在使用UserControl1Form1。我想在Form1.

I tried to do same via:

我试图通过以下方式做同样的事情:

AddHandler userControl1.Button1.Click, AddressOf Button1_Click

And:

和:

Public Sub Button1_Click(ByVal sender As Object, ByVal args As EventArgs) Handles userControl1.Button1.Click

End Sub

but getting error.

但得到错误。

回答by SysDragon

Create your event on the UserControl:

在 上创建您的活动UserControl

Public Class UserControl1

    Public Event UC_Button1Click()

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

End Class

And then use the new event:

然后使用新事件:

AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click


Or you can simply define it like this on the UserControland access to it from outside (not recommended):

或者你可以简单地定义它UserControl并从外部访问它(不推荐):

Public WithEvents Button1 As System.Windows.Forms.Button

And then:

进而:

AddHandler uc.Button1.Click, AddressOf Button1_Click