Question

In the General Ledger, when user double clicks on the dgv row, The Purchase Invoice Form opens with relevent Purchase Invoices. If the Purchase Invoice is already open in MDIPARENT, it will just activated, for this scenario i apply the check

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

I want to pass the parameter


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

Its not working and not able to pass PARAMETER, Whats The Solution For This ???

   return;
        }
   }

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

   form.btnNewInvoice.PerformClick();
   form.txtInvoiceNo.Text = invNo;
   form.dataGridView1.Focus();
}
Was it helpful?

Solution

Not sure, but it seems to me that you want to perform this code:

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

for when the form is already open. You say that, "Its not working and not able to pass PARAMETER". Most likely you are trying to use "f" in the snippet below?

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

If so, just cast "f" to PurchaseForm like this:

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

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

  pf.Visible = true;
  pf.Activate();
  .....
}

EDIT: Simple example of what Servy is talking about...

Basically you'd create a public method to receive the invoice number. That method can access the controls and set values directly. Something like this:

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

}

Then, after casting "f" to PurchaseForm (as shown previously), you'd just do:

if (f.GetType() == typeof(PurchaseForm ))
{
  PurchaseForm pf = (PurchaseForm)f;
  pf.SetInvoiceNumber(invNo);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top