让你的输入文本框也具有记忆功能
2004-12-15 13:16
489 查看
我们在用IE或者是Access时,一定都注意到这个很有意思的东西:输入文本框控件能通过匹配我们过去键入的内容来帮助我们自动添加完成我们要输入的内容,很是方便。但是,在我们编程时,这样花哨的功能并没有被封装在控件里。
其实,要教会它们这样做一点也不困难,涕淌以ComboBox控件为例(因为ComboBox用来做这个示例再合适不过了呵呵),给大家秀一段很简单的代码。其他的文本输入控件也是一样样的道理,我就不赘述了。
Private Sub ComboBox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
AutoCompleteKeyUp(ComboBox1, e)
End Sub
Public Sub AutoCompleteKeyUp(ByVal Combo As ComboBox, ByVal e As KeyEventArgs)
Dim strTyped As String
Dim intFoundIndex As Integer
Dim objFoundItem As Object
Dim strFoundText As String
Dim strAppendText As String
'忽略这些基本操作键
Select Case e.KeyCode
Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, _
Keys.Delete, Keys.Down, Keys.CapsLock
Return
End Select
'看看用户输入了些什么
strTyped = Combo.Text
intFoundIndex = Combo.FindString(strTyped)
'如果找到了的话……
If intFoundIndex >= 0 Then
'获取Combo下的匹配项
objFoundItem = Combo.Items(intFoundIndex)
'获取匹配的字符串
strFoundText = Combo.GetItemText(objFoundItem)
'开始自动添加文本
strAppendText = strFoundText.Substring(strTyped.Length)
Combo.Text = strTyped & strAppendText
'选亮添加的文本
Combo.SelectionStart = strTyped.Length
Combo.SelectionLength = strAppendText.Length
End If
End Sub
好了,你可以试试上面的代码。先在ComboBox的下拉列表里预存一些字符串吧,嘿嘿!
其实,要教会它们这样做一点也不困难,涕淌以ComboBox控件为例(因为ComboBox用来做这个示例再合适不过了呵呵),给大家秀一段很简单的代码。其他的文本输入控件也是一样样的道理,我就不赘述了。
Private Sub ComboBox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
AutoCompleteKeyUp(ComboBox1, e)
End Sub
Public Sub AutoCompleteKeyUp(ByVal Combo As ComboBox, ByVal e As KeyEventArgs)
Dim strTyped As String
Dim intFoundIndex As Integer
Dim objFoundItem As Object
Dim strFoundText As String
Dim strAppendText As String
'忽略这些基本操作键
Select Case e.KeyCode
Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, _
Keys.Delete, Keys.Down, Keys.CapsLock
Return
End Select
'看看用户输入了些什么
strTyped = Combo.Text
intFoundIndex = Combo.FindString(strTyped)
'如果找到了的话……
If intFoundIndex >= 0 Then
'获取Combo下的匹配项
objFoundItem = Combo.Items(intFoundIndex)
'获取匹配的字符串
strFoundText = Combo.GetItemText(objFoundItem)
'开始自动添加文本
strAppendText = strFoundText.Substring(strTyped.Length)
Combo.Text = strTyped & strAppendText
'选亮添加的文本
Combo.SelectionStart = strTyped.Length
Combo.SelectionLength = strAppendText.Length
End If
End Sub
好了,你可以试试上面的代码。先在ComboBox的下拉列表里预存一些字符串吧,嘿嘿!
相关文章推荐
- 【十五分钟Talkshow】为什么新浪微博的输入文本框具有记忆功能
- 【十五分钟Talkshow】为什么新浪微博的输入文本框具有记忆功能
- 让你的输入文本框也具有记忆功能
- ASP.NET输入文本框自动提示功能
- 在窗体上放置saveFileDialog、openFileDialog两个控件。实现功能:1)程序运行时,在文本框(注意文本框多行、带垂直滚动条)中输入汉字、字符等,单击保存按钮,即可将文本框中内容保
- Asp.net文本框输入提示功能(jquery-autocomplete)
- js实现Select下拉框具有输入功能的方法
- jqgrid增加check控件并且全选和全部不选,同时具有记忆勾选功能
- 写一个方法完成如下功能,判断从文本框textbox1输入的一个字符,如果是数字则求该数字的阶乘,如果是小写字条,则转换为大写,大写字符不变,结果在文本框textbox2中显示
- javascript让文本框只能输入数字, 带数字类型和限制最大值功能.
- java web在不使用任何插件情况下实现文本框输入自动补全功能
- 文本框输入值从数据库中查询出匹配相等的数据,记忆文本框
- 有输入功能的HTML下拉框--并且可以动态获取文本框的值
- 文本框输入提示/自动完成功能
- 文本框限制输入、关闭按钮功能失效(附:源码)
- 让页面具有语音识别输入功能(HTML5 speech 语音输入方法)
- net程序系统中有时需要对文本框的输入做一些限定,比如禁用粘贴功能
- ASP.NET输入文本框自动提示功能
- 自动具有水印和日历选择功能的文本框
- 文本框支持语音识别输入功能