1,105,232 Community Members

C# Winforms pass textbox text to another form

Member Avatar
sam1
Posting Whiz
301 posts since Nov 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

hi,

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
        {
            get
            {
                return Nearest.Text;
            }
            set
            {
                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...

Member Avatar
ddanbe
Posting Expert
5,335 posts since Oct 2008
Reputation Points: 2,054 [?]
Q&As Helped to Solve: 904 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

You are instantiating a new instance of Form1 so the textbox will be empty.
Look at this snippet: http://www.daniweb.com/code/snippet217193.html

Member Avatar
Ryshad
Nearly a Posting Virtuoso
1,263 posts since Aug 2009
Reputation Points: 480 [?]
Q&As Helped to Solve: 247 [?]
Skill Endorsements: 14 [?]
 
1
 

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 :)

Member Avatar
sam1
Posting Whiz
301 posts since Nov 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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 :)

Question Answered as of 4 Years Ago by ddanbe and Ryshad
Member Avatar
roemerito
Newbie Poster
17 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
                Properties.Settings.Default.Save();
                articulos.GetFamilia();

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


        }
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article