如何将组合框值与列表框中的项目进行比较 - vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21221099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:29:22  来源:igfitidea点击:

How to compare combobox value with items in listbox - vb.net

vb.netcomboboxlistbox

提问by SellaKumar

I have a combo box and a listbox. All i want is, when i select a value(text) from the combobox to check if there is a same value(same text) at the listbox and if there is, then msgbox should display as "DATA FOUND"

我有一个组合框和一个列表框。我想要的只是,当我从组合框中选择一个值(文本)以检查列表框中是否存在相同的值(相同文本)时,如果存在,则 msgbox 应显示为“已找到数据”

i tried this code, but its not working

我试过这段代码,但它不起作用

Dim i As Integer

Dim i 作为整数

    For i = 0 To ListBox1.Items.Count - 1
        If ComboBox1.SelectedItem = ListBox1.ValueMember Then
            MsgBox("DATA FOUND")
        End If

    Next i

Thanks in advance....

提前致谢....

回答by Steve

You are using two properties with a different meaning for your comparison. SelectedItemis an object (could be anything depending on how you have filled the combo, ValueMemberis just the name of a property to use as the actual value for the items in the ListBox.

您正在使用具有不同含义的两个属性进行比较。 SelectedItem是一个对象(可以是任何对象,具体取决于您如何填充组合,ValueMember只是用作 ListBox 中项目实际值的属性名称。

However the two classes (ListBox and ComboBox) share the same pattern for storing their list items, so supposing that both are populated using a list of strings then your code could be

然而,这两个类(ListBox 和 ComboBox)共享相同的模式来存储它们的列表项,因此假设两者都使用字符串列表填充,那么您的代码可能是

Dim curComboItem = ComboBox1.SelectedItem.ToString()
For i = 0 To ListBox1.Items.Count - 1
    If  curComboItem = ListBox1.Items(i).ToString() Then
        MsgBox("DATA FOUND")
        Exit For
    End If
Next i

回答by Daniel Charry

If ComboBox1.SelectedItem = ListBox1.ValueMember Then

should be

应该

If ComboBox1.SelectedItem = ListBox1.Items(i) Then

Notice that ComboBox1.SelectedItem only works for items inside of the Collection. You can extend this to any text by the .text parameter.

请注意, ComboBox1.SelectedItem 仅适用于 Collection 内的项目。您可以通过 .text 参数将此扩展到任何文本。

P.D.

PD

Next i '???

回答by T.S.

Using real objects in your ComboBoxand ListBoxcan often lead to better flexibility of your application.

在您的ComboBox和 中使用真实对象ListBox通常可以提高您的应用程序的灵活性。

For example, you have a car lot application, in which you have long list of available cars and you don't want to browse the entire list - you use ComboBox to select make and model, and do something with the items in your list box.

例如,您有一个停车场应用程序,其中有很长的可用汽车列表,但您不想浏览整个列表 - 您使用 ComboBox 选择品牌和型号,并对列表框中的项目执行某些操作.

(in pseudo-code)

(在伪代码中)

Your car object.

你的汽车对象。

class Car
    ModelId
    ModelMake
    ModelName
    FullName = ModelMake & " " & ModelName
End Class

class AvailableCar Inherits Car
    IsOnTheLot
    VIN
    Price
    'etc
End Class

In your Form class

在你的表单类中

comboCarMakes.DataSource = GetListOfMakesOfCars() ' List of Car classes
comboCarMakes.ValueMember = "ModelId"
comboCarMakes.DisplayMember = "FullName" 

listAvbailableCars.DataSource = GetListOfAvailableCars() ' List of AvailableCar classes
listAvbailableCars.ValueMember = "VIN"
listAvbailableCars.DisplayMember = "FullName" 

Sub comboCarMakes_SelectedIndexChanged


    Dim car as Car = DirectCast(comboCarMakes.SelectedItem, Car)
    For i = 0 To listAvbailableCars.Items.Count - 1
        If  car.ModelId = DirectCast(listAvbailableCars.Items(i), AvailableCar).ModelId Then
            ' Do something
        End If
    Next
End Sub

Advantage - you have lots of information available immediately.

优势 - 您可以立即获得大量信息。