vb.net 在 DevExpress CheckedComboBox 中获取选中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22103003/
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
Get Checked Items In A DevExpress CheckedComboBoxEdit
提问by Tim
I am using the DevExpress 9.3 CheckedComboBoxEdit, and I need to get the collection of all checked items. It seems like this should be a simple task, but the closest thing I have found to a solution is something that says I can use:
我正在使用 DevExpress 9.3 CheckedComboBoxEdit,我需要获取所有选中项的集合。看起来这应该是一个简单的任务,但我发现的最接近解决方案的是我可以使用的东西:
CheckedComboBoxEdit.Properties.GetItems.GetCheckedValues()
Unfortunately, there is no GetCheckedValues method here. I have found the following:
不幸的是,这里没有 GetCheckedValues 方法。我发现了以下内容:
CheckedComboBoxEdit.Properties.GetCheckedItems()
which returns an object, but I cannot find any reference on what I should cast the object as. I have also tried to iterate through the items, and check each one to see if it is checked, following the suggestion from here, but Items returns a collection of Strings, not CheckedListBoxItem, so I cannot test if they are checked.
它返回一个对象,但我找不到任何关于我应该将对象转换为什么的参考。我还尝试遍历项目,并按照此处的建议检查每个项目是否已选中,但 Items 返回字符串集合,而不是 CheckedListBoxItem,因此我无法测试它们是否已选中。
What I want is a String collection of checked items; right now, I am okay to receive them as any type of collection, or even create the collection myself. I know there must be some very simple thing that I am overlooking, but I can't seem to find it.
我想要的是一个检查项目的字符串集合;现在,我可以将它们作为任何类型的收藏来接收,甚至可以自己创建收藏。我知道一定有一些非常简单的东西被我忽略了,但我似乎无法找到它。
SOLUTION
解决方案
This is the solution that I came up with. I would prefer something more elegant; it seems like there should be a way to get the checked items, since this is what the control is for. Nevertheless, this seems to work:
这是我想出的解决方案。我更喜欢更优雅的东西;似乎应该有一种方法可以获取选中的项目,因为这是控件的用途。尽管如此,这似乎有效:
Private Function GetChecked() As List(Of String)
Dim checked As New List(Of String)
Dim checkedString As String = CType(SitePickerControl.Properties.GetCheckedItems(), String)
If (checkedString.Length > 0) Then
checked.AddRange(checkedString.Split(New Char() {","c}))
End If
Return checked
End Function
If anyone can give me a proper solution, I would love to see it.
如果有人能给我一个适当的解决方案,我很乐意看到它。
回答by bpiec
This is what I use:
这是我使用的:
var ids = (from CheckedListBoxItem item in checkedComboBoxEdit.Properties.Items
where item.CheckState == CheckState.Checked
select (int)item.Value).ToArray();
You can also make an extension method on CheckedListBoxItemwhich will return only checked items values.
您还可以创建一个扩展方法,CheckedListBoxItem该方法将仅返回选中的项目值。
(It's C#, not VB, but the concept is the same.)
(它是 C#,不是 VB,但概念是一样的。)
回答by Human Wannabe
I know this is an old post, but I thought I should pitch in anyway.
我知道这是一个旧帖子,但我认为无论如何我都应该投入。
I'm not sure on when v9.3 was released, but there definitely is a GetCheckedValues() function now. It is described here:
我不确定 v9.3 何时发布,但现在肯定有一个 GetCheckedValues() 函数。它在这里描述:
and I also found it as the answer in one of their support cases (which is much older than this post) here:
我还在他们的一个支持案例(比这篇文章早得多)中找到了答案:
https://www.devexpress.com/Support/Center/Question/Details/Q431364
https://www.devexpress.com/Support/Center/Question/Details/Q431364
So to get a list of all the selected values you need something like:
因此,要获取所有选定值的列表,您需要类似以下内容:
myCombo.Properties.GetItems().GetCheckedValues()
or to check if a specific value was selected:
或检查是否选择了特定值:
if (myCombo.Properties.GetItems().GetCheckedValues().contains("myvalue"))
I hope this helps future searchers.
我希望这有助于未来的搜索者。

