使用 vb.net 遍历组合框控件的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10994279/
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
To iterate through the values of combo box control using vb.net
提问by Amulraj
I update my question here .. Am using a combo box with no of phone numbers .I want to get the phone no one by one in a variable. Now am using the below code to get the combobox values. But still now am getting the following error message System.Data.DataRowView. Please help me to fix this error. am new for vb.net.
我在这里更新我的问题..我使用的是一个没有电话号码的组合框。我想在一个变量中没有一个接一个地得到电话。现在我使用下面的代码来获取组合框值。但现在仍然收到以下错误消息 System.Data.DataRowView。请帮我解决这个错误。我是 vb.net 的新手。
My partial code is here ..
我的部分代码在这里..
For i = 0 To ComboBox1.Items.Count
Dim s As String
s = Convert.ToString(ComboBox1.Items(i))
Next i
回答by RonB
you are using an index which is zero based.
您正在使用基于零的索引。
change this:
改变这个:
For i = 0 To ComboBox1.Items.Count
to this:
对此:
For i = 0 To ComboBox1.Items.Count - 1
回答by Dennis Traub
Your problem probably happens here:
您的问题可能发生在这里:
s = Convert.ToString(ComboBox1.Items(i))
This doesn't return the value. It returns a string representation of the object at the given index, which in your case apparently is of type System.Data.DataRowView
.
这不会返回值。它返回给定索引处对象的字符串表示形式,在您的情况下,它显然是类型System.Data.DataRowView
。
You would have to cast ComboBox1.Items(i)
to the approbriate type and access its Value
. Or, since its a DataRowView
, you can access the values throgh the appropriate column names:
您必须转换ComboBox1.Items(i)
为适当的类型并访问其Value
. 或者,由于它是 a DataRowView
,您可以通过适当的列名访问值:
Dim row = CType(ComboBox1.Items(i), System.Data.DataRowView)
s = row.Item("column_name")
Nevertheless, first of all you should definitely close and disposethe connection, no matter whether the transaction fails or succeeds. This can be done in a finally
block (option 1) or with a using
statement (option 2).
尽管如此,首先你一定要关闭并处理连接,无论事务是失败还是成功。这可以在finally
块中(选项 1)或使用using
语句(选项 2)完成。
Option 1
选项1
// ...
con1 = New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
con1.Dispose()
End Try
Option 2
选项 2
// ...
Using con1 as New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
End Try
End using
回答by mkstrick
I could not find this answer online in its entirety but pieced it together. In the snippet below cbox is a ComboBox control that has the DisplayMember and ValueMember properties initialized.
我在网上找不到完整的答案,而是将其拼凑在一起。在 cbox 下面的片段中是一个 ComboBox 控件,它具有初始化的 DisplayMember 和 ValueMember 属性。
Dim itemIE As IEnumerator = cbox.Items.GetEnumerator
itemIE.Reset()
Dim thisItem As DataRowView
While itemIE.MoveNext()
thisItem = CType(itemIE.Current(), DataRowView)
Dim valueMember As Object = thisItem.Row.ItemArray(0)
Dim displayMember As Object = thisItem.Row.ItemArray(1)
' Insert code to process this element of the collection.
End While
回答by DanW52
This also works!
这也有效!
Dim stgTest = "Some Text"
Dim blnItemMatched As Boolean = False
'-- Loop through combobox list to see if the text matches
Dim i As Integer = 0
For i = 0 To Me.Items.Count - 1
If Me.GetItemText(Me.Items(i)) = stgTest Then
blnItemMatched = True
Exit For
End If
Next i
If blnItemMatched = False Then
Dim stgPrompt As String = "You entered '" & stgTypedValue & "', which is not in the list."
MessageBox.Show(stgPrompt, "Incorrect Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Text = ""
Me.Focus()
End If
回答by mzonerz
Even after long time back you will achieve this with simply by following
即使在很长一段时间之后,您只需按照以下步骤即可实现这一目标
For Each item As Object In combx.Items
readercollection.Add(item.ToString)
Next
回答by TobScada
Please try this
请试试这个
For j As Integer = 0 To CboCompany.Items.Count - 1
Dim obj As DataRowView = CboCompany.Items(j)
Dim xx = obj.Row(0)
If xx = "COMP01" Then
CboCompany.SelectedIndex = j
Exit For
End If
Next