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();

它不工作并且无法通过参数,这是什么解决方案???

   return;
        }
   }

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

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

1 回答 1

0

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

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

当表单已经打开时。你说,“它不工作,不能通过参数”。您很可能正在尝试在下面的代码段中使用“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);
}
于 2013-10-25T16:21:23.673 回答