I want to pass textbox text from one form to another using Property get and set.

I have done it successfully on click event and then when popup window opens (or form2 opens) i get the value after initialization...

But i want to learn how to do it with property i already have my property in form1 where the textbox is:

public string PropNearest
                return Nearest.Text;
                Nearest.Text = value;

and in 2nd form i have

private Form1 frm= new Form1();
string sd = frm.PropNearest;

but the above returns always empty string...

7 Years
Discussion Span
Last Post by roemerito

As ddanbe pointed out, the problem is that you are calling the property from a new instance of your form.
This is a very common mistake and is a result of not fully understanding the class/object paradigm. You have a Form class that outlines the controls and behaviours of your form. An instance of that form is created in memory and holds its own distinct values.

Lets run through it to highlight the problem:

-When you run your application, an instance of your first form is created in memory (we'll call that firstForm1).

-You type something in the textbox on that form, so now firstForm1.Nearest.Text = "something".

-You then click a button which creates an instance of Form2 (we'll call it firstForm2).

-Inside firstForm2 you are declaring a variable 'frm' to store an instance of your Form1 class. You are then storing a new Form1(); in that variable. The key word here is new.
What you have done is created a completely new instance of Form1 (we'll call it secondForm1).

-So now when you call frm.PropNearest, what you are actually doing is getting the value of secondForm1.Nearest.Text which you havent changed so it is still an empty string.

To get the value you want, you need to locate firstForm1 and access the porperty in that instance. The problem is that your default Main() method creates an annonymous instance of your form. To access it from your second form you will need to pass a handle from Form1 and store it in Form2. You can either do this in the Form2 constructor:

private Form1 Form1Handle;
public Form2(Form1 handle2Form1)
   Form1Handle = Handle2Form1;

//in Form1
Form2 newForm = new Form2(this);

Or you can use a property in Form2 which you set from Form1:

private Form1 Form1Handle;
public Form1 Handle2Form1
get{return Form1Handle;}
set{Form1Handle= value;}

//in Form1
Form2 newForm = new Form2();
newForm.Handle2Form1 = this;

This will store a reference to your original instance of Form1 (firstForm1) inside your instance of Form2 (firstForm2). You can then call the property using that reference Form1Handle.PropNearest This is not an ideal situation though as you should try to make classes as distinct and seperate as possible. One solution to look into would be singletons.
This example does suffice to highlight the problem though. Hope this helps clear things up :)

Votes + Comments
Excellent explanation!

thank you both now i get it... basiacally i had the property in form1 and i was creating new instance as mentioned by ddanbe and Ryshad :)


I Solved my Issue

In MainForm i added a this method

public  void GetFamilia(){
            familiaTextBox.Text = Properties.Settings.Default.fam_id.ToString();
            label2.Text = Properties.Settings.Default.fam_desc.ToString();


And of the SubForm i added this method on the CellClick

 private void AceptarSeleccion(object sender, DataGridViewCellEventArgs e)
            if (Articulos.ActiveForm.Visible == true)
                Properties.Settings.Default.fam_id = int.Parse(idTextBox.Text);
                Properties.Settings.Default.fam_desc = familiaTextBox.Text;

                // Set a property on Form1 (w/o worrying about the implementation) form1.LabelText = textForLabelInForm1.Text;
               // MessageBox.Show("test");

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.