whatdoyouNeedFromMe
whatdoyouNeedFromMe

Reputation: 105

Issue passing the data from one form to another

Overview:

Passing the data from one form to another.

This is a WinForm application which has two forms, named form1 and PostCodeForm. I need to pass data from the short-lived form PostcodeForm back to Form1 through a clickEvent, then close the form. The values are stored into dataTable in PostcodeSearch class, and to access them I:

  1. Loop through dataTable in PostCodeForm

    for (var item = 0; item < retreiveResults.Count; item++)
    {
        var num = dataGridView2.Rows.Add();
        dataGridView2.Rows[num].Cells[0].Value = retreiveResults[item].City;
        dataGridView2.Rows[num].Cells[1].Value = retreiveResults[item].District;
        dataGridView2.Rows[num].Cells[2].Value = retreiveResults[item].HouseName;
    
  2. Created an instance of Form1 in PostCodeForm:

    Form1 formIndi = new Form1();
    
  3. Then created some local variables which are initialized just before the end of the loop in PostCodeForm

    var _City_Cell = dataGridView2.Rows[num].Cells[0].Value;
    var _District_Cell = dataGridView2.Rows[num].Cells[1].Value;
    var _HouseName_Cell = dataGridView2.Rows[num].Cells[2].Value;
    
  4. Then to pass them to Form1 (in PostCodeForm):

    formIndi.txt_City.Text = _StreetName_Cell.ToString();
    formIndi.txt_HouseName.Text = _HouseName_Cell.ToString();
    formIndi.txt_ District.Text = _District_Cell.ToString();
    

I need to pass the data back to the main form and store it in the relevant textBoxes.

Issue

My issue is that none of my textBoxes are updating with given values but when I debug the Vars inside the postCodeForm I can see the values, so I have no idea why then the TextBoxes are not displaying the values, as I have always passed data from form to form this way.

Upvotes: 2

Views: 85

Answers (2)

default locale
default locale

Reputation: 13446

As @Ferus7 pointed out you're creating a new instance of Form1 instead of updating the values in your main form.

//new instance with new text boxes and values
Form1 formIndi = new Form1();
//updates the control in the new form, doesn't affect the caller.
formIndi.txt_City.Text = _StreetName_Cell.ToString();

If you want to retrieve values from PostcodeForm, there're multiple ways to do that. One option is to declare properties/methods in PostcodeForm and to retrieve values through them:

//declaration in PostcodeForm
class PostcodeForm {
//...
public string StreetName {get; private set;}

//after data retrieval
StreetName = _StreetName_Cell.ToString();


//call PostcodeForm from Form1
postcodeForm = new PostcodeForm();
postcodeForm.ShowDialog();
//after that, get the value
txt_City.Text = postcodeForm.StreetName;

Another way is to pass the reference of Form1 to PostcodeForm:

class PostcodeForm {
//declare field
private final Form1 parent;
//create a constructor that accepts `Form1`
PostcodeForm(Form1 parent) 
{
    this.parent = parent;
    //... (InitializeComponents, etc.)
}

//update parent as necessary
parent.txt_City.Text = postcodeForm.StreetName;


//Sample call from Form1
postcodeForm = new PostcodeForm(this);
postcodeForm.ShowDialog();    

Upvotes: 3

Ferus7
Ferus7

Reputation: 727

Render the new instance form

Form1 PostCodeForm= new Form1();
PostCodeForm.Show();
Application.Run();

Upvotes: 1

Related Questions