Click here to Skip to main content
15,891,855 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to retrieve values from first form textboxes to another form textboxes.
Posted
Comments
BillWoodruff 26-Sep-11 12:40pm    
This type of question is asked frequently here, and answered. You need to give us some more details:

1. what creates both forms: does one form create the other and show it, or are they both created by something else ?

2. when, exactly, does the other form need to get the value in the first form's textboxes ? once only (as one answer you have below shows), or any time the end-user does something like click on a button in the second form ... or any-time the text in the first form changes ? Is the update required triggered when the user is using the first form, or the second ?

1. Create a public variable or property in form2 (Eg:Form1String)

C#
public partial class Form2 : Form
    {
        public string Form1String;

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Form1String);
        }
    }


2. Set for form2 property when calling form2 from form1

Form2 ofrm2 = new Form2();
           ofrm2.Form1String = textBox1.Text;
           ofrm2.Show();


3. When you click on the form 2 button it will display the text you have entered in form1

Hope this helps
 
Share this answer
 
Create one set property in second form like
C#
public String FirstFormText
        {
           set { textBox1.Text = value; }

        }



Assign TextBox value to second form property this will call set method in property and assign your text to second form text box

frm.FirstFormText = textBox1.Text; 
 
Share this answer
 
v2
Comments
BillWoodruff 26-Sep-11 12:42pm    
you are on the 'right track' here, but you need to show how the reference to the second form is set in the first form. and your answer assumes that the update of the second form is triggered by the user doing something in the first form ... that's not the only possibility ... but a good start on an answer.
Thanks for the thoughts BillWoodruff, I have modified the code now so that the form2 does not needs to be opened from form1 and hope this resolves the issue

1. Define the following under the form2 class

C#
public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }

            return null;
        }


2.Add a button in form 2 and the code behind is

C#
private void button1_Click(object sender, EventArgs e)
       {
           if ((Form1)IsFormAlreadyOpen(typeof(Form1)) != null)
           {
               var x = (Form1)IsFormAlreadyOpen(typeof(Form1));
               MessageBox.Show(x.textBox1.Text);

           } else {

               MessageBox.Show("The forms is not opened");
           }
       }


3. click the button on form 2 will retrieve the value in form1 Textbox1.

This does not need the form2 to be called from form1 and returns the Textbox1 value at that time the button is clicked.

Hope this helps
 
Share this answer
 
suppose your app has 2 forms

form1 & form2

and you need value of form 1 on form 2

there are 2 methods

1st one is you make a class that contains static variable .
so assign value to then and access where you want in your application.

2nd one is
make the constructor of form 2

after Initiliase Component of form2

int val;
Form2 =new Form2(int myVal)
{
val=myval;
}
 
Share this answer
 
You've got some technical answers already. But this is a design problem. If two forms need to look at the same data, that data should be in a separate data store, not in either form; the forms should just be (interactive) views on the data. Look into data binding and separation of data and UI; if using WPF, read up on MVVM.

The same data model should be bound to both forms, so that when the user types something on one form, it updates the data model, and notifies the binding mechanism so that the other control(s) which are bound to it can update themselves. Typically that notification is done through INotifyPropertyChanged.

There are a few cases where it is reasonable to directly access another form: in particular, if the second form is a subsidiary of the first (e.g. a dialogue created by pressing a button on the first), and it is modal (so you don't have synchronisation problems). Two forms which can be active at the same time pointing at the same data should be bound to an external data object, because that data clearly does not 'belong' to either form.
 
Share this answer
 
Comments
Bala Selvanayagam 27-Sep-11 11:11am    
I understand your point BobJanova.

Can I ask "Koushik Uppari" to give bit more background of the requirement please ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900