hi,
i want to get the value of the textbox from one form to another in c# windows application.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Selva GanapathyPosted Dec 3, 2014, 12:08 AM
Hi Santhosh,
You can set the textbox as public from the designer.cs file
Use "public TextBox textBox1;" instead of "private TextBox textBox1;" so that use can assess the text box using the form like "form1.textBox1"
or follow the blog by send the form details to another form
http://www.c-sharpcorner.com/UploadFile/6f0898/simple-way-to-establish-interaction-between-two-from-inside/
Regards,
Selva Ganapathy K
VulpesPosted Dec 2, 2014, 6:47 AM
Suppose it's on a form called Form2. Then an easy way of getting a reference to Form2 from another form is as follows:
Form2 f2 = (Form2)Application.OpenForms["Form2"];
Next you need to get a reference to the textbox itself (textBox1 say). Controls are normally private fields of their containing form but (from the Properties Box) you can change their Modifiers property to internal or public.
You can then simply do:
string text = f2.texBox1.Text;
You can also do it without changing the Modifiers property by going through the form's Controls property:
TextBox tb = (TextBox)f2.Controls["textBox1"];
string text = tb.Text;
However, this assumes that the textbox has been placed directly on the form. If it's been placed instead in some container such as panel1 say, then you can still use this approach but the code is more complicated.
Panel p = (Panel)f2.Controls["panel1"];
TextBox tb = (TextBox)p.Controls["textBox1"];
string text = tb.Text;
Ashok YadavPosted Dec 2, 2014, 6:12 AM
Deaclare public variable
assign the value of textbox
use that variable in another form
like
public string txtval ="";
txtval = TextBox1.Text;
use txtval variable in another form. you can also define this variable in public.
TextBox2.Text=txtval;
Joginder BangerPosted Dec 2, 2014, 6:03 AM