求各位帮帮忙。。。windowsform
我现在有两个窗口;一个作为主界面,另一个是一对话框的形式出现;
我的目的:想单击对话框那个界面的button按钮时,在主界面中显示相应的信息;
问题是:每点击一次,就会出现一个一样的主界面;请问怎么解决????
代码:form1中
private void btnEnterInsetStep_Click(object sender, EventArgs e)
{
this.Close();
Form1 f1 = new Form1();
f1.Show();
int index = f1.dgvRecipeEdit.Columns.Add("C", "");
string headerText = "step" + index.ToString();
f1.dgvRecipeEdit.Columns[index].HeaderText = headerText;
f1.dgvRecipeEdit.Columns[index].SortMode = DataGridViewColumnSortMode.NotSortable;
f1.dgvRecipeEdit.Columns[index].DefaultCellStyle.Font = new Font("宋体", 11F);
f1.dgvRecipeEdit.Columns[index].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
f1.dgvRecipeEdit.Columns[index].Width = 130;
}
form2中:
private void btnInsertRecipeStep_Click(object sender, EventArgs e)
{
FormInsertStep fis = new FormInsertStep();
fis.ShowDialog();
------解决方案--------------------传值方法参考http://www.cnblogs.com/cosoft/archive/2011/08/08/2130659.html
------解决方案--------------------你用下单例模式应该可以解决了
------解决方案--------------------代码:form1中
private void btnEnterInsetStep_Click(object sender, EventArgs e)
{
this.Close();
Form1 f1 = new Form1();
f1.Show();
int index = f1.dgvRecipeEdit.Columns.Add("C", "");
string headerText = "step" + index.ToString();
f1.dgvRecipeEdit.Columns[index].HeaderText = headerText;
f1.dgvRecipeEdit.Columns[index].SortMode = DataGridViewColumnSortMode.NotSortable;
f1.dgvRecipeEdit.Columns[index].DefaultCellStyle.Font = new Font("宋体", 11F);
f1.dgvRecipeEdit.Columns[index].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
f1.dgvRecipeEdit.Columns[index].Width = 130;
}
form2中:
private void btnInsertRecipeStep_Click(object sender, EventArgs e)
{
FormInsertStep fis = new FormInsertStep();
fis.ShowDialog();
在form1,中show form1,那就只能出现form1窗体了
------解决方案--------------------C# code
private void btnEnterInsetStep_Click(object sender, EventArgs e)
{
this.Close();
Form1 f1 =Form1.GetInstance();
f1.Show();
......................
}
Form1 窗体中
static Form1 form = null;
public static Form1 GetInstance()
{
if (form == null || form.IsDisposed)
{
form = new Form1 ();
}
return form;
}