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

Simple question. I have MainForm and settingsForm. The settings form is initialized once, and then displayed each time the user clicks a button. I need to do something when this button is pressed.

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

This is MainForm, which invokes the settings form, and it’s great. But I need SettingsForm to do something every time this happens. At present, I cannot understand if this call actually causes any events for which I can set handlers. Does this event trigger an event? If not, is there any other way that I can say that my SettingsForm does something every time this call is made?

Note. Any code in the main form after this line will not be executed until the SettingsForm function returns, but this is intentional.

Thanks.

Edit: One of the things in which I want my form to do this selects a specific control when this happens, but it seems like this is not possible until the form is loaded.

+6
source share
3 answers

You can override the OnVisibleChanged method in the settings form. Be sure to call base.OnVisibleChanged so as not to clutter up potential event observers (and anything else that the base class can do inside this method.)

+6
source

FormShown event - is created only once when the form is displayed for the first time. OnPaint / OnActivate - each time form is activated, but these events occur even when switching from another application, which, probably, you do not want to do. If you change the form, you can use OnVisibleChanged If you minimize the form, you can use the OnSizeChanged / OnLocationChanged event.

If it doesn't suit you, create a public property and set to false when the form is closed / locked and set to true before showing it. OnActivate , use this property to accomplish your task.

+3
source

It is possible to use the VisibleChanged event.

+1
source

Source: https://habr.com/ru/post/899638/


All Articles