473,387 Members | 1,502 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,387 software developers and data experts.

How to propagate an Event up to the MainForm?

Hi,

I'm developing a C# Windows Form app, which contains 2 UserControls
in the MainForm. One of the UserControls contains a number of layers
as shown in the following hierarcy:

MainForm
UserControl1 UserControl2
SplitContainer
SplitContainer.Panel1
UserControl3
Button1, Button2, etc

Problem:
When the user clicks a button on UserControl3, I need to pass that
information to UserControl1 on the MainForm. I know how to add an
event for a Button1_Click at the UserControl3 level, but I can't figure
out how to get this event to the MainForm. I've tried many things,
including adding a public ProcessButton() method to the MainForm
and then trying to call it from UserControl3 with 'this.ParentForm.???'.
All to no avail.

Question:
When the user clicks on Button1, how can I propagate this event up
to the MainForm so it can tell UserControl1 to do something?

Thanks for any suggestions,
RickL

Jul 21 '06 #1
1 5848
Hi,

If you make a public getter property for each control on each layer and
finally a public event on the last layer that gets fired from the button
click eventhandler. You can then add an eventhandler to that event by
accessing the right properties in the other layers.

The following code demonstrates this principle.

class Panel1 {
public Panel2 SubPanel {
get { return _panel2; }
}
}

....

class Panel2 {
public event EventHandler Event1;

private void Button1_Click(object sender,EventArgs e) {
if(Event1 != null)
Event1(this,EventArgs.Empty);
}
}
}

....

Class MainForm {
public MainForm() {
_panel1.Panel2.Event1 += new EventHandler(buttonClicked);
}

private void buttonClicked(object sender,EventArgs e) {
//TODO: Do something
}
}

Hope this helps.

--
Best regards,

W.Meints
"RickL" wrote:
Hi,

I'm developing a C# Windows Form app, which contains 2 UserControls
in the MainForm. One of the UserControls contains a number of layers
as shown in the following hierarcy:

MainForm
UserControl1 UserControl2
SplitContainer
SplitContainer.Panel1
UserControl3
Button1, Button2, etc

Problem:
When the user clicks a button on UserControl3, I need to pass that
information to UserControl1 on the MainForm. I know how to add an
event for a Button1_Click at the UserControl3 level, but I can't figure
out how to get this event to the MainForm. I've tried many things,
including adding a public ProcessButton() method to the MainForm
and then trying to call it from UserControl3 with 'this.ParentForm.???'.
All to no avail.

Question:
When the user clicks on Button1, how can I propagate this event up
to the MainForm so it can tell UserControl1 to do something?

Thanks for any suggestions,
RickL

Jul 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Susan Bricker | last post by:
Greetings. I have a Mainform with a subform. The Mainform has single record format and subform has continuous form format. When the Mainform opens, I force allowadditions and allowedits to FALSE...
3
by: DEK | last post by:
Stepping through my code the click eventhandler code executes through a few times before it finally stops. Even though I only click the button once, is there a reason for this, I have a few if...
0
by: Miquel | last post by:
Hi all. I felt frustrated when developing an 'UserControl' derivated from textBox, because sequence event (and Validate event) seems to fail. I Always thought my code was wrong. But after...
1
by: Trevor | last post by:
I need to display a confirmation message box when the user tries to exit the main form of my application. I placed code in the MainForm_Closing event to handle this and it works as expected. Now I...
7
by: ChrisM | last post by:
I posted this last week, so apologies for re-posting but I'm still looking for a sensible answer, and I'm hoping somone new might be able to cast some light... Basically, I have a fairly...
5
by: Ole | last post by:
The Application.Exit (called from a worker thread) method doesn't cause the OnClosing event handler to be run - is that a default behaviour??? If so - what should I do to properly close all threads...
1
by: Andrus | last post by:
T reproduce, run code. Observed: Vista dialog Myapp stops working Expected: Messagebox. If RaiseTypeLoadException() is removed from Main(), Messagebox occurs. ThreadException event does...
13
by: billelev | last post by:
I have an Access form (MainForm) that has a SubForm. I would like to know if a MainForm event exists that detects a SubForm record change. I can use the "On Current" event to detect when the...
13
by: FelixS | last post by:
I have developed a way to enter the data from long medical document, using structure: Mainform (subform1,... subfromN). This is reflecting the logical model of data: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.