473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 15168

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: BlueMonkMN | last post by:
I've been trying to think of the right way to design relationships between objects with different desired lifetimes that raise events. If an event source is a relatively permanent object and the...
3
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a...
1
by: Chris Bruce | last post by:
In my application I need a way to distiguish between the following events: 1. When a user closes an MDI child window. 2. When the user closes the MDI parent window which subsequently closes the...
8
by: Waleed Seada | last post by:
Hi all What need to be done to get the MDI parent call his MDI children Events, methods Thanks in advance /WS
2
by: Gary Brown | last post by:
Hi, I dynamically connect a MDI parent's toolstrip button to a child event handler by calling the following member at the child's Activated and Deactivate events. It looks simple enough but...
1
by: Nayan | last post by:
I am creating new child forms in MDI app. I want different (behaviour) event handlers of paint event to be created for each child form. Each child has different handler. Any idea if it can be...
7
by: Sheikko | last post by:
Hi all, i have an application and i wan to call another program into it, like notepad, so when i minimize the notepad, or maximize, it must rest in my application. thank you
6
by: Daz | last post by:
Hello everyone, I would like to open a child window from the parent, and add an onload event listener to the child window which will tell the parent when the document has loaded. As far as I...
19
by: zacks | last post by:
I have a .NET 2.0 MDI application where the child form has a Tab Control. Each of the Tab in the Tab Control has a Validating event to handle what it should do when the user changes tabs. But...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.