Passing Values from one Form to another Form in a Button click
Actually I have an object to pass. So I did this
in form1-->
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
in form2-->
public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
a.set(g);
this.Close();
}
}
pass a value from one form to another
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.
Passing a value from one form to another form
You don't access your form1, from which you created form2. In form2 button1_Click you create new instance of Form1, which is not the same as initial. You may pass your form1 instance to form2 constructor like that:
// Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2(this);
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
Form1 ownerForm = null;
public Form2(Form1 ownerForm)
{
InitializeComponent();
this.ownerForm = ownerForm;
}
private void button1_Click(object sender, EventArgs e)
{
this.ownerForm.PassValue(textBox1.Text);
this.Close();
}
}
Passing Textbox Values one form to another form in Windows Forms
One way is to just have a method on your second form that returns any needed details
using (DrawForm drawForm = new DrawForm ())
{
drawForm.ShowDialog(); //will wait for the form to close before continuing
var userResults = drawForm.GetResults(); //returns whatever you need the form to return
return userResults;
}
The above logic only works if the second form is a popup form of sorts. If you want both forms to be persistent, then it's a bit different. You might want to ensure both forms have access to each other. When you create the drawform be sure to save a reference to it. and in the constructor for your drawform add an argument to include the main form.
DrawForm drawForm = new DrawForm(this);//now the forms can talk to each other
in your drawing form when the user hits 'go'
mainForm.TriggerFinishedDrawing(drawingObject);
There are several ways to achieve this sort of behaviour, you just need to decide what sort of flow makes the most sense in your use case
How to pass string value from one form to another form's load event in C #
Just create a property on the Form2 class and set it before you show Form2.
public class Form2
{
...
public string MyProperty { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(this.MyProperty);
}
}
From Form1:
public void button1_Click(object sender, EventArgs e)
{
string departmentName = "IT";
Form2 frm2 = new Form2();
frm2.MyProperty = departmentName;
frm2.Show();
this.Hide();
}
How do I pass a string value from one form to another in c#?
The easiest way to pass any value from a form to another form is by creating a second constructor to the form you want to pass the value to. For Example:
Let's say you want to pass string from form1 to form2 then the code would look something like this:
First create a second constructor in form2 like:
public Form2(string s)
{
InitializeComponent();
String_passed_from_Form_1 = s
}
Then when you make the object of form2 in form1 code:
Form2 obj = new Form2("String Value to pass to form2");
obj.show();
Doing the above the string value will be stored in the vairable "String_passed_from_Form_1" and would be accessable in the other form.
Related Topics
Linq Join Iquery, How to Use Defaultifempty
How to Set the Windows Default Printer in C#
Preprocessor Directive in C# for Importing Based on Platform
Get the Final Generated HTML Source Using C# or Vb.Net
Datetime Tostring Issue with Formatting Months with "Mm" Specifier
Could Not Load File or Assembly 'System.Web.Http 4.0.0 After Update from 2012 to 2013
How to Read Data from Excel File Using C#
Pair Bluetooth Devices to a Computer with 32Feet .Net Bluetooth Library
Rotate a Point by Another Point in 2D
The Property 'Id' Is Part of the Object's Key Information and Cannot Be Modified
C# Regular Expressions, String Between Single Quotes
How to Set Dynamic Value in My Attribute
Manual Editing of *.Designer.Cs File