如何将事件从usercontrol调用到主窗体C# [英] How do I can call event from usercontrol to main form C#

查看:235
本文介绍了如何将事件从usercontrol调用到主窗体C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个userControl,我在那里按钮,我想在我的主表单中调用事件点击。



我尝试了什么:



有我尝试过的,但它没有在表单中工作。



UserControl

Hi,

I have a userControl and I've buttons there, I'd like to call event click in my main form.

What I have tried:

There is what I've tried, but it doesn't work in form.

UserControl

public event EventHandler ButtonClick;

        protected void Button1_Click(object sender, EventArgs e)
        {
            //bubble the event up to the parent
            if (this.ButtonClick != null)
                this.ButtonClick(this, e);
        }





表格



Form

<pre lang="c#">GameField.ButtonClick += new EventHandler(UserControl_ButtonClick);

        protected void UserControl_ButtonClick(object sender, EventArgs e)
        {
            //handle the event 
        }





我无法从主窗体中看到类userControl。这可能是因为我有一个部分类。



I can't see class userControl from main form. It might be cause I've a partial class.

public partial class GameField : UserControl

推荐答案

请看这里:在两个表单之间传输信息,第2部分:子项到父项 [ ^ ] - 它基于表单,但它是与控件完全相同(表格无论如何都来自Control!)。
See here: Transferring information between two forms, Part 2: Child to Parent[^] - it's based on forms, but it's exactly the same with Controls (Forms are derived from Control anyway!).


这篇关于如何将事件从usercontrol调用到主窗体C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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