在CheckedListBox中搜索时检查选中的项目

时间:2013-07-19 14:34:50

标签: vb.net checkedlistbox

我在使用VB时遇到了一些问题。

我正在尝试创建代码,以便在您搜索时将选中的项目保留在CheckedListBox中。我有一个存储已检查项目名称的数组,但是当您使用CkeckedListBox.SetCheckedItem()方法时,您需要使用索引,并且每次我在列表框中搜索时都会更改索引。

这是我到目前为止所做的:

Dim checkeditems(1000) As String
If txtSearch.Text.Length = 1 Then
    For i = 0 To 1000
        If lstVerktyg.CheckedItems.Item(i) = "" Then
            Exit For
        End If
        checkeditems(i) = lstVerktyg.CheckedItems.Item(i)
    Next
End If

Dim Connection As New MySqlConnection("server=" & My.Settings.Host & ";user id=" & My.Settings.User & "; password=" & My.Settings.Pass & "; port=3306; database=" & My.Settings.DB & "; pooling=false")
Try
    Connection.Open()
Catch ex As MySqlException
    Exit Sub
End Try
Dim Reader As MySqlDataReader
Dim Query As MySqlCommand
Dim ResultsNumber As Integer = 0
lstVerktyg.Items.Clear()

Query = New MySqlCommand("SELECT `namn` FROM `verktyg` WHERE `namn` LIKE '%" & txtSearch.Text.Replace("'", "\'") & "%' LIMIT 300;", Connection)
Reader = Query.ExecuteReader()

While (Reader.Read())
    lstVerktyg.Items.Add(Reader.GetString("namn"))
End While

If Reader IsNot Nothing Then Reader.Close()
For i = 0 To 1000
    If checkeditems(i) = "" Then
        Exit For
    End If
    If lstVerktyg.GetItemChecked(i) Then

    End If
Next

我该怎么办?

1 个答案:

答案 0 :(得分:0)

这是一个如何做到这一点的简单示例。假设您最初使用某些项填充CheckedListBox控件,如下所示:

CheckedListBox1.Items.AddRange({"Item 1", "Item 2", "Item 3"})

然后,您显示表单并让用户检查他们选择的任何项目。然后,当您要重新加载列表并保留选中的项目时,您可以这样做。首先,您要创建所有已检查项目的列表:

Dim checkedItems As New List(Of Object)(CheckedListBox1.CheckedItems.Count)
For Each i As Object In CheckedListBox1.CheckedItems
    checkedItems.Add(i)
Next

然后,您可以安全地清除CheckedListBox

中的项目
CheckedListBox1.Items.Clear()

然后,您可以使用刷新的数据重新加载列表,例如:

CheckedListBox1.Items.AddRange({"Item 1", "Item 2", "Item 3", "Item 4"})

然后,您可以遍历CheckedListBox中的项目并检查每个项目是否存在于已保存的已检查项目列表中:

For i As Integer = 0 To CheckedListBox1.Items.Count - 1
    If checkedItems.Contains(CheckedListBox1.Items(i)) Then
        CheckedListBox1.SetItemChecked(i, True)
    End If
Next

因此,要将该方法应用于您的代码示例,它看起来像这样:

Dim checkedItems As New List(Of Object)(lstVerktyg.CheckedItems.Count)
If txtSearch.Text.Length = 1 Then
    For Each i As Object In lstVerktyg.CheckedItems
        If i = "" Then Exit For
        checkedItems.Add(i)
    Next
End If

Dim Connection As New MySqlConnection("server=" & My.Settings.Host & ";user id=" & My.Settings.User & "; password=" & My.Settings.Pass & "; port=3306; database=" & My.Settings.DB & "; pooling=false")
Try
    Connection.Open()
Catch ex As MySqlException
    Exit Sub
End Try
Dim Reader As MySqlDataReader
Dim Query As MySqlCommand
Dim ResultsNumber As Integer = 0
lstVerktyg.Items.Clear()

Query = New MySqlCommand("SELECT `namn` FROM `verktyg` WHERE `namn` LIKE '%" & txtSearch.Text.Replace("'", "\'") & "%' LIMIT 300;", Connection)
Reader = Query.ExecuteReader()

While (Reader.Read())
    lstVerktyg.Items.Add(Reader.GetString("namn"))
End While

If Reader IsNot Nothing Then Reader.Close()
For i As Integer = 0 To lstVerktyg.Items.Count - 1
    If checkedItems.Contains(lstVerktyg.Items(i)) Then
        lstVerktyg.SetItemChecked(i, True)
    End If
Next