vb.net winform怎么能做到ChecklistBox和文本框组合,文本框输入可以进行模糊查询,可以多次模糊查询,比如我想选中001的所有数据,选中后还想选择003的所有数据
时间: 2024-03-08 08:47:34 浏览: 69
您可以使用一个ChecklistBox和一个文本框来实现这个功能。首先,将数据绑定到ChecklistBox中,然后在文本框中输入关键字进行模糊查询。以下是一个简单的实现方法:
1. 将数据绑定到ChecklistBox中:
```vb.net
Dim items As New List(Of String) From {"001-1", "001-2", "001-3", "002-1", "002-2", "003-1", "003-2", "003-3"}
ChecklistBox1.DataSource = items
```
2. 实现文本框的模糊查询:
```vb.net
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim keyword As String = TextBox1.Text.Trim()
If String.IsNullOrEmpty(keyword) Then
ChecklistBox1.DataSource = items
Else
Dim filteredItems As List(Of String) = items.Where(Function(item) item.Contains(keyword)).ToList()
ChecklistBox1.DataSource = filteredItems
End If
End Sub
```
3. 处理多次模糊查询:
您可以使用一个按钮来触发多次模糊查询,每次查询后更新ChecklistBox中的数据。以下是一个简单的实现方法:
```vb.net
Dim originalItems As New List(Of String) From {"001-1", "001-2", "001-3", "002-1", "002-2", "003-1", "003-2", "003-3"}
Dim items As List(Of String) = originalItems
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim keyword As String = TextBox1.Text.Trim()
If String.IsNullOrEmpty(keyword) Then
ChecklistBox1.DataSource = items
Else
Dim filteredItems As List(Of String) = items.Where(Function(item) item.Contains(keyword)).ToList()
ChecklistBox1.DataSource = filteredItems
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim selectedItems As New List(Of String)
For Each item In ChecklistBox1.CheckedItems
selectedItems.Add(item.ToString())
Next
items = originalItems.Where(Function(item) selectedItems.Contains(item.Split("-")(0))).ToList()
If Not String.IsNullOrEmpty(TextBox1.Text.Trim()) Then
Dim keyword As String = TextBox1.Text.Trim()
Dim filteredItems As List(Of String) = items.Where(Function(item) item.Contains(keyword)).ToList()
ChecklistBox1.DataSource = filteredItems
Else
ChecklistBox1.DataSource = items
End If
End Sub
```
在这个例子中,我们保存了原始数据列表originalItems和当前的数据列表items。每次点击按钮时,我们根据选中的项更新items,并根据当前文本框中的关键字重新绑定ChecklistBox中的数据。
阅读全文