您的位置:首页 > 编程语言 > VB

章鱼哥出品_VB.NET ComboBox、 TextBox 实现自动完成、自动过滤 模糊查询的功能

2014-10-03 21:13 1401 查看
本文以ComboBox为例讨论自动过滤功能,主要有两种方式:(TextBox也一样)
一、以AutoCompleteSource、AutoCompleteMode、AutoCompleteCustomSource这三个属性的设置来实现自动过滤。


但是这种方法只能从左到右的过滤,比如Items中有 12、13两项,输入1的时候就会出现12、13两项,但是输入2的时候就不会有任何显示。下面是这种方法的具体实现代码:

' 自动过滤函数
    '章鱼哥 QQ 3107073263 群 309816713
    Private Sub AutoComplete(ByVal ComBox As ComboBox)
        'With 函数的使用,不了解的可以上网查下 
        With ComBox
            '首先清空自动完成自定义数据源,这样您可以在每次combobox的items项改变时,调用该函数
            .AutoCompleteCustomSource.Clear()
            '设置自动完成的完整字符串源,共有9种选项,这里选择了自定义源
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            '设置组合框的文本完成行为,有四种选项,这里选择的是自动完成和列表框显示的组合
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            '将items中所有的字符串添加到自定义数据源中
            For Each item As String In .Items
                .AutoCompleteCustomSource.Add(item)
            Next
        End With
    End Sub

执行效果如图:



二、 模糊查询方式 看到网上有很多人问到如何做到模糊查询,就是输入字符串中的任意一个字符,就会显示全部符合条件的结果,如上面提到的12、13的例子,第一种方法就是实现不了输入2显示12的效果。这里笔者给出一种解决方法,具体实现如下:
'这种方法是使用一个ComboBox 和ListBox的组合的方式实现,ComboBox命名为ComboBox1 ,ListBox是代码创建
    ' 在ComboBox1的TextChanged事件中执行操作如下
    Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
        '获取输入的字符串
        Dim text As String = ComboBox1.Text.Trim()
        '用以记录匹配字符串的个数
        Dim index As Integer = 0
        Dim listBox1 As New ListBox
        ' list_Pd是定义的全局布尔变量,用于判断是否创建了listbox控件
        If list_Pd Then '如果已经创建
            For Each contr As Control In Me.Controls '遍历窗体中的所有控件,寻找创建的listbox控件
                If contr.Name = "list" Then
                    listBox1 = CType(contr, ListBox)
                End If
            Next
        Else '如果没有创建,则调用Custom_ListBox()函数创建
            listBox1 = Custom_ListBox(ComboBox1)
        End If
        '将listbox 控件所有项清空
        listBox1.Items.Clear()
        '将查询的结果添加到listbox 的items 中
        For Each Str As String In ComboBox1.Items
            '将所有的字符串全部转化为小写再判断,这样输入就不用分大小写了
            If Not text = "" And Str.ToLower.Contains(text.ToLower) Then
                index += 1
                listBox1.Items.Add(Str)
            End If
        Next
        '判断符合条件的项的个数,
        If index = 1 Then
            ComboBox1.Text = listBox1.Items(0)
            listBox1.Visible = False
        ElseIf index > 1 Then
            listBox1.Visible = True
        Else
            listBox1.Visible = False
        End If
    End Sub
    '自动创建listbox控件的函数
    Private Function Custom_ListBox(ByVal ComBox As ComboBox) As ListBox
        Dim Listbox As New ListBox
        Dim point As Point
        point.X = ComBox.Location.X
        point.Y = ComBox.Location.Y + ComBox.Height
        With Listbox
            .Name = "list" '设置控件名称
            .Location = point '设置控件的位置,放在combobox的下面
            .Width = ComBox.Width '控件的宽度,与combobox的宽一样
            .Height = ComBox.Height * (ComBox.Items.Count + 1) '高度
            .Items.Clear()
            .Visible = False
        End With
        AddHandler Listbox.Click, AddressOf ListBox_Click '添加点击事件 ListBox_Click()
        Me.Controls.Add(Listbox) '这步重要 将控件添加到窗体中。没有这句将不会显示listbox控件
        list_Pd = True
        Return Listbox
    End Function
    '创建的listbox的点击事件函数
    Private Sub ListBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CType(sender, ListBox).Visible = False
        ComboBox1.Text = CType(sender, ListBox).SelectedItem
    End Sub




代码执行效果如图:







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: