Layout-Resize-OnLoad

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,
This is my first experiment with C#, I'm trying to handle the resize
event. According to the documentation I should handle the Layout event
for this. My question is: how do I register this event? The following
code does not work because I never recieve the OnLoad event. What am I
doing wrong?

public class frmMain : System.Windows.Forms.Form
{
protected virtual void OnLoad(EventArgs e)
{
frmMain.ActiveForm.Layout += new
System.Windows.Forms.LayoutEventHandler(this.frmMain_Layout);
}

private void frmMain_Layout(object sender,
System.Windows.Forms.LayoutEventArgs e)
{
splMain.Left = pgTasks.Left + pgTasks.Width;
splMain.Height = this.Height;
}
}

Another question, while I'm here, the line:

frmMain.ActiveForm.Layout += new
System.Windows.Forms.LayoutEventHandler(this.frmMain_Layout);

does not make sense to me. I would think that the line should be:

this.ActiveForm.Layout += new
System.Windows.Forms.LayoutEventHandler(this.frmMain_Layout);

replacing 'frmMain' with 'this' but the 'this' object reference has a
very different set of intellisense attributes, of which ActiveForm is
not one of them.

Thanks
 
John said:
protected virtual void OnLoad(EventArgs e)
{
frmMain.ActiveForm.Layout += new
System.Windows.Forms.LayoutEventHandler(this.frmMain_Layout);
}

Sorry for the, somewhat, quick post, replacing 'virtual' with 'override'
solves the OnLoad problem, but it still doesn't solve the original
question: How do I register the LayoutEventHandler?

Registering in the OnLoad event is incorrect because ActiveForm is null
at that point.

Thanks
 
I take it you are coming from VB6?

frmMain.ActiveForm is a static member, and is not what you should be doing.
Instead
this.Layout + = new
System.Windows.Forms.LayoutEventHandle(this.frmMain_Layout);
should work.
Also, you could simply override OnLayout in your form class, just make sure
you call base.OnLayout.
 
Back
Top