?
Solved

How to detect event in user control from main Win Forms app?

Posted on 2005-05-09
9
Medium Priority
?
226 Views
Last Modified: 2010-04-16
Hello experts!

I have a created a user control. It's a panel with an exit button.

In my main app I would like to have access to the ExitButton_Click event that is triggered in my custom control.

How to do this?


Thanks a lot for caring. This should not be too difficult, but it is urgent...
0
Comment
Question by:i-Thomas
  • 4
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13960173
So, you have a User Control, with an Exit button, but you want that event to pass up to the main application, so that it can do the exiting.  Am I correct in that assessment?

Bob
0
 

Author Comment

by:i-Thomas
ID: 13960271
Hi Bob!

Wow! You are faster than Lucky Luke ;-)  A big praise to ee!


The event of the button on the user control will send event to the main app and the main app will just close this instance of the UserControl and refresh array etc.

Best regards,

Thomas
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 13960322
I called the event ClosePanel, instead of ExitButton, since it makes more sense to me with that name.

    // Define the event.
    public event EventHandler ClosePanel;

    private void buttonClose_Click(object sender, System.EventArgs e)
    {
   
      // Raise the event back to the call with the necessary arguments.
      this.ClosePanel(this, new System.EventArgs());
    }

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:i-Thomas
ID: 13960468
Super! Works!

For my learning:

You declare: public event... this part is clear to me. What does the additional "EventHandler" exactly do?


this.ClosePanel is clear to me, I think, it simply callse the event ClosePanel, right?

What I do not understand fully is the terms in the brackets: (this, new System.EventArgs());

I would highly appreciate, if you could give me some explanations for my understanding!


Thanks a lot!

Thomas
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13960490
There is a delegate defined in C# that has this signature:

public delegate void EventHandler(object sender, EventArgs e);

When you want to raise the event, then you just need to pass the correct arguments.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13960503
In the main form, you just need to subscribe to the ClosePanel event, and use the sender object passed to know which panel to close.

Bob
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13960524
if i might finetune TheLearnedOne's code and offer an optmised way of writing events (only applicable to C#)

private static readonly object ClosePanelEventHandler = new object();
      
public event EventHandler ClosePanel
{
      add { this.Events.AddHandler(ClosePanelEventHandler,value); }
      remove{ this.Events.RemoveHandler(ClosePanelEventHandler,value); }
}

private void OnClosePanel()
{
      EventHandler handler = this.Events[ClosePanelEventHandler] as EventHandler;
      if (handler != null)
            handler(this,EventArgs.Empty);
}


private void ExitButton_Click(object sender,EventArgs e)
{
      OnClosePanel();
}
0
 

Author Comment

by:i-Thomas
ID: 13970529
Hello b1xml2!

What is the difference / what are the advantages of your code vs. the code of TheLearnedOne?

Thanks a lot for your efforts!


BTW:

I would be interested in some explaining words about "object sender" ...
0
 

Author Comment

by:i-Thomas
ID: 13976422
I have opened a new thread for "the rest" of the question. If you are interested, please follow this link:

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21420186.html

I would be really interested what is the difference in both of your methods for implementing the event in the user control!

Regards,

i-Thomas
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question