从 CheckedListBox VB.NET Winform 中检索选中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17576340/
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
Retrieved the checked items from a CheckedListBox VB.NET Winform
提问by user2103670
I asked this question before but suggested to provide a more details. Here is the problem:
我之前问过这个问题,但建议提供更多细节。这是问题所在:
I have a CheckedListBox called
CheckedList_Facility
. All items in thisCheckedList_Facility
are getting from SQL Server Datasource. All items are loaded properly using the below codeDim queryString As String = "SELECT Facility FROM Database.dbo.Facility " Dim connection As New SqlConnection(connectionString) Dim command As New SqlCommand(queryString, connection) connection.Open() Dim dataReader As SqlDataReader = command.ExecuteReader() Dim source As New BindingSource source.DataSource = dataReader CheckedList_Facility.DataSource = source CheckedList_Facility.ValueMember = "Facility" connection.Close()
I would like to get a list of items that are checked. For example,
[X] AAA
[X] BBB
[ ] CCC
[ ] DDD
[X] EEE
我有一个名为的 CheckedListBox
CheckedList_Facility
。此中的所有项目CheckedList_Facility
均来自 SQL Server 数据源。使用以下代码正确加载所有项目Dim queryString As String = "SELECT Facility FROM Database.dbo.Facility " Dim connection As New SqlConnection(connectionString) Dim command As New SqlCommand(queryString, connection) connection.Open() Dim dataReader As SqlDataReader = command.ExecuteReader() Dim source As New BindingSource source.DataSource = dataReader CheckedList_Facility.DataSource = source CheckedList_Facility.ValueMember = "Facility" connection.Close()
我想获得已检查项目的列表。例如,
[X] AAA
[X] BBB
[ ] CCC
[ ] 滴滴
[X] 电子电气设备
then the list should be "AAA", "BBB", "EEE"
那么列表应该是“AAA”、“BBB”、“EEE”
To test if the item is retrieved correctly, I using a button call
bt_GetItem
and when this button is pressed, a msgbox displays the items that are checked. With this code:Dim itemChecked As Object For Each itemChecked In CheckedList_Facility.CheckedItems MsgBox(itemChecked.ToString) Next
However, I only receive this error message
System.Data.Common.DataRecordInternal
为了测试项目是否被正确检索,我使用了一个按钮调用
bt_GetItem
,当这个按钮被按下时,一个 msgbox 显示被检查的项目。使用此代码:Dim itemChecked As Object For Each itemChecked In CheckedList_Facility.CheckedItems MsgBox(itemChecked.ToString) Next
但是,我只收到此错误消息
System.Data.Common.DataRecordInternal
Technically, this might not be an error but instead of receiving "AAA", I get this
从技术上讲,这可能不是错误,但我没有收到“AAA”,而是收到了这个
System.Data.Common.DataRecordInternal
回答by Winks
Because you bound the checkedlistbox to your datareader, the checked object internally is actually a {System.Data.Common.DataRecordInternal}
and not a string or any other native object.
You have to access the item
property within the object to get to the string you want, like so:
因为您将checkedlistbox 绑定到您的datareader,所以内部的checked 对象实际上是a{System.Data.Common.DataRecordInternal}
而不是字符串或任何其他本机对象。您必须访问item
对象内的属性才能获得所需的字符串,如下所示:
MsgBox(itemChecked.item("Facility").ToString)
回答by Alex
In order for your "AAA" (type of string) to be displayed you must access the itemChecked
object's property. Since you're selecting "Facility" we'll use that. The message you were receiving (System.Data.Common.DataRecordInternal) was the object itemChecked
type.
为了显示您的“AAA”(字符串类型),您必须访问itemChecked
对象的属性。由于您选择了“设施”,我们将使用它。您收到的消息(System.Data.Common.DataRecordInternal)是对象itemChecked
类型。
MsgBox(itemChecked.Items("Facility").ToString)
Should output "AAA"
应该输出“AAA”
回答by Mateus Campos
For i As Integer = 0 To lbSit.Items.Count - 1
If CType(lbSit.Items(i), DataRowView)(lbSit.ValueMember).ToString = "32" Then
lbSit.SetItemChecked(i, True)
End If
Next
回答by 15464
Private Sub WhatIsChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WhatIsChecked.Click
' Display in a message box all the items that are checked.
Dim indexChecked As Integer
Dim itemChecked As Object
Const quote As String = """"
' First show the index and check state of all selected items.
For Each indexChecked In CheckedListBox1.CheckedIndices
' The indexChecked variable contains the index of the item.
MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + _
CheckedListBox1.GetItemCheckState(indexChecked).ToString() + ".")
Next
' Next show the object title and check state for each item selected.
For Each itemChecked In CheckedListBox1.CheckedItems
' Use the IndexOf method to get the index of an item.
MessageBox.Show("Item with title: " + quote + itemChecked.ToString() + quote + _
", is checked. Checked state is: " + _
CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".")
Next