我在使用 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
我该怎么办?