以表单处理用户控件的控件事件 [英] Handle event of a User Control's control in a form

查看:130
本文介绍了以表单处理用户控件的控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UserControl1 中有一个按钮。
我在 Form1 中使用 UserControl1
我想在 Form1 中处理Button的Click事件。

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

我尝试通过以下方式执行: / p>

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

但是收到错误。

推荐答案

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

然后使用新的事件:

AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click






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


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

Public WithEvents Button1 As System.Windows.Forms.Button

然后:

AddHandler uc.Button1.Click, AddressOf Button1_Click

这篇关于以表单处理用户控件的控件事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆