我有两个类(表单),当我单击时,我希望将 class2
中的一个项目添加到 class1
中的 listBox
中“接受”按钮。
我尝试使用以下代码,但列表框中没有任何变化:
private void button1_Click(object sender, EventArgs e)
{
CarRental i = new CarRental();
string id = idRental.Text.ToString();
i.listBox1.Items.Add(id);
i.listBox1.Update();
this.Close();
}
我哪里出错了?
最佳答案
在 Form2
上声明 RentalId
属性。在 CarRental
表单(您的第一个表单)中执行以下操作:
using(Form2 form2 = new Form2())
{
if (fomr2.ShowDialog() != DialogResult.OK)
return;
listBox.Items.Add(form2.RentalId);
}
以这种方式实现 Fomr2.RentalId
属性:
public string RentalId
{
get { return idRental.Text; } // you don't need ToString() call
}
然后选择“接受”按钮并将其 DialogResult
属性设置为 OK
。因此,单击该按钮将关闭对话框表单并返回 DialogResult.OK
。
关于c# - (c# + windows 窗体) 将项目添加到不同类中的 listBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13439624/