如何在检查唯一值的同时将数据从一种形式插入到另一种形式的列表框中?
How do you insert data from one form into a listbox in another form while checking for unique values?
我有两个表单,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);
}
您可以在 ListBox
的 Items
集合上调用 Contains
方法来检查 ListBox
是否已经包含条目。
public void UpdateListBox(string lstValue)
{
if(vehicleList.Items.Contains(lstValue)
{
return; // already in list, do not add again
}
vehicleList.Items.Add(lstValue);
}
既然你要查询“注册号”是否已经在列表中,那么,按照目前的情况,你有两种选择……
- 从传入的变量
lstValue
中解析出注册号,然后循环遍历每个列表项,查看是否包含注册号。
或
- 将注册号作为单独的变量传入。然后循环遍历列表中的每一项,看该项是否包含注册号。
使用第二种方法可能类似于……
public void UpdateListBox(string regNumber, string lstValue) {
foreach (string item in vehicleList.Items) {
if (item.Contains(regNumber)) {
MessageBox.Show("Registration number: " + regNumber + " already exist in the list! Item not added!");
return;
}
}
vehicleList.Items.Add(regNumber + " " + lstValue);
}
我有两个表单,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);
}
您可以在 ListBox
的 Items
集合上调用 Contains
方法来检查 ListBox
是否已经包含条目。
public void UpdateListBox(string lstValue)
{
if(vehicleList.Items.Contains(lstValue)
{
return; // already in list, do not add again
}
vehicleList.Items.Add(lstValue);
}
既然你要查询“注册号”是否已经在列表中,那么,按照目前的情况,你有两种选择……
- 从传入的变量
lstValue
中解析出注册号,然后循环遍历每个列表项,查看是否包含注册号。
或
- 将注册号作为单独的变量传入。然后循环遍历列表中的每一项,看该项是否包含注册号。
使用第二种方法可能类似于……
public void UpdateListBox(string regNumber, string lstValue) {
foreach (string item in vehicleList.Items) {
if (item.Contains(regNumber)) {
MessageBox.Show("Registration number: " + regNumber + " already exist in the list! Item not added!");
return;
}
}
vehicleList.Items.Add(regNumber + " " + lstValue);
}