I have tried this to pass the information:
Form1 frm1 = new Form1();
textBox1.Text = ((TextBox)frm1.Controls["textBox1"]).Text;
This is in the form load of the form getting the information. But there is no text. How do I fix this? Form2
is grabbing Form1
's text and displaying it.
I find Easy and Logical Way to Passing Text value one textbox to other in Windows Application.
In Second Form Write Code:-
Create a Parameter of *Form2* Constructor.
namespace PassingValue
{
public partial class Form2 : Form
{
public Form2(string message)
{
InitializeComponent();
Textbox1.Text= message;
}
}
}
In First Form Write Code:-
Use the Parameter of Second Form in *First Form*:-
namespace PassValue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 f2=new Form2(Textbox.Text);
f2.Show();
}
}
}
Expose the contents of the textbox using a property:
class Form1 {
public string MyValue {
get { return textBox1.Text; }
}
}
Then in Form2 do this:
var frm1 = new Form1();
frm1.ShowDialog(this); // make sure this instance of Form1 is visible
textBox1.Text = frm1.MyValue;
If you want frm1
to be constantly visible then make frm1
a class variable of Form2
, and call .Show()
in the constructor of Form2
for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With