当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > .NET中为组合框添加自动查询功能

ASP.NET
灵活正确的实现.NET插件机制
在Asp.net中为图像加入版权信息
重构Session确实让代码简洁干净了不少
JSP与ASP.Net之间的Session值共享
如何获取当前程序文件的路径 Current Path
在.NET框架应用程序中发送电子邮件
asp.net开发wap程序必备:识别来访手机品牌型号
ASP.NET 2.0中使用自定义provider
asp.net开发wap必备:更好的匹配手机设备
用ashx动态生成文件
ASP.NET中17种正则表达式
提高ASP.Net应用程序性能的十大方法
一个通过web.Mail发送邮件的类
在DataGrid里面根据日期的不同显示new图标
Asp.Net细节性问题精萃
自定义控件中使用枚举类型的属性(原创)
.NET开发 正则表达式中的 Bug
.Net学习:IronPython分析Lambda表达式
ASP.NET中的Response对象的方法
在ASP.NET 2.0中数据绑定的实现方法

ASP.NET 中的 .NET中为组合框添加自动查询功能


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 68 ::
收藏到网摘: n/a

在窗体中添加如下方法:

第一个方法是AutoCompleteKeyUp,它将组合框和KeyEventArgs对象作为参数,需要在组合框的KeyUp事件中调用此方法;它全根据用户输入的内容选择最接近的内容;

第二个方法是AutoCompleteLeave,在激活组合框的Leave事件时调用,此方法仅提取用户最终选择的内容,按照组合框中的每个匹配内容修改其大小写。

代码如下:

以下为引用的内容:

  Private 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.Down, Keys.Delete, Keys.CapsLock

  Return

  End Select

  '在查询列表中找到

  strTyped = Combo.Text

  intFoundIndex = Combo.FindString(strTyped)

  If intFoundIndex >= 0 Then

  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

  Private Sub AutoCompleteLeave(ByVal Combo As ComboBox)

  Dim intFoundIndex As Integer

  intFoundIndex = Combo.FindStringExact(Combo.Text)

  Combo.SelectedIndex = -1

  Combo.SelectedIndex = intFoundIndex

  End Sub

  Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp

  AutoCompleteKeyUp(ComboBox1, e)

  End Sub

  Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave

  AutoCompleteLeave(ComboBox1)

  End Sub