【问题标题】:Select items from one ListBox into another从一个 ListBox 中选择项目到另一个
【发布时间】:2015-10-13 08:38:33
【问题描述】:

我有两个 ListBoxes listbox1 和 listbox2。我想获取 ListBox1 中的所有重复项,我将在 TextBox 中搜索并放入 ListBox2 中,当我要搜索的所有重复项都在 ListBox2 中时,它会自动计数,请帮助我。

例如,ListBox1 中的项目

DOG
DOG
DOG
CAT
CAT

当我在 TextBox 中键入 DOG 时,ListBox1 中的所有 DOG 将被复制到 ListBox2。我该怎么做?

我试过了

Dim check As Boolean
For Each item In ListBox1.Items
    check = ListBox1.FindStringExact(item)
    ListBox2.Items.Add(item)
Next

我也尝试过这个,但它是错误的,它是我要搜索的确切单词之前的行。 例如 狗 狗 狗 猫 猫 我会在文本框中搜索 CAT,listbox2 中的输出是 3 这是我的代码:

暗检查为字符串

    check = ListBox1.FindStringExact(TextBox1.Text)
    ListBox2.Items.Add(check)

【问题讨论】:

    标签: vb.net listbox


    【解决方案1】:
    listBox2.Items.AddRange(listBox1.Items.Cast(Of ListItem)().Where(Function(x) x.Text = TextBox1.Text ).ToArray(Of ListItem)())
    

    【讨论】:

    • 是的,先生,我在 listitem 中有一个错误,我该怎么办?
    • 先生,现在可以了。非常感谢先生的帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!! :D
    【解决方案2】:

    在文本框中输入值后单击按钮尝试以下代码。

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim searchFor As String = TextBox1.Text
        For Each item In ListBox1.Items
            If item = searchFor Then
                ListBox2.Items.Add(item)
            End If
        Next
    
        Do While ListBox1.Items.Contains(searchFor)
            ListBox1.Items.Remove(searchFor)
        Loop
    End Sub
    

    【讨论】:

    • 但是先生可以吗?复制不删除listbox1 中的项目?到listbox2?
    • 只需删除 Do While 代码。如果这解决了您的问题,请将其标记为答案。
    • 先生,现在可以了。非常感谢您的帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多