【发布时间】:2017-11-28 11:47:12
【问题描述】:
我正在使用 vb.net 制作一个 Windows 窗体(桌面窗口),其中顶部有一个 TextBox,下面有一个 CheckBoxList。现在我想使用文本框中的值在 CheckBoxList 中搜索特定的复选框。我该怎么做?
【问题讨论】:
标签: .net search checkbox checkboxlist
我正在使用 vb.net 制作一个 Windows 窗体(桌面窗口),其中顶部有一个 TextBox,下面有一个 CheckBoxList。现在我想使用文本框中的值在 CheckBoxList 中搜索特定的复选框。我该怎么做?
【问题讨论】:
标签: .net search checkbox checkboxlist
首先对数据进行排序,然后再将其放入 CheckListBox。现在,如果我们将 CheckListBox 的所有项目存储在一个 ArrayList(或 Dictionary)中,那么任务就变得非常简单了。只需遍历 ArrayList(或 Dictionary)中的所有项目,然后在 ArrayList(或 Dictionary)中找到 以 TextBox 中用于搜索的值开头的值处停止。对 TextBox 的 TextChanged 事件执行此过程。现在找到索引后,只需将 CheckListBox 的选定索引更改为找到的索引。这是我的代码。
Private Sub txtSearch_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Dim iIndex As Integer = 0
Try
For Each key In gDict.Keys
If key.ToUpper.StartsWith(txtSearch.Text.ToUpper) Then
CheckedListBox1.SelectedIndex = iIndex
Exit For
End If
iIndex += 1
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "TP Designer", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
PS : 不要忘记对放入 CheckListBox 的数据进行排序
【讨论】: