vb.net 如何从 Check List VB 获取已检查项目的列表。网络Winform
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17557199/
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
How to get a list of checked item from Check List VB. Net Winform
提问by user2103670
After several hours of trying, I was able to get a Check List binded to SQL source. However, now I would like to retrieve all of the items in the check list that are checked. I tried the above code but it does not work
经过几个小时的尝试,我能够得到一个绑定到 SQL 源的检查列表。但是,现在我想检索检查列表中已检查的所有项目。我试过上面的代码,但它不起作用
For i As Integer = 0 To checkList_Facility.Items.Count - 1
If (checkList_Facility.GetItemChecked(i)) = True Then
MsgBox(checkList_Facility.Items(i))
End If
Next
Binding Code:
绑定代码:
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 var As New DataTable
Dim source As New BindingSource
source.DataSource = dataReader
CheckList_Facility.DataSource = source
CheckList_Facility.ValueMember = "Facility"
connection.Close()
回答by shahkalpesh
How about using CheckedItemscollectioninstead?
改用CheckedItems集合怎么样?
EDIT:
编辑:
dim itemChecked as Object
For Each itemChecked In checkList_Facility.CheckedItems
MsgBox(itemChecked.ToString())
Next
回答by Neolisk
CheckedItemsseems to be a good option. Here is how it works. For simplicity, suppose your underlying business object (DataSource) is a Dictionary(Of String, String):
CheckedItems似乎是一个不错的选择。下面是它的工作原理。为简单起见,假设您的底层业务对象 (DataSource) 是一个Dictionary(Of String, String):
Dim dict As New Dictionary(Of String, String)
With dict
.Add("1", "One")
.Add("2", "Two")
.Add("3", "Three")
End With
And you have the following binding:
你有以下绑定:
With CheckedListBox1
.DataSource = dict.ToList
.ValueMember = "Key"
.DisplayMember = "Value"
End With
It seems that Microsoft do not want us to use this approach, probably due to compatibility issues. You know this because .DataSourceand other members are hidden in intellisense. However, for simple setup such as the above, it works, just don't rely on intellisense in typing these members.
微软似乎不希望我们使用这种方法,可能是因为兼容性问题。你知道这一点是因为.DataSource其他成员隐藏在智能感知中。然而,对于像上面这样的简单设置,它是有效的,只是不要依赖智能感知来输入这些成员。
Now let's say you have a button with the following code in it:
现在假设您有一个包含以下代码的按钮:
Dim v = CheckedListBox1.CheckedItems
In this case vis a collection of underlying business objects, for the above it will be collection of KeyValuePair(Of String, String).
在这种情况下v是底层业务对象的集合,对于上述情况,它将是KeyValuePair(Of String, String).
In your case your business object is of type DataRecordInternal, so you have to expect this type to be returned and account for it in code. Note that it may not have a goodbehavior of ToString(), so this will not work as you expect it:
在您的情况下,您的业务对象是 type DataRecordInternal,因此您必须期望返回此类型并在代码中对其进行说明。请注意,它可能没有良好的行为ToString(),因此这不会像您期望的那样工作:
MsgBox(checkList_Facility.Items(i))
I believe you are looking to do some processing instead, messaging a user about every checked item is not very useful. So instead you may want to have a different code, such as:
我相信您希望进行一些处理,而不是向用户发送有关每个已检查项目的消息并不是很有用。因此,您可能希望使用不同的代码,例如:
Dim record As DataRecordInternal = checkList_Facility.Items(i)
'do some processing on record variable

