首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不重复前面的文本的情况下从CheckedListBox获得项目文本到ListBox?VB.net

如何在不重复前面的文本的情况下从CheckedListBox获得项目文本到ListBox?VB.net
EN

Stack Overflow用户
提问于 2020-11-18 14:17:17
回答 2查看 572关注 0票数 0

我有一个由10个名字组成的CheckedListBox A到J(这是一个字符串,但这里我只是使用abc),我想做它,这样每次用户检查某个东西时,文本都会同时显示在列表框中。我试过这样做:

代码语言:javascript
运行
复制
For i = 0 To chklistbxDrugAvailableList.Items.Count - 1
            Dim drugs As String = CType(chklistbxDrugAvailableList.Items(i), String)
            If chklistbxDrugAvailableList.GetItemChecked(i) Then
                listbxYourOrder.Items.Add(drugs)
            End If
           'drugs = nothing
Next

但是,当我检查A、B、C时,列表框中的文本被重复如下:

代码语言:javascript
运行
复制
A
A
B
A
B
C

我不知道为什么会这样。我没有将drugs声明为数组。即使我使用drugs = nothing,它仍然会给出重复的值。

我该怎么办?抱歉,如果这是个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-18 14:49:00

以下是我要做的事情:

代码语言:javascript
运行
复制
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    'Clear the current ListBox contents.
    ListBox1.Items.Clear()

    'Get the indexes of the currently checked items.
    Dim checkedIndices = CheckedListBox1.CheckedIndices.Cast(Of Integer)()

    'Add the current index to the list if the item is being checked, otherwise remove it.
    checkedIndices = If(e.NewValue = CheckState.Checked,
                        checkedIndices.Append(e.Index),
                        checkedIndices.Except({e.Index}))

    'Get the checked items in order and add them to the ListBox.
    ListBox1.Items.AddRange(checkedIndices.OrderBy(Function(n) n).
                                           Select(Function(i) CheckedListBox1.Items(i)).
                                           ToArray())
End Sub

或者像这样:

代码语言:javascript
运行
复制
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    'Get the indexes of the currently checked items.
    Dim checkedIndices = CheckedListBox1.CheckedIndices.Cast(Of Integer)()


    'Add the current index to the list if the item is being checked, otherwise remove it.
    checkedIndices = If(e.NewValue = CheckState.Checked,
                        checkedIndices.Append(e.Index),
                        checkedIndices.Except({e.Index}))

    'Get the checked items in order and add them to the ListBox.
    ListBox1.DataSource = checkedIndices.OrderBy(Function(n) n).
                                         Select(Function(i) CheckedListBox1.Items(i)).
                                         ToArray()
End Sub

区别在于,由于第二个选项使用数据绑定,默认情况下将选择ListBox中的第一个项。

需要有点冗长的原因是,ItemCheck事件是在选中或未选中项之前引发的。因此,我们需要先获得当前选中的项,然后添加或删除当前项,这取决于当前项是选中还是未选中。

编辑:

这里有一个不需要清除ListBox的选项

代码语言:javascript
运行
复制
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    Dim item = CheckedListBox1.Items(e.Index)

    If e.NewValue = CheckState.Checked Then
        ListBox1.Items.Add(item)
    Else
        ListBox1.Items.Remove(item)
    End If
End Sub

这样做的问题是,项目将以检查它们的顺序出现在ListBox中,而不是它们在CheckedListBox中的出现顺序。其他选项将两个列表中的项目保持相同的顺序。

票数 0
EN

Stack Overflow用户

发布于 2020-11-18 18:36:08

如果我理解你的问题,我会这样做:

代码语言:javascript
运行
复制
   lstbxYourOrder.Items.Clear()
   For Each l As String In chklistbxDrugAvailableList.CheckedItems
         listbxYourOrder.Items.Add(l)
   Next
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64894864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档