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:
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
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.
frm2.Show();
just opens the second form and will allow the user to interact with the first form. If you use
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.