VB.NET ComboBox 验证值是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35157860/
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 19:42:24  来源:igfitidea点击:

VB.NET ComboBox Verify if Value Exists

vb.netcomboboxvb.net-2010verify

提问by Charles Rutherford

I am currently working on an application that was coded in VB. I am making modifications and adding features to it.

我目前正在开发一个用 VB 编码的应用程序。我正在对其进行修改并添加功能。

The issue I have is that I want to run a verification check for the ComboBox based on if the value exists before I attempt to select it.

我遇到的问题是,在尝试选择它之前,我想根据该值是否存在对 ComboBox 运行验证检查。

The combo box is populated from a sql query with a dictionary data source

组合框由带有字典数据源的 sql 查询填充

Dim TimerComboSource As New Dictionary(Of String, String)()
TimerComboSource.Add(varSQLReader("ID").ToString, varSQLReader("Name").ToString)
'Binds the values to the comboboxes
cmbTimer.DataSource = New BindingSource(TimerComboSource, Nothing)
cmbTimer.DisplayMember = "Value"
cmbTimer.ValueMember = "Key"

I select a value from a different ComboBox which is populated with a different SQL Query/SQL Table.

我从不同的 ComboBox 中选择一个值,该 ComboBox 填充了不同的 SQL 查询/SQL 表。

When I select the second ComboBox value, the table it comes from contains the ID of the first ComboBox. I want to verify if the Value Exists in the first ComboBox before I select it.

当我选择第二个 ComboBox 值时,它来自的表包含第一个 ComboBox 的 ID。我想在选择它之前验证第一个 ComboBox 中是否存在该值。

The following does not work:

以下不起作用:

If cmbTechnician.Items.Contains(varSQLReader("Tech_ID").ToString) Then
    cmbTechnician.SelectedValue = varSQLReader("Tech_ID").ToString
End If

Is there a specific way in VB to make this work without it being overly complicated? The other work around would to make a more complicated SQL query but I rather not do that if there's a simpler way.

在 VB 中是否有一种特定的方法可以使这项工作不会过于复杂?另一种解决方法是进行更复杂的 SQL 查询,但如果有更简单的方法,我宁愿不这样做。

回答by user3697824

Since you are using a BindingSourceon a Dictionary, you should use the DataSourceto determine of things exist. If you tried to add to cmbTimer.Itemsor delete from it directly, you'd get an error telling you to use the DataSource. So do the same for checking if something exists (dont use a dictionary with local scope):

由于您BindingSource在字典上使用 a ,您应该使用DataSource来确定事物是否存在。如果您尝试cmbTimer.Items直接向其中添加或删除,则会收到错误消息,提示您使用DataSource. 所以做同样的事情来检查是否存在(不要使用具有本地范围的字典):

' form or class level collection
Private cboSource As New Dictionary(Of String, String)

cboSource.Add("red", "red")
cboSource.Add("blue", "blue")
cboSource.Add("green", "green")

cbo.DataSource = New BindingSource(cboSource, Nothing)
cbo.DisplayMember = "Value"
cbo.ValueMember = "Key"

If cbo.Items.Contains("red") Then
    Console.Beep()     ' wont hit
End If

If cboSource.ContainsValue("red") Then
    Console.Beep()     ' hits!
End If

The suggestion in comments suggests casting the DataSourceof the BindingSourceback to dictionary:

评论中的建议建议将DataSourceBindingSource返回到字典:

Dim tempBS As BindingSource = CType(cbo.DataSource, BindingSource)
Dim newCol As Dictionary(Of String, String) = CType(tempBS.DataSource, Dictionary(Of String, String))

If newCol.ContainsValue("red") Then
    Console.Beep()     ' hits!
End If

It is easier and more direct to retain a reference to the dictionary, but recreating it will work.

保留对字典的引用更容易和更直接,但重新创建它会起作用。

回答by Jze

Here are another way =>

这是另一种方式=>

 If cmbTechnician.FindString(varSQLReader("Tech_ID").ToString) <= -1 Then
         'Do Not Exist 
 End If

This will find the display member and will return an index of the row if there exist value, otherwise will return -1.

这将找到显示成员,如果存在值,将返回该行的索引,否则将返回 -1。

More Info are here=>ComboBox.FindString

更多信息在这里=> ComboBox.FindString