471,319 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Call an event handler on a parent form from child form

I have a parent form with a menu button that has a handler.
I want to call this same handler when a button on a child form is
clicked.

I have this simplified code in the main form's Load handler.

public class frmMain
{

private System.Windows.Forms.Button btnClickMe;

private void frmMain_Load(object sender, System.EventArgs e)
{
// Following instance becomes visible when a menu
// button is clicked.
ChildForm frmMyChild = new ChildForm ( );

// ...
}

// ...
// (Windows Form Designer generated code)
// ...
private void btnClickMe_Click (object sender, System.EventArgs e)
{
// do some complicated stuff here
Console.WriteLine ("btnClickMe was clicked."

}
On the child form, I have another button which we'll call btnClickMeToo
with the usual click handler,

void btnClickMeToo_Click(object sender, System.EventArgs e)
{

}

I want this main form to "listen" to btnClickMeToo as well and
execute the code in its btnClickMe_Click handler when
it is clicked. The console message makes clear that
the event from the child form's button was handled.

I know I'm overlooking something fundamental here.
Anyone know how to do this?

Jan 20 '06 #1
3 14531

jimco...@pacbell.net wrote:
I have a parent form with a menu button that has a handler.
I want to call this same handler when a button on a child form is
clicked.


A correction here. The menu handler makes the
child form visible, and creates a new instance of it
it has been disposed.

The Click Me button the main form has handler
that among other things writes a one-liner to the
console. It is *this* handler I wish to call when a
button on the child form is clicked.

Jan 20 '06 #2
Try this...

frmChild.btnClickMeToo.Click += new
EventHandler(frmMain.btnClickMe_Click);

This will make it so that when the child button is clicked, the main
form will also handle it.

Jan 20 '06 #3
Coder wrote:
Try this...

frmChild.btnClickMeToo.Click += new
EventHandler(frmMain.btnClickMe_Click);

This will make it so that when the child button is clicked, the main
form will also handle it.


Thanks, Coder! Yes, that does do it. And if
the child form has been disposed, a new event handler
entry is necessary, too, I presume.

You know, the simple question I should have asked
(I mention it here so that someone will find it in a Google
search) is:

"How do I make one event handler handle
multiple events where the events are buttons
clicked on different forms?"
Jim

Jan 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris Bruce | last post: by
8 posts views Thread by Waleed Seada | last post: by
2 posts views Thread by Gary Brown | last post: by
1 post views Thread by Nayan | last post: by
19 posts views Thread by zacks | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.