best way to pass value to another open form  
Author Message
Bear23





PostPosted: Visual C# General, best way to pass value to another open form Top

In vb.net it is so simple all you do is add a button on the form and when both form1 and form2 is open all you had to do is this.

form2.textbox1.text = "Cat"

where in c# it is not that simple.

I am curious to see how others do what I have mentioned above in c#.

So basically the scenerio is I have both form1 and form2 open. Form2 has a textbox and form1 has a button on it that I want to pass a certain word to the textbox on form2



Visual C#19  
 
 
Udhaya Kumar D





PostPosted: Visual C# General, best way to pass value to another open form Top

In the form1:

=================================
private Form2 form2;

public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Show();
}

private void button_Click(object sender, EventArgs e)
{
form2.TextBox1.Text = "Assigned By Form1";
}
==================================

And in the form2 add a property for the textbox:

===================================

public TextBox TextBox1
{
get
{
return textBox1;
}
set
{
textBox1 = value;
}
}

===================================


 
 
Bear23





PostPosted: Visual C# General, best way to pass value to another open form Top

what about if you have a textbox in form1 and want to pass that.

Also assigning the caption of form2 with a value from a textbox in form1.

Why did microsoft make vb.net so much easier to do this.

meaning

Form2.text = Form1.textbox.text

this cannot be done with open forms in c#.

Little things like this make c# a pain.


 
 
Udhaya Kumar D





PostPosted: Visual C# General, best way to pass value to another open form Top

To assign the caption of form2 with a value from a textbox in form1.

In the form1:

=================================
private Form2 form2;

public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Text = form1.textBox1.Text;
form2.Show();
}
=================================

To pass the textbox value of form1 to form2

private void button_Click(object sender, EventArgs e)
{
form2.TextBox1.Text =
form1.textBox1.Text;
}
==================================