我有两种形式。在form1上,我有一个按钮和一个文本框(用于打开form2的按钮和用于从form2中获取列表框选定项的值的文本框)。在form2上,我只有一个列表框。我只是想在单击form1上的一个按钮(用于打开form2)时,打开form2并将来自list2的列表框中的选定项列表到listbox中,以填充form1的文本框。
您可以使用委托执行此操作。这是一个简单的例子
在表格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;
}
在表格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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句