【发布时间】:2021-07-08 00:53:08
【问题描述】:
我有两个表单,HomeForm 和 AddForm。 AddForm 有五个文本框,registrationNumber、carMake、carModel、carYear 和 carHireCost。我已经创建了按预期将信息发送到列表框的代码。这看起来像这样:
HomeForm f1 = (HomeForm)Application.OpenForms["HomeForm"];
f1.UpdateListBox(registrationNumber.Text + " " + carMake.Text + " " + carModel.Text + " " + carYear.Text + " " + carHireCost.Text);
this.Hide();
我希望能够检查注册号是否已经存在。如果是这样,它将引发错误并且不会更新列表框。如果它是唯一的注册号,它会将其添加到列表中,以及其他详细信息。在尝试更新列表之前如何编写检查唯一性的语句?
例如:
if ("check registration exists = does exist"){
MessageBox.Show("Registration Number already exists.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}else{
HomeForm f1 = (HomeForm)Application.OpenForms["HomeForm"];
f1.UpdateListBox(registrationNumber.Text + " " + carMake.Text + " " + carModel.Text + " " + carYear.Text + " " + carHireCost.Text);
this.Hide();
}
编辑 我的主表单中有一个更新列表框的方法,其中 vehicleList 是列表框的名称。它看起来像这样:
public void UpdateListBox(string lstValue)
{
vehicleList.Items.Add(lstValue);
}
【问题讨论】:
-
这可以通过多种方式完成。您可以在 HomeForm 中有一个方法可以检查列表框是否具有传入的注册号,您可以有第三个类将注册号添加到您可以检查的集合中,等等。
-
代码
f1.UpdateListBox在做什么?您应该显示此代码。您应该edit您的问题提供有关代码当前如何将项目添加到ListBox.的更多详细信息 -
使用表单实例。查看我的两个表单项目:stackoverflow.com/questions/34975508/…
-
我已经用更新列表框的主表单中的方法更新了我的问题