VB.NET中TextBox的智能感知应用实例
作者:shichen2014 时间:2021-07-09 09:36:31
本文实例形式介绍了VB.NET中TextBox的智能感知实现方法,功能非常实用,具体如下:
该实例主要实现:在TextBox中键入字符,可以智能感知出列表,同时对不存在的单词(没有出现智能感知的)自动显示“Not Found”。
对此功能首先想到的是利用TextBox的AutoComplete功能。该功能允许你设置不同形式的AutoComplete智能感知,譬如:
1)AutoCompleteSource:设置感知源头类型(这里是CustomSource)。
2)AutoCompleteMode:设置感知的模式(输入不存在的字符追加,不追加还是同时存在,这里显然不追加)。
3)AutoCompleteCustomSource:设置源头数据(AutoCompleteSource必须是CustomSource)。
接下来思考如何在输入第一个字符的时候判断是否被感知到,如果没有则显示文本。
拖拽一个Label到窗体上,然后在TextBox的KeyUp事件中对数据源进行判断(为了方便,直接先把数据源数据转化成Array的形式然后使用扩展方法Any进行判断),同时为了防止界面卡死,使用异步。
具体实现代码如下:
Public Class Form1
Dim collection As New AutoCompleteStringCollection
Private ReadOnly arrayCollection() As String = {"a"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub New()
InitializeComponent()
collection.AddRange(New String() {"apple", "aero", "banana"})
TextBox1.AutoCompleteCustomSource = collection
ReDim arrayCollection(collection.Count - 1)
collection.CopyTo(arrayCollection, 0)
End Sub
''' <summary>
''' When release the keys, plz start a background thread to handle the problem
''' </summary>
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
Dim act As New Action(Sub()
'Check whether there are any values inside the collection or not
If (TextBox1.Text = "") OrElse (arrayCollection.Any(Function(s)
Return s.StartsWith(TextBox1.Text)
End Function)) Then
Label1.BeginInvoke(New MethodInvoker(Sub()
Label1.Text = String.Empty
End Sub))
Else
Label1.BeginInvoke(New MethodInvoker(Sub()
Label1.Text = "Not found"
End Sub))
End If
End Sub)
act.BeginInvoke(Nothing, Nothing)
End Sub
End Class
这里有一些注意点:
1)异步的异常不会抛出(因为异步的本质是CLR内部的线程),只能调试时候看到。因此编写异步程序必须万分小心。
2)VB.NET定义数组(譬如定义String(5)的数组,其实长度是6(从0~5)包含“5”自身,因此数组复制(Redim重定义大小)的时候必须Count-1,否则重新定义的数组会多出一个来,默认是Nothing,这会导致异步线程出现异常)。
标签:VB.NET,TextBox
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 使用SharpZipLib生成压缩包的实例代码
2021-08-29 20:32:57
![](https://img.aspxhome.com/file/2023/8/85738_0s.png)
Kotlin协程之Flow基础原理示例解析
2021-10-17 21:07:44
![](https://img.aspxhome.com/file/2023/4/116034_0s.png)
java递归设置层级菜单的实现
2023-03-05 14:14:57
vc提示unexpected end of file found的原因分析
2022-01-19 12:58:00
Jenkins任务批量修改的技巧分享
2023-01-11 09:47:44
详解.NET 6如何实现获取当前登录用户信息
2022-01-29 12:51:15
![](https://img.aspxhome.com/file/2023/5/80095_0s.png)
浅谈Mybatis分页插件,自定义分页的坑
2021-12-15 01:28:20
![](https://img.aspxhome.com/file/2023/2/108202_0s.jpg)
C#实现二叉排序树代码实例
2021-10-10 06:26:12
![](https://img.aspxhome.com/file/2023/3/102223_0s.png)
浅谈java多线程wait,notify
2023-11-29 16:29:03
C# 获取硬盘号,CPU信息,加密解密技术的步骤
2023-04-23 10:37:50
android实现拍照或从相册选取图片
2022-08-08 06:55:36
![](https://img.aspxhome.com/file/2023/4/109154_0s.gif)
C#微信公众号开发之自定义菜单
2023-01-23 02:07:08
![](https://img.aspxhome.com/file/2023/9/68079_0s.png)
Java微信支付-微信红包
2023-11-01 01:07:05
![](https://img.aspxhome.com/file/2023/6/66166_0s.png)
javaweb实现在线支付功能
2022-02-08 19:17:34
![](https://img.aspxhome.com/file/2023/8/93348_0s.jpg)
浅析java修饰符访问权限(动力节点Java学院整理)
2023-07-13 18:07:34
.NET WinForm实现在listview中添加progressbar的方法
2021-10-08 20:23:54
JAVA 中Spring的@Async用法总结
2023-11-28 16:35:58
详细了解C# 枚举与位枚举
2023-02-19 17:36:57
![](https://img.aspxhome.com/file/2023/3/113723_0s.png)
Spring MVC Controller返回值及异常的统一处理方法
2023-04-01 00:23:12
Java常见问题之javac Hello.java找不到文件的解决方法
2023-12-15 21:19:45
![](https://img.aspxhome.com/file/2023/3/83163_0s.png)