I have two forms, and I need pass a value from form1.textbox1 to form2.variable
Form1:
string Ed = \"\", En = \"\";
public string En1
{
get { r
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_;
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;
}
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;
}
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 ... .
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.
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