Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What events are triggered when ShowDialog(ParentForm) is called in C#

Tags:

c#

winforms

Simple question. I have a MainForm and a settingsForm. The settings form is initialized once and then shown every time the user clicks a button. I need it to do something when this button is clicked.

m_inputSettings.ShowDialog(this); //edit settings selected, so show that form

This is the MainForm calling the settings form, which it does fine. But I need the SettingsForm to do something every time this happens. Presently, I cant figure out if this call actually triggers any events I can set handlers for. Does it trigger an event? If not, is there another way I can tell my SettingsForm to do something every time this call happens?

Note: Any code in the mainform after that line doesn't get executed until the SettingsForm returns, but that is intentional.

Thanks.

Edit: One of the things I want my form to do it select a specific control when this happens, but it seems that that is impossible until after the form is done loading everything.

like image 766
Akron Avatar asked Oct 19 '11 18:10

Akron


3 Answers

FormShown event - raised only once when form is displayed first time. OnPaint / OnActivate - every time form is activated, but these events raised even when you switch with other application, which probably you don't want to do. If you are changing form visbility, then you can use OnVisibleChanged If you are minimizing the form, you can use OnSizeChanged / OnLocationChanged event.

If none suits you, make a public property and set false when form is closed / hidded, and set true before showing it. OnActivate, use this property to do your task.

like image 91
hungryMind Avatar answered Nov 15 '22 18:11

hungryMind


Override OnShown() event in your form this will raise only once after the form is opened The disadvantage of OnVisibleChanged is it will also get raised when the form is closed. On Paint , On Activate and OnEnter will raise before form is shown to the user.

like image 21
Sooraj kumar Avatar answered Nov 15 '22 18:11

Sooraj kumar


You can override the OnVisibleChanged method in your settings form. Make sure to call base.OnVisibleChanged though as to not screw up any potential observers of the event (and anything else the base class may do inside of that method.)

like image 26
Ed S. Avatar answered Nov 15 '22 19:11

Ed S.