在表单之间使用列表 [英] Using list between forms
问题描述
我在form1上生成了一个列表,它的内容是数字和一个K000-000形式的字母.然后我想打开form2.在表单 2 上,我有一个文本框、一个列表框和一个按钮.在文本框中,您将键入更多数字,例如 12345.当您单击按钮时,我希望它添加带有-"的 form1 列表的内容以及您在 Form2 的文本框中键入的内容.所以列表框将是 K000-000-12345.我不确定如何在 Form2 上正确使用 Form1 的列表并将其添加到其中.
I generated a list on form1 who's contents are numbers and a letter in the form of K000-000. I then want to open form2. On form 2 I have a text box, a list box and a button. In the text box you will type some more number in like 12345. When you click the button I want it to add the contents of form1's list with a "-" on the end and the contents you typed in Form2's textbox. So the listbox will be K000-000-12345. I'm not sure how to properly use Form1's list on Form2 and also add to it.
表格 1
DesignNo.FindByItem(electype, (int.Parse)(dwgno));
List<DesignNo> Electrode = DesignNo.FindByItem(electype, (int.Parse)(dwgno));
if (Electrode.Count <= 0)
{
MessageBox.Show("Unknown Electrode Number");
}
frmElectrode frmelec = new frmElectrode();
frmelec.Show();
示例中的 frmelec 为 Form2.
frmelec being Form2 in the example.
推荐答案
在 Form 2 中创建一个公共属性
create a public property inside Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string Content { get; private set; }
public void ButtonOkOnClick()
{
this.Content = this.textBox1.Text;
this.Close();
}
}
在 Form2 关闭后消费 Form1 中的公共属性
Consume the public property in Form1 after Form2 gets closed
public Form1()
{
InitializeComponent();
Form2 form2 = new Form2();
form2.Show();
form2.Closed += (sender, args) => this.list.Add(form.Content);
}
这样依赖才是正确的方向,form2只是一个输入表单,可以不带任何依赖地重用.
This way the dependency is the right direction, form2 is just a input form and can be reused without any dependencies.
这篇关于在表单之间使用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!