Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使自定义控件中的按钮触发onClick事件,并在自定义控件所在的主窗体中对其进行处理?_C# - Fatal编程技术网

C# 如何使自定义控件中的按钮触发onClick事件,并在自定义控件所在的主窗体中对其进行处理?

C# 如何使自定义控件中的按钮触发onClick事件,并在自定义控件所在的主窗体中对其进行处理?,c#,C#,在C#中,我创建了一个从UserControl继承的自定义控件,并在自定义控件中添加了一个按钮btnInTrayMap。 然后我将自定义控件添加到主窗体中。主窗体还有一个按钮用于比较他们的行为。 我观察到的是,主窗体上的按钮在单击时工作正常。但是,自定义控件中的按钮btnInTrayMap在单击时根本没有响应 自定义控件中的按钮有以下代码: public partial class TrayMap : UserControl { public

在C#中,我创建了一个从UserControl继承的自定义控件,并在自定义控件中添加了一个按钮btnInTrayMap。 然后我将自定义控件添加到主窗体中。主窗体还有一个按钮用于比较他们的行为。 我观察到的是,主窗体上的按钮在单击时工作正常。但是,自定义控件中的按钮btnInTrayMap在单击时根本没有响应

自定义控件中的按钮有以下代码:

public partial class TrayMap : UserControl
{                      
    public TrayMap()
    {                                    
        InitializeComponent();               
    }        

    public event EventHandler MyCustomClickEvent;

    protected virtual void OnMyCustomClickEvent(object sender, EventArgs e)
    {            
        if (MyCustomClickEvent != null)
            MyCustomClickEvent(this, e);
    }

    private void btnInTrayMap_Click(object sender, EventArgs e)
    {            
        OnMyCustomClickEvent(sender, EventArgs.Empty);            
    } 
}
我相信TrayMap.designer.cs中的btnTrayMap.Click事件处理程序可能导致了以下问题:

this.btnInTrayMap.Click += new System.EventHandler(this.btnInTrayMap_Click);
在主表格中,我有以下代码:

public partial class Form1 : Form
{        
    public Form1()
    {
        InitializeComponent();
    }

    private void btnInForm_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Test Button In Form", "btnInForm Button Clicked", MessageBoxButtons.OK);
    }

    public void MyCustomClickEvent(object sender, EventArgs e)
    {
        Button button = sender as Button;
        MessageBox.Show("Test Button In TrayMap", button.Text + " Button Clicked", MessageBoxButtons.OK);
    }
}
我想知道如何设置事件委派,以便在单击按钮btnInTrayMap时执行主窗体中的MyCustomClickEvent方法。
谢谢。

您尚未在主窗体中注册您的活动。这样试试

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        trayMap.MyCustomClickEvent += MyCustomClickEvent;  // i'm assuming trayMap is the name of user control in main form.
    }

    private void btnInForm_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Test Button In Form", "btnInForm Button Clicked", MessageBoxButtons.OK);
    }

    private void MyCustomClickEvent(object sender, EventArgs e)
    {
        Button button = sender as Button;
        MessageBox.Show("Test Button In TrayMap", button.Text + " Button Clicked", MessageBoxButtons.OK);
    }
}

您是否已在主窗体中注册了
MyCustomClickEvent
?如何在主窗体中注册MyCustomClickEvent?请给出一个示例。谢谢。显示你的主页用户控制代码。我已经编辑了我的原始文章,包括所有代码