如何将一个表单的实例传递给另一个表单
问题描述:
我有一个名为form1
的表单,其中包含在运行时创建的控件.
I have a form called form1
with controls which are created during run-time.
当我按下窗体上的一个按钮时,另一个名为combat
的窗体加载,并且form1
被隐藏,因此只有1个窗体(combat
)可见.
When I press a button on the Form another Form loads called combat
and form1
is hidden so that only 1 form (combat
) is visible.
当我按下combat
上的按钮时,我希望显示我的form1
表格.但是我无法访问它.
When I press a button on combat
I want my form1
form the be shown. However I can't access it.
这是我尝试过的:
private void combatBtn_Click(object sender, EventArgs e)
{
Form combat = new Combat(this);
this.Hide();
combat.Show();
}
public partial class Combat : Form
{
public Combat(Form form)
{
InitializeComponent();
form.Show();
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}
答
public partial class Combat : Form
{
private form1 form; // Or whatever class you form1 is supposed to be
public Combat(Form form)
{
InitializeComponent();
this.form = form;
}
private void button1_Click(object sender, EventArgs e)
{
form.Show();
}
}