Reputation: 13820
This question is not a duplicate of Win Form in front of parent form
because the author of that one didn't seem to mind if the parent form was hidden, and the answers suggesting ShowDialog() hide the parent. I don't want the parent form hidden.
I have the following code in form_load
private void Form1_Load(object sender, EventArgs e)
{
var childform = new Form();
childform.Show();
childform.BringToFront();
childform.Activate();
}
Yet it won't show the child in front of the parent. It shows this, as you can see, it's showing the parent form in front and the child behind, rather than the other way around which I want.
I've read of using childform.ShowDialog()
but I don't want that because that would make the parent invisible while the child form shows. I want both visible.
I've read that BringToFront won't work 'cos it's for controls not forms, and to use Activate, but as you can see i've tried Activate and it isn't working either.
Upvotes: 0
Views: 3986
Reputation: 56727
Try your code in the form's Shown
event. That way the second form is shown after the main form and should thus become active.
Upvotes: 2
Reputation: 3134
Simply move the action to Shown event. You can remove both BringToFront and Activiate.
private void Form1_Shown(object sender, EventArgs e)
{
var childform = new Form();
childform.Show();
}
It is because when Load event is called, the form is yet shown.
As a result, your sequence becomes undesirably:
Therefore, your parent is shown later and thus anyway is upper.
By using Shown event, the issue solved.
Upvotes: 2
Reputation: 802
Form.Show() method has an overload that taken in an argument that specifies the parent of the form. Th child form is always shown above the parent form.
childForm.Show(this); //this is the parent form
Upvotes: 3
Reputation: 25601
Just include this
as a parameter to the Show
method.
var childform = new Form();
childform.Show(this);
Upvotes: 1
Reputation: 1844
try this after InitializeComponent():
this.Activated += (s, e) => ((Form)s).SendToBack();
where "this" is the parent form, so I'm casting "s" to Form
Upvotes: 0