0

我在使用 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

我该怎么办?

4

1 回答 1

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
于 2013-07-19T18:48:56.027 回答