如何使用form.Activate()传递参数;而不是form.Show();

时间:2013-10-25 14:04:08

标签: c# winforms c#-4.0 c#-3.0

在总帐管理系统中,当用户双击dgv行时,将使用相关的采购发票打开采购发票表单。如果购买发票已经在MDIPARENT中打开,它将被激活,对于这种情况,我应用支票

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
  string invNo = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
  foreach (Form f in Application.OpenForms)
  {
    if (f.GetType() == typeof(PurchaseForm ))
    {
      f.Visible = true;
      f.Activate();
      .....
    }
   ....
  }
  ....
}

我想传递参数


 PurchaseForm form = new PurchaseForm();
 form.btnNewInvoice.PerformClick();
 form.txtInvoiceNo.Text = invNo;
 form.dataGridView1.Focus();

它没有工作,无法通过PARAMETER,这是什么解决方案???

   return;
        }
   }

   PurchaseForm form = new PurchaseForm();
   form.MdiParent = MainForm.ActiveForm;
   form.Show();

   form.btnNewInvoice.PerformClick();
   form.txtInvoiceNo.Text = invNo;
   form.dataGridView1.Focus();
}

1 个答案:

答案 0 :(得分:0)

不确定,但在我看来,您想要执行此代码:

form.btnNewInvoice.PerformClick();
form.txtInvoiceNo.Text = invNo;
form.dataGridView1.Focus();

表单已经打开。你这么说,“它无法工作,无法通过PARAMETER”。您最有可能尝试在下面的代码段中使用“f”吗?

if (f.GetType() == typeof(PurchaseForm ))
{
  f.Visible = true;
  f.Activate();
  .....
}

如果是这样,只需将“f”投射到PurchaseForm:

if (f.GetType() == typeof(PurchaseForm ))
{
  PurchaseForm pf = (PurchaseForm)f;

  pf.btnNewInvoice.PerformClick();
  pf.txtInvoiceNo.Text = invNo;
  pf.dataGridView1.Focus();

  pf.Visible = true;
  pf.Activate();
  .....
}
编辑:Servy所说的简单例子......

基本上,您需要创建一个公共方法来接收发票号。该方法可以直接访问控件和设置值。像这样:

public partial class PurchaseForm : Form
{

    public void SetInvoiceNumber(string invNo)
    {
        this.Visible = true;
        this.Activate();

        this.txtInvoiceNo.Text = invNo;
        this.btnNewInvoice.PerformClick();
        this.dataGridView1.Focus();
    }

}

然后,在将“f”转换为PurchaseForm之后(如前所示),您只需:

if (f.GetType() == typeof(PurchaseForm ))
{
  PurchaseForm pf = (PurchaseForm)f;
  pf.SetInvoiceNumber(invNo);
}