Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Load event for Winforms Control just as Form class

is there a way I can get a Load event for System.Windows.Forms.Control just like System.Windows.Forms.Form.Load?

I want to run some initialize code before the control first shown.

Also, it would be nice to be able to do the same for System.Windows.Forms.ToolStripStatusLabel which is not actually a Control, but works like one.

Ideally, I can do this: control.OnLoad(() => { dosomething here; });

in which OnLoad is a extension method that would run the argument Action when the "control" "Loads".

Thanks!

like image 850
deerchao Avatar asked Dec 10 '09 01:12

deerchao


People also ask

How do I create a form load event?

A third way using the designer (probably the quickest) - when you create a new form, double click on the middle of it on it in design mode. It'll create a Form load event for you, hook it in, and take you to the event handler code. Then you can just add your two lines and you're good to go!

How do you call a load event in C#?

You have to call Form_load. Form_Load(this, null);

How do I load a Windows Form?

Loader is an operating system that is used for loading Programs and Libraries. It is one of the essential stages in the process of starting a program. Click New >> Project >> Visual C# >> Windows >> Windows Forms Application. Enter your Project name and click OK.


2 Answers

Form.Load event is called by the OnLoad method which is called from the OnCreateControl method which belongs to the Control class. So for the form the calling sequence would be following:

OnCreateControl start
  OnLoad start
      Form Load event call
  OnLoad finish
OnCreateControl finish

I guess you can override OnCreateControl for your component and add your optimization code there.

Hope this helps, Regards.

like image 158
serge_gubenko Avatar answered Nov 15 '22 06:11

serge_gubenko


For a control you can override either OnControlCreated or OnHandleCreated. The latter one can fire multiple times if it is necessary to recreate the control window. Be sure to use it if your code affects the window itself. In other words, if you do anything that requires the Handle property.

Few suitable choices for a ToolStripItem derived control. I'd recommend overriding SetVisibleCore() or OnAvailableChanged() or the AvailableChanged event. They run when the Visible property of the ToolStripItem changes. Beware that it may fire multiple times, keep a bool field that tracks that your initialization code has already run.

Last but not least, be sure to only do any of this if your code actually requires the control to be created. The vast majority of init code can go in the constructor. You only need a Load event if your code depends on the actual Location and Size of the control. Which might be different from the designer value if the form rescales itself due to a different system font or video DPI setting on the target machine.

like image 34
Hans Passant Avatar answered Nov 15 '22 06:11

Hans Passant