Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have created two forms

1) form1
2) form2

i want to pass the value of the text box from form1 to form2
Posted
Comments
RaisKazi 1-Nov-11 7:02am    
Web/Windows?

Assuming this is not a web question ('forms' mean something rather different there): instead of just passing values between forms as the previous answers show (which is technically correct), it is much better if the data is stored in a separate data storage class, not in either form, and both forms have their values bound to the data storage class.

By 'bound' here I don't necessarily mean literally using the data binding, though if it's WPF/Silverlight it makes sense to use the binding in the XAML, but they should read and write the values back into the data class where appropriate. (For example, in a modal dialog, read the value before showing the dialog and write it back on Save.)

Depending on the complexity of the application and the binding needed, it may be appropriate to insert a binding helper or view-model class between the data model and the UI; for a simple text property though you can generally bind straight to the model. (One reason not to would be if you wanted to implement notification in a helper layer not in the data model itself.)

This may seem like overkill for a simple application but it is a good thing to get into the habit of doing because for a real system it will help you keep things much cleaner.
 
Share this answer
 
use querystring,
for form1 set method="get" and action="form2"
and on form2 load event write.
C#
textbox2.text=Request.querystring["textbox1"].tostring();
 
Share this answer
 
v2
there are multiple options,

1) QueryString - the value will be passed with the url

in form1 set the value
C#
Response.Redirect("~/form2.aspx?id=" + textbox1.text + "&city=" + textbox2.text);


in form2
C#
string strValue1 = Request.QueryString["id"];
string strValue2 = Request.QueryString["city"];


2) Session State

in form1 set the value
C#
Session["id"] = textbox1.text;

in form2 get the value
C#
string strValue = convert.toString(Session["Id"]);


the rest of the options are least important, however, I am stating here, do Google for these topics, to read some basic details.

3) cross page posting
4) cookies
5) application state

mark as answer if helps you
 
Share this answer
 
v2
create a single argument constructor on form2
i.e.

C#
class Form2
{
string frm1_tb_value = string.Empty;

Form2(string value)
{
  frm1_tb_value = value;
}

}


now when you create an object of Form2 on Form1 for opening it. pass textbox value inside the bracess of Form2 constuctor
i.e

C#
Form2 frm = new Form2(textbox1.Text);
frm.Show();


this is one of the ways, you can pass value to other forms. :)
 
Share this answer
 
Comments
member60 1-Nov-11 7:43am    
cool
In windows Form you can use proprties to achive this.
or
you can make control public as

make TextBox1 public on Form2
At Form1
on button click that open form 2
use
C#
Form2 f2=new Form2();
f2.TextBox1.Text=textBox1.Text;//form1 textbox
f2.Show();
 
Share this answer
 
declare session variable like that,

form1:

C#
session["value"]=textbox1.text;


form2:

<pre lang="c#">
textbox1.text=session["value"];
 
Share this answer
 
v2

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