Passing data between two forms with properties [duplicate]
In form2 you need to create event and subscribe to it in form1. Thats all.
//declare event in form 2
public event EventHandler SomeTextInSomeFormChanged;
// call event in form2 text_changed event
if(SomeTextInSomeFormChanged != null)
SomeTextInSomeFormChanged(this, null);
//in form 1 subcribe to event
var form2 = new Form2();
form2.SomeTextInSomeFormChanged += SomeHandlerInForm1WhereYouCanSetForcusInForm1
Update:
Form2:
public Form2()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
//if subscribers exists
if(SomeTextInSomeFormChanged != null)
{
SomeTextInSomeFormChanged(form2_textBox1, null);
}
}
Form1:
public partial class Form1 : Form {
public Form1() { InitializeComponent(); }
private void form1_button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);
}
public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
{
var textBoxFromForm2 = (TextBox)sender;
form1_textBox1.Text = textBoxFromForm2.Text
this.Focus();
}
}
The website listed below has very good tutorials. This particular page demonstrates how this can be achieved:
http://www.vcskicks.com/data-between-forms.php