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...
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...
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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
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(Clo sePanelEve ntHandler, value); }
remove{ this.Events.RemoveHandler( ClosePanel EventHandl er,value); }
}
private void OnClosePanel()
{
EventHandler handler = this.Events[ClosePanelEven tHandler] as EventHandler;
if (handler != null)
handler(this,EventArgs.Emp ty);
}
private void ExitButton_Click(object sender,EventArgs e)
{
OnClosePanel();
}
private static readonly object ClosePanelEventHandler = new object();
public event EventHandler ClosePanel
{
add { this.Events.AddHandler(Clo
remove{ this.Events.RemoveHandler(
}
private void OnClosePanel()
{
EventHandler handler = this.Events[ClosePanelEven
if (handler != null)
handler(this,EventArgs.Emp
}
private void ExitButton_Click(object sender,EventArgs e)
{
OnClosePanel();
}
ASKER
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" ...
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" ...
ASKER
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
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
Bob