【问题标题】:Removing items from a ListBox in VB.net从 VB.net 中的 ListBox 中删除项目
【发布时间】:2017-07-20 02:31:07
【问题描述】:

我有两个ListBox1ListBox2。我已通过选择 ListBox1 项目将项目插入到具有以下代码的 ListBox2 中:

da6 = New SqlDataAdapter("select distinct(component_type) from component where   component_name='" & ListBox1.SelectedItem() & "'", con)
da6.Fill(ds6, "component")
For Each row As DataRow In ds6.Tables(0).Rows
    ListBox2.Items.Add(row.Field(Of String)("component_type"))
Next

但是当我重新选择 ListBox1 的另一个项目时,ListBox2 会显示预加载项目和现在一起加载的项目。 我只想现在加载的项目显示在列表框中。 我使用了这段代码,但问题没有解决:

For i =0 To ListBox2.items.count - 1
    ListBox2.Items.removeAt(i)
Next

listbox2.items.clear() 也不起作用..

如何清除ListBox2中的所有项目?

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    简单使用:

    ListBox2.Items.Clear()
    
    • 要考虑上一次编辑:添加新项目之前执行此操作

    MSDN:ListBox.ObjectCollection.Clear

    从集合中删除所有项目。

    请注意,您的方法的问题是RemoveAt 更改了所有剩余项目的索引。

    当您从列表中删除一个项目时,索引会更改为 列表中的后续项目。有关已删除项目的所有信息 被删除。您可以使用此方法从 通过指定要从列表中删除的项目的索引来列出。至 指定要删除的项目而不是项目的索引,使用 删除方法。 要从列表中删除所有项目,请使用清除 方法

    如果你还是想使用RemoveAt,你可以倒退,例如:

    for-loop:

    For i As Int32 = ListBox2.Items.Count To 0 Step -1
        ListBox2.Items.RemoveAt(i)
    Next
    

    while

    While ListBox2.Items.Count > 0
        ListBox2.Items.RemoveAt(ListBox2.Items.Count - 1)
    End While
    

    旧 C# 代码

    for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        ListBox2.Items.RemoveAt(i);
    
    while(ListBox2.Items.Count > 0)
        ListBox2.Items.RemoveAt(ListBox2.Items.Count - 1);
    

    【讨论】:

    • 请注意,包含项目的属性是Items
    • @user1945423:抱歉,由于我目前在 C# 中使用(和测试),所以我已经切换了答案中的语言;)编辑了我的答案。
    • 那么我如何从数据集中添加列表框并从列表框中清除项目
    • @user1945423:我假设您首先从新选择的项目中添加了新项目,然后删除了所有项目。当然那是行不通的。您需要先清除 ListBox 中的所有项目,然后添加新项目。
    • @Tim Schmelter 是的,我做到了,但是当我重新选择另一个项目时,listbox2 显示之前加载的项目和现在加载的项目
    【解决方案2】:

    这段代码对我有用:

    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

    【讨论】:

      【解决方案3】:

      如果你只想清除列表框,你应该使用 Clear (winforms | wpf | asp.net) 方法:

      ListBox2.Items.Clear()
      

      【讨论】:

      • 那么我如何从数据集中添加列表框并从列表框中清除项目
      【解决方案4】:

      有一种简单的方法可以删除选定的项目,而所有这些人都在寻求一种硬方法:

      lstYOURVARIABLE.Items.Remove(lstYOURVARIABLE.SelectedItem)
      

      我在 Visual Studio 的 Visual Basic 模式下使用了它。

      【讨论】:

        【解决方案5】:

        这是我想出的从列表框中删除用户选择的项目的代码它似乎在多选列表框中工作正常(selectionmode 属性设置为多扩展)。:

        Private Sub cmdRemoveList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRemoveList.Click
            Dim knt As Integer = lstwhatever.SelectedIndices.Count
            Dim i As Integer
            For i = 0 To knt - 1
                lstwhatever.Items.RemoveAt(lstwhatever.SelectedIndex)
            Next
        End Sub
        

        【讨论】:

          【解决方案6】:

          我已经测试过了,效果很好

          For i =0 To ListBox2.items.count - 1
          ListBox2.Items.removeAt(0)
          Next
          

          【讨论】:

          • 它会删除整个列表框:)
          【解决方案7】:

          我认为您的 ListBox 已经用 ListBox2.Items.Clear() 清除了。问题是您还需要使用 ds6.Tables.Clear() 从以前的结果中清除数据集。

          在你的代码中添加这个:

          da6 = New SqlDataAdapter("select distinct(component_type) from component where   component_name='" & ListBox1.SelectedItem() & "'", con)
              ListBox1.Items.Clear()    ' clears ListBox1
              ListBox2.Items.Clear()    ' clears ListBox2
              ds6.Tables.Clear()        ' clears DataSet  <======= DON'T FORGET TO DO THIS
          da6.Fill(ds6, "component")
          For Each row As DataRow In ds6.Tables(0).Rows
              ListBox2.Items.Add(row.Field(Of String)("component_type"))
          Next
          

          【讨论】:

            【解决方案8】:

            这对我有用。

            Private Sub listbox_MouseDoubleClick(sender As Object, e As MouseEventArgs) 
                    Handles listbox.MouseDoubleClick
                    listbox.Items.RemoveAt(listbox.SelectedIndex.ToString())
                End Sub
            

            【讨论】:

              【解决方案9】:
                 Dim ca As Integer = ListBox1.Items.Count().ToString
                  While Not ca = 0
                      ca = ca - 1
                      ListBox1.Items.RemoveAt(ca)
                  End While
              

              【讨论】:

              • 这是 OP 无法使用的相同代码。你试过了吗?
              • 你要返回一个 ToString() 的整数?
              • @Bujutsu 不是同一个代码,因为迭代方向不同。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-07-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-03-08
              相关资源
              最近更新 更多