-1

I'd like to do the same as this:

Show a child form in the centre of Parent form in C#

But at any moment, not only at start.

Reason is because I show and hide a form. So this only work on first show because after that it is just hidden, so I don't "Start" it again.

Community
  • 1
  • 1
Cher
  • 2,789
  • 10
  • 37
  • 64

2 Answers2

0

C# code to show subForm in center of this (this form is papa of subForm)

frmSub fs=new frmSub();
fs.StartPosition = FormStartPosition.CenterParent;
fs.Parent = this;
fs.Show();
0

Try this and keep the FormBorderStyle of the parent form to FixedSingle so that it's not resizable.

Say, suppose you do this on a button1_Click() event and just declare Form f2 = new Form(); above the event. And set the start position of the child form to CenterScreen like this:

f2.StartPosition = FormStartPosition.CenterScreen;

Using a checkBox you can show/hide the child form easily.

Form f2 = new Form();
private void button1_Click(object sender, EventArgs e)
{
    this.FormBorderStyle = FormBorderStyle.FixedSingle;
    this.IsMdiContainer = true;            
    //Form f2 = new Form(); To prevent creating a new form everytime.
    f2.MdiParent = this;
    f2.StartPosition = FormStartPosition.CenterScreen;
    if (checkBox1.Checked)
        f2.Hide();
    else
        f2.Show();
}
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32