VB.ET学习笔记:WinForm扩展ComboBox控件——仿百度搜索框(输入文本智能模糊提示说明、自动匹配过滤)
VB.ET学习笔记:WinForm扩展ComboBox控件——仿百度搜索框(输入文本智能模糊提示说明、自动匹配过滤)
ComboBox控件可以输入文本也可以提供列表来选择项,而且还自带有属性来实现自动匹配,但是它有一个弊端,只能从头开始匹配,例如"张三丰",只能输入“张”、"张三"或“张三丰”才能匹配出来,而输入"三"或“三丰”是匹配不了。演示代码如下:
Dim data() As Stri
VB.ET学习笔记:WinForm扩展ComboBox控件——仿百度搜索框(输入文本智能模糊提示说明、自动匹配过滤)
ComboBox控件可以输入文本也可以提供列表来选择项,而且还自带有属性来实现自动匹配,但是它有一个弊端,只能从头开始匹配,例如"张三丰",只能输入“张”、"张三"或“张三丰”才能匹配出来,而输入"三"或“三丰”是匹配不了。演示代码如下:
Dim data() As String = {"张三丰|ZSF", "李四|LS", "王五|WW", "赵六|ZL", "田七|TQ"}With Me.ComboBox1.Items.AddRange(data).AutoCompleteMode = AutoCompleteMode.SuggestAppend.AutoCompleteSource = AutoCompleteSource.ListItemsEnd With
效果如图:
而在项目想实现类似百度搜索框,输入文本时能够自动匹配,重要的是实现模糊匹配,也就是说在前台输入"三"或“三丰”也是有匹配项的。实现效果如图:
于是就开始百度,在CSD里查到《自动完成TextBox实现类似百度搜索框》一文,是通过扩展控件方法来实现的,改编了一下,用到ComboBox控件来。唉,不生产代码,只能做代码的搬运工啦,55……
Imports System.ComponentModel
Public Class ComboBoxExInherits ComboBox#Region "字段"''' <summary>''' 列表框''' </summary>Private listBox As ListBox''' <summary>''' 记住前输入的字符串''' </summary>Private oldText As String''' <summary>''' 显示面板''' </summary>Private panel As Panel''' <summary>''' 进程锁''' </summary>Private _lockObj As Object = ew Object()
#End Region#Region "属性"''' <summary>''' 在显示之前键入的最小字符''' </summary>''' <returns></returns><Category("筛选"), Description("在显示之前键入的最小字符"), DefaultValue(1)>Public Property MinTypedCharacters As Integer = 1''' <summary>''' listBox选择索引值''' </summary>''' <returns></returns><Category("筛选"), Description("listBox选择索引值"), DefaultValue(0)>Public Property LstSelectedIndex As IntegerGetReturn listBox.SelectedIndexEnd GetSet(ByVal value As Integer)If listBox.Items.Count > 0 Then listBox.SelectedIndex = valueEnd SetEnd Property''' <summary>''' 当前显示的实际列表''' </summary>''' <returns></returns>Private Property CurrentAutoCompleteList As List(Of String)''' <summary>''' 该控件的父窗体''' </summary>''' <returns></returns>Private ReadOnly Property ParentForm As FormGetReturn Me.Parent.FindForm()End GetEnd Property
#End Region#Region "构造函数"Public Sub ew()'调用基类构造函数()'列表框Me.listBox = ew ListBox()Me.listBox.ame = " SuggestionListBox"Me.listBox.Font = Me.FontMe.listBox.Visible = True'这个容器用来保持列表框所在位置Me.panel = ew Panel()Me.panel.Visible = FalseMe.panel.Font = Me.Font'能够适应父窗体的大小更改Me.panel.AutoSizeMode = AutoSizeMode.GrowAndShrink'初始化最小尺寸以避免重叠或闪烁问题Me.panel.ClientSize = ew Size(1, 1)Me.panel.ame = " SuggestionPanel"Me.panel.Padding = ew Padding(0, 0, 0, 0)Me.panel.Margin = ew Padding(0, 0, 0, 0)Me.panel.BackColor = Color.TransparentMe.panel.ForeColor = Color.TransparentMe.panel.Text = " "Me.panel.PerformLayout()'控件是否存在容器里If ot pa
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2024-06-03 21:58:50
推荐阅读
留言与评论(共有 10 条评论) |
本站网友 林爸爸回应 | 4分钟前 发表 |
0)Me.panel.BackColor = Color.TransparentMe.panel.ForeColor = Color.TransparentMe.panel.Text = " "Me.panel.PerformLayout()'控件是否存在容器里If ot pa | |
本站网友 动脉硬化闭塞症 | 15分钟前 发表 |
改编了一下 | |
本站网友 病房管理制度 | 23分钟前 发表 |
0)Me.panel.BackColor = Color.TransparentMe.panel.ForeColor = Color.TransparentMe.panel.Text = " "Me.panel.PerformLayout()'控件是否存在容器里If ot pa | |
本站网友 l15教练机 | 3分钟前 发表 |
0 | |
本站网友 维棉网 | 4分钟前 发表 |
DefaultValue(0)>Public Property LstSelectedIndex As IntegerGetReturn listBox.SelectedIndexEnd GetSet(ByVal value As Integer)If listBox.Items.Count > 0 Then listBox.SelectedIndex = valueEnd SetEnd Property''' <summary>''' 当前显示的实际列表''' </summary>''' <returns></returns>Private Property CurrentAutoCompleteList As List(Of String)''' <summary>''' 该控件的父窗体''' </summary>''' <returns></returns>Private ReadOnly Property ParentForm As FormGetReturn Me.Parent.FindForm()End GetEnd Property #End Region#Region "构造函数"Public Sub ew()'调用基类构造函数()'列表框Me.listBox = ew ListBox()Me.listBox.ame = " SuggestionListBox"Me.listBox.Font = Me.FontMe.listBox.Visible = True'这个容器用来保持列表框所在位置Me.panel = ew Panel()Me.panel.Visible = FalseMe.panel.Font = Me.Font'能够适应父窗体的大小更改Me.panel.AutoSizeMode = AutoSizeMode.GrowAndShrink'初始化最小尺寸以避免重叠或闪烁问题Me.panel.ClientSize = ew Size(1 | |
本站网友 补缴养老保险新政策 | 18分钟前 发表 |
改编了一下 | |
本站网友 中国贫困县名单 | 13分钟前 发表 |
用到ComboBox控件来 | |
本站网友 李广益 | 21分钟前 发表 |
输入文本时能够自动匹配 | |
本站网友 超级工程 | 19分钟前 发表 |
VB.ET学习笔记:WinForm扩展ComboBox控件——仿百度搜索框(输入文本智能模糊提示说明 |