Reputation: 325
I am creating one c# project. In this project I have one mdi form and many child forms.
All the child forms contains one panel named as panel1.
Now when child form opens i use the following code in all child form
all child forms' load event contains the following line.
this.WindowState = FormWindowState.Maximized;
and all child forms' resize event contains the following line.
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;
so my question is if possible that the above code i write only once so i donot write this code in all the child forms load and resize event.
Upvotes: 1
Views: 185
Reputation: 444
I think the best way is to do it before opening the child form!
public class ChildForm : Form
{
public void doTheSizing()
{
// make it maximize...
// your sizing of pannel...
// etc...
}
}
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm childForm01 = new ChildForm();
childForm01.doTheSizing();
// now show the child window using Show() or ShowDialog().
childForm01.ShowDialog();
}
}
Upvotes: 0
Reputation: 4911
Yes it is possible. You have to pull up
common functionality into base class event handlers, and then invoke them from child ones:
public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
this.Load += new System.EventHandler(this.FormLoad);
this.Resize += new System.EventHandler(this.FormResize);
}
protected virtual void FormLoad(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
protected virtual void FormResize(object sender, EventArgs e)
{
panel1.Left = (this.ClientSize.Width - panel1.Width) / 2;
panel1.Top = (this.ClientSize.Height - panel1.Height) / 2;
}
...
}
public class DerivedForm : BaseForm
{
protected override void FormLoad(object sender, EventArgs e)
{
base.FormLoad(sender, e);
// specific code goes here
}
protected override void FormResize(object sender, EventArgs e)
{
base.FormResize(sender, e);
// specific code goes here
}
...
}
Upvotes: 1
Reputation: 157118
Create a base class and derive every child class from it.
Like this:
public class FormBase : Form
{
public FormBase()
{
this.WindowState = FormWindowState.Maximized;
// put more generic code here
}
... // or here
}
public class YourMdiChild : FormBase
{
... // all code specific for that child
}
Upvotes: 0