Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i have a variable in form 1
and a button
i want send value of this variable to from 2 when i do click on the button.
and show me in form 2





and how i do set form 3 as start form(set to default)?
Posted

Hi,
You can pass variable from From1 to Form2 through constructor. Write following in button click event to pass TextBox1 value in Form1 to TextBox1 in Form2 :
C#
string data = TextBox1.Text;
Form2 obj = new Form2(data);
obj.Show();

In Form2 create a constructor like this:
Form(string data)
{
     this.TextBox1.Text = data;
}
 
Share this answer
 
v2
C#
class form1
{
   type var1;
}

class form2
{
   type var2=form1.var1;
}
 
Share this answer
 
v2
form1


Button click_event

C#
string name="Sham";

form2 frm=new form2();
frm.name1=name;
frm.show();this.hide();

form2

form load event
C#
string name1;

textbox1.text=name1.tostring();
 
Share this answer
 
v2
For setting Form3 as Startup Form go to class "Program":

C#
Application.Run(new Form3());


There is sample for passing data:

C#
public partial class Form1: Form
{
    private string Data = "some Data...";

    private void Button_Click(object sender, EventArgs e)
    {
         Form2 form = new Form2()
         {
              Value = this.Data
         };

         form.Show();
    }
}


public partial class Form2: Form
{
    public Form2()
    {
        InitializeComponent();
        if (!string.InNullOrEmpty(this.Value))
             this.textBox1.Text = this.Value;
    }

    public string Value { get; set; }
}
 
Share this answer
 
v2
Hi,

Is it fine if you make your variable static ? static will do your work for accessing variable on form2. Let me know if you have any question on static variable.

And for the startup page you need to write code in your program.cs page. write below code in Main function of program.cs page.

C#
Application.Run(new Form3());


Hope this works for you,

thanks
-Amit Gajjar
 
Share this answer
 
pass controls/variables while create instance of new form
eg.
C#
public class formDest
{
	public formDest(string c)
	{
		//something to do
	}
}


now, pass value from source form to destination form
C#
public class formSource
{
	public void btn_click(...)
	{
		formDest fd = new formDest(btn1.Text);
		// this line will initialize form (using New) and pass the btn1.Text's value to destination form through parameter
		fd.show();
	}
}

Happy Coding!
:)
 
Share this answer
 
v3
Try This :-

in Form 1 Class create variable like this:-
C#
public static string a;

then on Button Click event pass the value to variable declared like this. Suppose if I want to Pass TextBox Value then :-
C#
a = TextBox1.Text
write this following code, this will pass your textbox value to variable a.
Then on Form 2, suppose You want to show the value of Form 1 in Label control then write following code:-
C#
label1.Text = Form1.a;

you can see the value passed in textbox on Form 1.

Answer to Second Question.
In Solution Explorer there is a file named Program.cs. In that you will find the following method :-
C#
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new form1());            
}

In this method in Third Line i.e.
C#
Application.Run(new form1());  
write the name of the form which you want to call first .

Any query still, plz reply or comment.
 
Share this answer
 
Try this,
Take Use sessionStorage.setItem() in form1 and use sessionStorage.getItem() in form2 button click event
 
Share this answer
 
Comments
sadegh_rusta 17-Aug-12 16:00pm    
i dont have this method in my cs file

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