0

I have one main form called frmMain and two textboxes on that called txtCustomer and txtProduct and two buttons called btnInsertCustomer and btnInsertProduct.

I then have two forms called frmCustomer and frmProduct. In each form I have a dataGridView which shows the information of customers and products respectively.

I want for example when I click on btnInsertCustomer the frmCustomer opens and I can double click on the dataGridView in this form. When I do, it should insert the value of field customerCode into txtCustomer in frmMain.

Then I want to click on btnInsertProduct and frmProduct will open and I can double click on one row on dataGridView and insert the value of field productCode into txtProduct in frmMain without loosing the value of txtCustomer that I have inserted earlier.

I can get only one value from one of the forms into my main form with my current approach. I made an identifier of txtCustomer and txtProduct assigned to public. Then on the event of CellDoubleClick of dataGridView of frmCustomer I wrote this code:

frmMain fr = new frmMain();
fr.txtCustomer = dgv1FrmCustomer.SelectedRows[0].Cells[1].Value.ToString();
fr.Show();

and the same code for the frmProduct. The problem with this method is that I can only get data from one form. When I open the other form and select a row the data on the previous textbox is gone. I wonder how I can get data from both forms?

David Arno
  • 42,717
  • 16
  • 86
  • 131
hamed
  • 127
  • 1
  • 3
  • 9
  • I'm not sure I completely understand your scenario, but you are instantiating a new frmMain object, so it will not know anything about other frmMain objects unless you overload the constructor. – Johnie Karr Jul 06 '15 at 12:28
  • possible duplicate of [How to access a form control for another form?](http://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) – David Arno Jul 06 '15 at 12:33

1 Answers1

1

You just need to set up some properties on your child forms that you can use to get/set values.

Properties on child form

public class ChildForm : Form
{
    // FIELDS
    private string customerName;
    private string customerCode;

    // PROPERTIES
    public string CustomerName
    {
        get { return customerName; }
        set { customerName = value; }
    }

    public string CustomerCode
    {
        get { return customerCode; }
        set { customerCode = value; }
    }

    // FORM CLOSING
    private void ChildForm_FormClosing(object sender, EventArgs e)
    {
        // SET VALUES
        this.customerName = "name";
        this.customerCode = "012345";
    }
}

Main form - Call child form and get values when closed

using (ChildForm myChildForm = new myChildForm())
{
    myChildForm.ShowDialog();
    string returnedCustomerName = myChildForm.CustomerName;
    string returnedCustomerCode = myChildForm.CustomerCode;
}
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • Thanks. I set the fields and properties but I have problem with the second part of the code. Where do I have to write this using ... code? I wrote it in body of Main form and errors where shown. – hamed Jul 06 '15 at 15:01
  • In this example the `using` statement would go in your main form, probably within a button click or whatever you're using to decide when to launch this form. This bit of code creates a new instance of ChildForm, shows it as a dialog which locks the main form, when the dialog is closed we return the results to the main form. You can then take the returned values and do whatever you want. I hope this makes sense. – Equalsk Jul 06 '15 at 15:05
  • Thank you so much. It worked properly and it was so easy. – hamed Jul 06 '15 at 15:29
  • I guess the first line of the code is as 'using (ChildForm myChildForm = new ChildForm())' – hamed Jul 06 '15 at 15:42