Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Application Framework Forms , +
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 11-Aug-12 2:08am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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 :
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;
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

class form1
{
   type var1;
}
 
class form2
{
   type var2=form1.var1;
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

form1
 

Button click_event
 
   string name="Sham";
   
   form2 frm=new form2();
   frm.name1=name;
   frm.show();this.hide();
form2
 
form load event
     string name1;
     
     textbox1.text=name1.tostring();
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

For setting Form3 as Startup Form go to class "Program":
 
Application.Run(new Form3());
 
There is sample for passing data:
 
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; }
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
 
Application.Run(new Form3());
 
Hope this works for you,
 
thanks
-Amit Gajjar
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

pass controls/variables while create instance of new form
eg.
public class formDest
{
	public formDest(string c)
	{
		//something to do
	}
}
 
now, pass value from source form to destination form
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!
Smile | :)
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Try This :-
 
in Form 1 Class create variable like this:-
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 :-
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:-
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 :-
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new form1());            
}
In this method in Third Line i.e.
Application.Run(new form1());  
write the name of the form which you want to call first .
 
Any query still, plz reply or comment.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Try this,
Take Use sessionStorage.setItem() in form1 and use sessionStorage.getItem() in form2 button click event
  Permalink  
Comments
sadegh_rusta at 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)

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100