2

I have 2 forms called BillingForm(parent form) and SearchProduct(child form).

BillingForm code

private void textBoxProductNo_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode==Keys.F9)
    {
        using(SearchProduct sp=new SearchProduct)
        {
            sp.ShowDialog(this);
        }
    }
}

public void updatedText(string fromChildForm)
{
     textBoxProduct.text=fromChildForm;
}

SearchProduct form code (Child Form)

private void dataGridView1_KeyDown(object sender,KeyEventArgs e)
{
    if(e.KeyCode==Keys.Enter)
    {
        BillingForm bf=(BillingForm)this.Owner;   //Error appear here 
        bf.updatedText("Hello World");
        this.close();
    }
}

I am getting an error message.

An unhandled exception of type 'System.InvalidCastException' occurred in BillingSoftware.exe
Additional information: Unable to cast object of type 'BillingForm.MDIParent' to type 'BillingForm.BillingForm

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Faisal
  • 179
  • 1
  • 4
  • 15
  • 1
    Possible duplicate of [How do I pass a value from a child back to the parent form?](http://stackoverflow.com/questions/280579/how-do-i-pass-a-value-from-a-child-back-to-the-parent-form) – itsme86 Jul 15 '16 at 21:12
  • @itsme86 I saw that coding. I used the same coding only. But I am getting Exception. Please look at my error message – Faisal Jul 15 '16 at 21:15
  • Is BillingForm an MDI form? – itsme86 Jul 15 '16 at 21:17
  • @itsme86 MDIParent is MDI form.. BillingForm is one of child form of MDI form. When press ctrl+N in MDI form then BillingForm will be opened – Faisal Jul 15 '16 at 21:22
  • From the code and error message, it appears that the `textBoxProductNo` control belongs to `BillingForm.MDIParent`, not `BillingForm.BillingForm`. Is that the case? – Rufus L Jul 15 '16 at 21:24
  • You can try something like `BillingForm bf = Application.OpenForms.OfType().FirstOrDefault();` – Slai Jul 15 '16 at 21:26
  • @RufusL No. textBoxProductNo control belongs to BillingForm, not to MDIParent Form. MDIParent form contains menu name called File->new(BillingForm) – Faisal Jul 15 '16 at 21:29
  • 2
    Put a breakpoint on the line `BillingForm bf=(BillingForm)this.Owner;` and debug. When the code breaks, highlight your mouse over `Owner` and see what its type is. From there, see if there are any properties on `Owner` which will return the `BillingForm` that you are looking for. – Ian R. O'Brien Jul 15 '16 at 21:30
  • @Slai Thank you for your code. It's working perfectly. Can you explain me that why my code shows error?. Thank you my friend – Faisal Jul 15 '16 at 21:33
  • @Faisal because BillingForm is not the the owner :] maybe `BillingForm bf = this.MDIParent as BillingForm;` is ? – Slai Jul 15 '16 at 21:48

1 Answers1

0

Try to pass the parent into constructor and use it in a variable

using(SearchProduct sp = new SearchProduct(this))
{
    sp.ShowDialog(this);
}

//In SearchProduct class
public BillingForm MyParent {get; private set;}

public SearchProduct(BillingForm parent)
{
    this.MyParent = parent;
}

private void dataGridView1_KeyDown(object sender,KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        BillingForm bf = this.MyParent;
        bf.updatedText("Hello World");
        this.close();
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
Esperento57
  • 16,521
  • 3
  • 39
  • 45