在.Net WinForms中我有两种形式。 Form1是开放且活跃的。如何打开新表格(Form2):
第一点很简单,请参阅stackoverflow... 但我不知道如何在Form1下显示Form2。 感谢。
修改
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();
}
}
窗体2
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;
}
}
答案 0 :(得分:2)
我认为第二点你需要做的是:
var form2 = new Form2();
form2.MdiParent = form1; //form1 is your parent form; use this operator in case you are creating this form from out base form
form2.WindowState = FormWindowState.Normal;
form2.Show();
有关详细信息,请查看此内容:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdiparent.aspx
修改
请参阅下面的代码,其中给出了在Form1顶部打开frm的外观。
public partial class Form1 : Form
{
Form2 frm;
public Form1()
{
InitializeComponent();
frm = new Form2();
}
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
textBox1.Focus();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!frm.Visible)
{
this.AddOwnedForm(frm);
frm.Show();
frm.SendToBack();
this.BringToFront();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.AddOwnedForm(frm);
frm.Show();
frm.SendToBack();
this.BringToFront();
}
}
答案 1 :(得分:0)
您只需实例化Form2并显示它,例如:
var form2 = new Form2();
form2.Visible = true;
然后将Form1设置为活动表单(假设这是在Form1的代码中完成的 - 因此this
):
this.BringToFront();
答案 2 :(得分:0)
我猜想你想要一个对话
var form2 = new Form2();
form2.Visible = true;
this.ShowDialog();