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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Joginder BangerRank #360 · 7K reputationPosted Dec 2, 2014, 1:03 AM EST
Hi Santhosh Kumar make the object of your windows form. I assume windows form 1 textbox to copy data and past windows form 2 text box. windowsform1 win1= new Windowsform1(); this.textBox1.Text = win1.textBox1.Text; try this code. if face any error comment to replyAshok Yadav184 reputationPosted Dec 2, 2014, 1:12 AM EST
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;
VulpesRank #55 · 29.2K reputationPosted Dec 2, 2014, 1:47 AM EST
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;
Selva GanapathyRank #560 · 4.3K reputationPosted Dec 2, 2014, 7:08 PM EST
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