Load as an event in Windows Forms for any control, such as a text box, checkbox?

On Windows Forms, when UserControl or Form first appears, the Load event is fired.

http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.load.aspx

Is there such an event for controls such as Checkbox, TextBox, Label?

+4
source share
1 answer

No. You can use the HandleCreated event, it fires when you create your own window for the control. The first event you can rely on is fired after the class constructor is launched. It starts when the parent adds the control to its Controls collection, and the control becomes visible.

Beware, however, that this event may fire more than once. Controls can be recreated when certain properties are reassigned, a type that requires the nativeWindowExEx () function to be called with new style flags. Therefore, at least you will need to wear a bool flag that tracks this.

Also note that adjusting the properties of the control after creating your own window is rather inefficient. All Winforms controls were designed so that the properties were set before creating your own window. Regardless of which code you generate almost certainly, you should use the class constructor instead. Or the derivative control itself. Or in the parent code, as for InitializeComponent () for the form or user control.

The same goes for the existing Load event. It is usually overused due to the legacy of VB6, where the Load event was very important. However, in Winforms, this is only required for code that depends on the final location and size of the control or form. What may differ from the structural properties due to the scaling of the form. Any other code belongs to the constructor.

+5
source

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


All Articles