1

I have 2 forms: Form1, Form2

In Form1 I have a textBox with some data that user have to enter. I need the text that the user entered in Form1's textBox. How can I access to that in Form2?

I used property but it did not work because the text value entered by user in run time. can any body help me?

flindeberg
  • 4,887
  • 1
  • 24
  • 37
Elahe
  • 1,379
  • 2
  • 18
  • 34
  • 2
    Any code you tried so far??? – Pratik Mar 18 '14 at 06:14
  • you should make `Invoke` to it – cnd Mar 18 '14 at 06:22
  • This is a typical problem if you don't separate view and data. Read more about MVC. Be warned that it's not a quick fix. But in the long term your code will be better and not a collection of ugly kludges and hacks. – nalply Mar 18 '14 at 06:24
  • 1
    Web is stateless. You need to save From1 data in a storage like database or session and then use in the second form. – Alborz Mar 18 '14 at 06:29
  • My view code and data is seprate, its a sample. Its a windows project and my data is stored in dataBase. but I need to the data that user select in form1. – Elahe Mar 18 '14 at 06:38
  • @Elahe Show some code! This is like asking "my car isn't working, why?" You do see that all the answers have made different assumptions? – flindeberg Mar 18 '14 at 06:41
  • This question is too vague. – Mick Mar 18 '14 at 07:29

4 Answers4

3
Form1 frmOne = new Form1();

string text= frmOne.Textbox1.Text;

also do not forget to change the modifier of the textbox to public

codemania
  • 1,098
  • 1
  • 9
  • 26
Dinesh Lamsal
  • 67
  • 1
  • 7
  • you save my life :D - i have a problem from changing textbox text from started form in another tread and .... , this way help me to change it from other form from other thread – ehsan wwe Nov 16 '21 at 09:25
2

Make a constructor for form2 that accept string and in calling new form2 pass form1.frm1Textbox.text to contructor then set it to form2.frm2Textbox.text

Form2 form = new Form2(frm1Textbox.text);

in form2 constructor

public class Form2 : Form
{
    public Form2(string text)
    {
        frm2Textbox.Text = text; 
    }
}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • thanks for your answer but it dosent work. when user inter a text value in text box, the value of text box dident change in form2. thats true when the programmer inter a value in textbox in sourceCode – Elahe Mar 18 '14 at 06:19
  • If you want to do that then use local storage like session – Sajeetharan Mar 18 '14 at 06:33
1

Create a public function, in which just return the textbox's text. Like below

public string Get_Form1Text()
{
    return textbox1.Text;
}

And then call it from Form2 where you need the textbox's value.


New Edit

First create a property in Form2. Then implement textbox text changed event in form1. then assign the textbox's value to Form2's property in event.

After this you have to implement property change event in Form2. For raise that event refer this Question

Community
  • 1
  • 1
Karthik AMR
  • 1,694
  • 21
  • 29
1

Try to get the value using static variable, which should be created in Form2,

        private static string form1text;
        public static string Form1text
        {
            get { return form1text; }
            set { form1text= value; }
        }
///
string variable = form1text;//current value can be accesed

Assisgn textbox value to Form2 Static variable from Form1, like as follows

Form1.Form1text = textbox1.Text;
Anees Deen
  • 1,385
  • 2
  • 17
  • 31