Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have 2 forms.form1 and form2,in form1 there was a menuStrip and a button..when i click the button the form2 appear but below the form1 button.
my code for button1_click is here:

C#
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.MdiParent = this;
           
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.WindowState = FormWindowState.Maximized;
          
            frm.Show();
        }
Posted
Updated 20-Dec-14 5:28am
v2
Comments
Richard MacCutchan 20-Dec-14 11:11am    
That looks like C# code to me. And what exactly is the problem?
Sergey Alexandrovich Kryukov 20-Dec-14 19:30pm    
Why doing such things? I would not advise to use MDI at all...
—SA
rajesh mahesh bhai 20-Dec-14 21:22pm    
my problem is that when i open 2nd form it appears behind the mdiparent form control
rajesh mahesh bhai 20-Dec-14 21:24pm    
if i`m not use mdi than suggest me any other method??

First of all, your code is in C# and not VB.Net. So the code solution below is in C# as well.

I guess the big question is, what are you really trying to do with the second form? Does the second form display a dialog for the user to answer questions? Or display data? Do you really need to use MDI? It's not clear from your question what you are trying to do. So, I will attempt to cover as many scenarios as possible.

Look at how Word, Excel and other MDI applications work. All the controls on the main form, or MDI parent is located either in the menu strip or in a tool strip. So the middle of the form, or MDI container, does not contain any controls at all.

The problem you are facing is because that is how MDI parent-child forms work. And that is why your second form appears behind your button. There is nothing you can do about that. Changing the Z-order or playing around with the TopMost property on the second form will not change anything.

Now, if you just want Form2 to appear on top of the whole application and not necessarily bound by the MDI parent, then the following code will work:

C#
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Owner = this;

            frm2.StartPosition = FormStartPosition.CenterParent;

            frm2.TopMost = true;

            frm2.Show();
        }


There are two things to think about:

1) When positioning the second form. Think about where you'd like the form to be located. The code above will position the form in the center of the parent or calling form. The line
C#
frm2.Owner = this;

is important to establish who the owner is so that Form2 will know where to position itself.

2) Do you want the second form to be modal or not.
C#
frm2.Show();

just opens the second form and will allow the user to interact with the first form. If you use
C#
frm2.ShowDialog();

instead, then the second form will become modal and the user will need to close it before they can interact with the first form.

If you still insist on using a MDI form, then you will need to move your button to a tool strip so that it is outside of the MDI container. Then your code will work.
 
Share this answer
 
v2
Why are you setting the StartPosition of the form if you're maximizing it? This renders the StartPosition meaningless.

The next question is why are you even using MDI??
 
Share this answer
 

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