Reputation: 127
I have two forms, and I need pass a value from form1.textbox1 to form2.variable
Form1:
string Ed = "", En = "";
public string En1
{
get { return En; }
set { En = value; }
}
public string Ed1
{
get { return Ed; }
set { Ed = value; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.Show();
F2.textbox1value = Ed;
F2.textbox2value = En;
}
` and Form2:
public string textbox1value
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public string textbox2value
{
get { return textBox2.Text; }
set { textBox2.Text = value; }
}
private void button1_Click(object sender, EventArgs e)
{
Form1 F1 = new Form1();
F1.Ed1 = textBox1.Text;
F1.En1 = textBox2.Text;
}
when I click "save" on form2 and open debug I see "ed = 3; en = 5", but when i click "open form2" on form1 and open debug, i see "Ed = null; En = null;" and shows a blank form without value in textboxes. help please.
Upvotes: 4
Views: 53300
Reputation: 11
This question may be old, but anyways...
Another way to do it is, to have a constructor in form2 that accepts an argument of the same type of the data you want to pass to, and, when button in form1 is clicked, you create the instance of form2 using the constructor that accepts the argument, and pass the data to it.
//Form1
Form2 form2;
button1_clic(object sender, eventArgs e)
{
form2 = new Form2(textbox1.text);
form2.Showdialog();
}
//Form2
string var = string.empty;
public Form2(string val)
{
InitializeComponents();
var = val;
}
Upvotes: 0
Reputation: 1411
You create a new form, so old values will be lost. Default values are null.
Form1 F1 = new Form1(); //I'm a new Form, I don't know anything about an old form, even if we are the same type
You can use static vars, which would be the easiest solution to archive your goal, but there are other ways like constructors, containers, events etc.
public static string En1
{
get { return En; }
set { En = value; }
}
public static string Ed1
{
get { return Ed; }
set { Ed = value; }
}
And in the other form
private void button1_Click(object sender, EventArgs e)
{
Form1 F1 = new Form1();
Form1.Ed1 = textBox1.Text;
Form1.En1 = textBox2.Text;
}
Please be advised that a static variable exists only once for a class. So if you have multiple instances and you change the static variable in one, the change also affects all other instances.
Upvotes: 12
Reputation: 1
If you describe value as static then you can access it directly in Form1 and you can access it from Form2 :
static public string Text_;
string PassedValue_=Form1.Text_;
Upvotes: 0
Reputation: 1
This can be achieved easily by creating an instance of Form 1 in Form 2. This is one of the approach.
Follow the steps:
In Form 1 : Make sure that your control is public.
eg: txtForm1.Text = "Bangalore";
In Form 2 :
Step 1: Create an instance of Form 1 globally. If the instance is created locally the value contained by the control cannot be accessed, only null value will be returned even the data has been populated to it.
Step 2 : Retrieve the control's value by Form 1's instance.
eg: Form1 frm1 = new Form1();
string Form1Value = frm1.txtForm1.Text
Upvotes: 0
Reputation: 584
Always keep a copy of the information of form2
in form1
, this is:
When the user clicks save on form2
the information go in the local variables of form2
and then form2
runs an Event (telling form1
that its information must be saved). In form1
you handle this event and tell form1
that whenever this event is run from1
must copy the information of form2
into itself.
On the other hand when ever you are opening form2
again you should first give the information back to it and then execute the show()
method. After this you should handle the shown()
event of form2
in the way that whenever it is shown, first form2
must put the information it has to the related textboxes, etc ... .
Upvotes: 0
Reputation: 10694
You can create constuctor for form2
which accept 2 arguments and access these variables
Form2 frm2 = new Form2(textBox1.Text,textBox2.Text);
frm2.Show();
Constructor would look like
public Form2(string txt1,string txt2)
{
InitializeComponent();
textbox1value.Text = txt1;
textbox1value.Text=txt2
}
There are many ways to pass data between forms such as
1) Using constructor
2) Using objects
3) Using properties
4) Using delegates
Check this link for details http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
Hope It helps!
Upvotes: 8
Reputation: 376
The debugger doesn't complain about anything? Hm. Maybe you could try to modify your button1_click method in form 1 as follows:
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
Form2.Parent = this;
F2.Show();
F2.textbox1value = F2.Parent.Ed;
F2.textbox2value = F2.Parent.En;
}
Upvotes: 0