I have two forms. On form1 I have a button and a textbox (button to open form2 and textbox to get the value of listbox selectd item from form2 ). On form2 I only have a listbox. I just simply want, when click on a button (for opening form2) on form1, form2 to open and listbox selected item from listbox on form2 to fill in textbox on form1.
2 Answers
0
votes
You can go with delegate-event mechanism. implement a delegate and corresponding event in your form2 and call this event whenever you want to update value back on form1. Call this delegate and attach an handler on form1 whenever you initialize form2 and open it. this way you will the handler of your listbox value on form1 and can set your textbox on form1
0
votes
You can do this using delegates. Here's a simple example
On Form 1
private void Button1_Click(System.Object sender, System.EventArgs e)
{
using (Form2 frm = new Form2(UpdateTextBoxValue)) {
frm.ShowDialog();
}
}
public void UpdateTextBoxValue(string value)
{
TextBox1.Text = value;
}
On Form 2
public delegate void UpdateTextBoxValue(string value);
private UpdateTextBoxValue _updateTextBoxValue;
public New(UpdateTextBoxValue updateTextBoxValue)
{
InitializeComponent();
_updateTextBoxValue = updateTextBoxValue;
}
private void ListBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
_updateTextBoxValue.Invoke(ListBox1.SelectedItem.ToString);
}