Link to home
Start Free TrialLog in
Avatar of i-Thomas
i-Thomas

asked on

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

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...
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of i-Thomas
i-Thomas

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
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();
}
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" ...
I have opened a new thread for "the rest" of the question. If you are interested, please follow this link:

https://www.experts-exchange.com/questions/21420186/object-sender-how-to-get-the-id-name-index-of-a-user-control-in-array-list.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