开发者

Add a Load event for Winforms Control just as Form class

开发者 https://www.devze.com 2022-12-13 20:29 出处:网络
is there a way I can get a Load event for System.Windows.Forms.Control just like System.Windows.Forms.Form.Load?

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 wh开发者_开发技巧en the "control" "Loads".

Thanks!


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.


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.


I needed a solution like this for a TabPage within a TabControl.The only thing I came up with was using the paint event handler. I added the event handler for Paint and in the very first line I remove the event handler and then do more initialization code. This only works if you do nothave any custom painting. Alternatively, if you do need to do custom painting you could add a flag to check for each time Paint Executes.

//Paint only runs once    
private void tabPage1_Paint(object sender, PaintEventArgs e)
{
    tabPage1.Paint -= tabPage1_Paint;    
    //Do initialization here    
}

/////////////////////////////////////////////////////////////////////////////////

//Paint always runs

private bool IsFirstPaint = true;

private void tabPage1_Paint(object sender, PaintEventArgs e)    
{    
    if(IsFirstPaint)
    {
        IsFirstPaint = false;    
        //Do initialization here
    }    
    //Do custom painting here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消