Add a Load event for Winforms Control just as Form class
Asked Answered
R

3

6

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!

Redpoll answered 10/12, 2009 at 1:52 Comment(0)
R
8

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.

Rale answered 10/12, 2009 at 4:10 Comment(4)
Thanks for your tip. Followed your advice I found CreateControl event of Control class. Is there something like this in ToolStripItem class?Redpoll
Are you trying to create new ToolStripItem descendant? I guess you can put the initialization code into its contructorRale
No I can't because the code has something to do with its parent: the ToolStrip control. in the constructor, they'd be null.Redpoll
you can use OnOwnerChanged(EventArgs e) method to track the Owner property change; and OnParentChanged(ToolStrip oldParent, ToolStrip newParent) to handle parent control change. Both should be called when ToolStripItem get added to the StatusStrip control items collectionRale
H
5

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.

Hushhush answered 10/12, 2009 at 18:6 Comment(1)
Thanks. I tested it, but not working like I expected. The VisibleChanged/AvailableChanged event fires when their GetCurrentParent() is null.Redpoll
D
0

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
}
Donets answered 11/1, 2012 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.