Hi Guys, Can Anybody tell me where i am wrong. The event can't be fired on clicking button6

private void button6_Click(object sender, EventArgs e)
        {
            this .button7.Click +=new EventHandler(button7_Click);
        }
        private void button7_Click(object sender,EventArgs e)
        {
            MessageBox.Show("Button7 Clicked when ur pressing button6");
        }

Recommended Answers

All 5 Replies

Not sure what the problem is. Clicking on button6 adds a click handler for button7. Clicking on button7 should fire the handler.

I don't know what you are trying to accomplish.

If you want to call the button7_Click() method when button6 is clicked then you can do it like this:

private void button6_Click(object sender, EventArgs e)
 {
     button7_Click(null, null);
 }

What farooqaaa said is correct of course. But, if you want to call the Click event of a control, without knowing which method is handling the event (e.g. event handler was changed), you have to inherit the control and expose the OnClick() method. For example:

public class MyButton : Button
{
    public void ClickMe()
    {
        OnClick(null);
    }
}

Events can only be invoked by the declaring class, even inherited classes cannot.

Why would you do an even for some button inside other button? This makes no sence.
Events for all the button can be generated on the load time. Or when you create some button in the run time (afterwards).

If you still inisit you can do it:

private void button6_Click(object sender, EventArgs e)
        {
            //just make sure your button initialized on form!!
            this.button7.Click += new EventHandler(button7_Click);
            EvenArgs ee = new EventArgs();
            button7_Click(this.button7, ee); //this will fire button event!
        }
        private void button7_Click(object sender,EventArgs e)
        {
            MessageBox.Show("Button7 Clicked when ur pressing button6");
        }

Thanks all for replying such wonderful guidance. Mitja Bonca this is working fine for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.