5
Answers

Positioning forms

Photo of garuka

garuka

15y
4k
1
Hi All,
I want to do two things.
1. I have a MDI and when a specific form  load it should position in the middle of the MDI. For example login window should load in the middle of MDI.

2. I have a MDI and I can load some windows inside that. And I some of those windows load other windows too. I want to position them in the middle of window which is used to call them

Please post some sample code on how to do this.

Answers (5)

0
Photo of Kirtan Patel
NA 35k 2.8m 15y
sorry Dear there are some Correction in code


try below code now it will work


private void button1_Click(object sender, EventArgs e)

        {

            Form2 f2 = new Form2();

            f2.MdiParent= this;

            f2.StartPosition = FormStartPosition.CenterScreen;

            f2.Show();

        }


Accepted
0
Photo of Kirtan Patel
NA 35k 2.8m 15y
Hi


Make MDIFroms
KeyPreview Property to True

then  here Is Code For key press Event of MDI Form Now it will Open Form2 When i Press ' A' From keyboard ( Shift+A) key

  private void Form1_KeyPress(object sender, KeyPressEventArgs e)

        {


            if (e.KeyChar == (char)(65))

            {

                Form2 f2 = new Form2();

                f2.MdiParent = this;

                f2.StartPosition = FormStartPosition.CenterScreen;

                f2.Show();

            }

        }


0
Photo of garuka
NA 26 0 15y

Hi Kirtan,
Sweet. It's working. I think issue No1 is solved now.

But any idea about issue two? Do I have to handle this in from level instead of MDI level?
0
Photo of garuka
NA 26 0 15y

Hi Kirtan,
There is a problem in that approach which I already tried. The problem is related to part 1. My Login form loads when the MDI is loading. So when I add that code segment I get the error "Top-level control cannot be added to a control."

About the second part..the problem that child window is loaded from MDI window. the MDI should listern to key board and load the window in the middle of currently focused form inside MDI.

So I'm sorry I don't think that suggestion will work.
0
Photo of Kirtan Patel
NA 35k 2.8m 15y
Hi Garuka ,

here is solution for your Problem



you can Do it By Both Way By Code  and Setting Property

 Just Setting StartPositon Propety of Form  to "CenterScreen" of Which you trying to show in MDI form


or By Coding You can do it Like below Code


private void button1_Click(object sender, EventArgs e)

        {

            Form2 f2 = new Form2();

            f2.Parent = this;

            f2.StartPosition = FormStartPosition.CenterScreen;

            f2.Show();

        }