Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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
Winforms 如何在winform C上将usercontrol中的事件按钮单击创建为usercontrol_Winforms_Events_User Controls - Fatal编程技术网

Winforms 如何在winform C上将usercontrol中的事件按钮单击创建为usercontrol

Winforms 如何在winform C上将usercontrol中的事件按钮单击创建为usercontrol,winforms,events,user-controls,Winforms,Events,User Controls,我对usercontrol有如下问题: 我有一个formA包含1个名为UC_wrap的用户控件 和UC_wrap包含一个名为UC_child的用户控件 在UC_中,子项包含:一个按钮添加、一个按钮编辑、一个按钮删除和一个文本框 但我不知道如何为formA上的每个按钮创建事件? 求求你,有人帮帮我 试试这个 //UC_child - child user control code public event EventHandler addClick; public event EventHand

我对usercontrol有如下问题:

我有一个formA包含1个名为UC_wrap的用户控件 和UC_wrap包含一个名为UC_child的用户控件 在UC_中,子项包含:一个按钮添加、一个按钮编辑、一个按钮删除和一个文本框 但我不知道如何为formA上的每个按钮创建事件? 求求你,有人帮帮我

试试这个

//UC_child - child user control code

public event EventHandler addClick;
public event EventHandler editClick;
public event EventHandler deleteClick;

//call above event in each button click ie.
private void btnAdd_Click(object sender, EventArgs e)
{
    if (addClick != null)
         addClick(sender, e);
}
//Do same for other edit and delete button




//UC_wrap- UC_wrap usercontrol code
//Hand UC_Child event in UC_wrap
//Create event again in UC_wrap
public event EventHandler addClick;
public event EventHandler editClick;
public event EventHandler deleteClick;

private void UC_Child_Load(object sender, EventArgs e)
{
   UC_Child1.addClick += new EventHandler(add_Click);
   //Do same for other edit and delete button
}

private void add_Click(object sender, EventArgs e)
{
   if (addClick != null)
         addClick(sender, e);
}


//formA-This is your form code
private void formA_Load(object sender, EventArgs e)
{
   UC_wrap1.addClick += new EventHandler(add_Click);
   //Do same for other edit and delete button
}
private void add_Click(object sender, EventArgs e)
{
   //Place your code here.
}

您可以为UC_Wrap装配一个事件,将它从按钮接收到的任何事件转发给订阅它的人

partial class UC_Wrap : Control
{
    public event EventHandler AddButtonClick
    {
        add { addButton.Click += value; }
        remove { addButton.Click -= value; }
    }
    // etc
}
然后UC_控件可以转发这些事件

partial class UC_Control : Control
{
    public event EventHandler AddButtonClickedInWrap
    {
        add { ucWrap.AddButtonClick += value; }
        remove { ucWrap.AddButtonClick -= value; }
    }
    // etc
}
最后,在FormA级别,您可以订阅事件并处理它

partial class FormA : Form
{
    protected override void OnLoad() 
    {
        ucControl.AddButtonClickedInWrap += ActuallyDoSomething;
    }

    private void ActuallyDoSomething(object sender, EventArgs e)
    {
        // do something
    }
}
这可能是最好的办法。我能想到的唯一简单的方法是公开每个子控件,但这有一个主要的缺点,就是暴露的内容远远超出了需要