In .Net WinForms I have two forms. Form1 is open and active. How can I open new form (Form2) that:
- Form1 must have focus
- Form2 is opened under Form1 (background Form1)
First point is simple see stackoverflow... But I don't know how to show Form2 under Form1. Thanks.
EDIT
public partial class Form1 : Form
{
Form2 frm;
public Form1()
{
InitializeComponent();
frm = new Form2();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.AddOwnedForm(frm);
frm.Show();
frm.SendToBack();
this.BringToFront();
}
private void button1_Click(object sender, EventArgs e)
{
frm.Show();
frm.SendToBack();
this.BringToFront();
}
}
Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Focus();
}
protected override bool ShowWithoutActivation
{
get
{
return true;
}
}
protected override CreateParams CreateParams
{
get
{
//make sure Top Most property on form is set to false
//otherwise this doesn't work
int WS_EX_TOPMOST = 0x00000008;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TOPMOST;
return cp;
}
}