Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In windows Application,how we fix a child form at a fixed position in a parent form?
we cant drag that childform to any other position.
Posted

1 solution

You certainly can add a Form as a 'child form' of another Form, and it's draggable "as is," as long as you expose the TitleBar:
private void Form1_Load(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.TopLevel = false;
    //
    f2.Parent = this;
    //
    f2.ControlBox = false;
    f2.MinimizeBox = false;
    f2.MaximizeBox = false;
    f2.ShowInTaskbar = false;
    f2.FormBorderStyle = FormBorderStyle.FixedToolWindow;
    f2.Text = "Form2";
    f2.Show();
}

But, note that in this scenario we are not setting the second Form to be an MDIChild of the first Form which we have defined as an MDIParent Form: so in this case setting the FormStartPosition of the second Form by something like:
f2.FormStartPosition = FormStartPosition.CenterParent; 
Will have no effect.

In general, both using MDI architecture, and using Forms within Forms is not that good an idea, and MDI is now "deprecated," meaning not considered a good, modern, architecture.

Please think about making the second Form 'owned' by the first Form:
private void Form1_Load(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.TopLevel = true;
    //
    f2.Owner = this;
    //
    f2.StartPosition = FormStartPosition.CenterScreen;
    f2.ControlBox = false;
    f2.MinimizeBox = false;
    f2.MaximizeBox = false;
    f2.ShowInTaskbar = false;
    f2.FormBorderStyle = FormBorderStyle.FixedToolWindow;
    f2.SizeGripStyle = SizeGripStyle.Hide;
    f2.Text = "Form2";
    f2.Show();
}
And see how this works for you. You can easily write a 'Move event handler for the second Form to keep it inside the bounds of the first Form: if that's important to your design, or, if you enable Form2 to be resized you can handle the ReSize event and, if you wish, make Form2 appear "within" Form1.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900