Home > Back-end >  How to pass multiple button click event from child to parent in c#
How to pass multiple button click event from child to parent in c#

Time:07-11

  • I have 18 buttons on the child form "Control Test" which send event to the parent form
  • Out of 18 buttons, 14 are ON and OFF functionality, making 7 pairs as in the picture
  • The problem is raising the event for each button, it causes very long and messy code, both in the child and the parent form,
  • Is there any less complex way to do it? like I have done with the menu.

Child Form:
enter image description here

Child Form:

// B Plus Relay On Button
public event EventHandler BPRElayOnBtnClicked;
protected virtual void WhenBPRelayOnBtnClicked(EventArgs e)
{
    BPRElayOnBtnClicked.Invoke(this, e);
}
private void BPRelayOn_btn_Click(object sender, EventArgs e)
{
    WhenBPRelayOnBtnClicked(e);
}

    // B Plus Relay OFF Button
public event EventHandler BPRElayOffBtnClicked; 
protected virtual void WhenBPRelayOffBtnClicked(EventArgs e)
{
    BPRElayOnBtnClicked.Invoke(this, e);
}
private void BPRelayOff_btn_Click(object sender, EventArgs e)
{
    WhenBPRelayOffBtnClicked(e);
}



// B Minus Relay ON Button
public event EventHandler BMRElayOnBtnClicked;
protected virtual void WhenBMRelayOnBtnClicked(EventArgs e)
{
    BMRElayOnBtnClicked.Invoke(this, e);
}

private void BMRelayOn_btn_Click(object sender, EventArgs e)
{
    WhenBMRelayOnBtnClicked(e);
}

// B Minus Relay OFF Button
public event EventHandler BMRElayOffBtnClicked;
protected virtual void WhenBMRelayOffBtnClicked(EventArgs e)
{
    BMRElayOffBtnClicked.Invoke(this, e);
}

private void BMRelayOff_btn_Click(object sender, EventArgs e)
{
    WhenBMRelayOffBtnClicked(e);
}
...... //event for each button

Parent Form:

private void viewToolStripMenuItem_Click(object sender, EventArgs e)
{
    ToolStripMenuItem menu = sender as ToolStripMenuItem;

    switch (menu.Name)
    {
               
        case "controlTestToolStripMenuItem":
            if (Application.OpenForms["CtrlTest"] is CtrlTest ctrlTest)
            {
                ctrlTest.Focus();
                return;
            }
            ctrlTest = new CtrlTest();
            ctrlTest.BPRElayOnBtnClicked  = CtrlTest_BPRElayOnBtnClicked;
            ctrlTest.BPRElayOffBtnClicked  = CtrlTest_BPRElayOffBtnClicked;
            ctrlTest.BMRElayOnBtnClicked  = CtrlTest_BMRElayOnBtnClicked;
            ctrlTest.BMRElayOffBtnClicked  = CtrlTest_BMRElayOffBtnClicked;
            ctrlTest.PreRElayOnBtnClicked  = CtrlTest_PreRElayOnBtnClicked;
            ctrlTest.PreRElayOffBtnClicked  = CtrlTest_PreRElayOffBtnClicked;
            ctrlTest.MdiParent = this;
            ctrlTest.Show();
            break;
            .........//Other menus ...
        default:
            break;
private void CtrlTest_BPRElayOnBtnClicked(object sender, EventArgs e)
{
    //Do something here
}
private void CtrlTest_BPRElayOffBtnClicked(object sender, EventArgs e)
{
    //Do something here
}
private void CtrlTest_BMRElayOnBtnClicked(object sender, EventArgs e)
{
    //Do something here
}
private void CtrlTest_BMRElayOffBtnClicked(object sender, EventArgs e)
{
    //Do something here
}

CodePudding user response:

For the on/off, take a look at the following enter image description here

  • Related