Public Class AutoCombobox
    
Inherits System.Windows.Forms.ComboBox
Windows 窗体设计器生成的代码
  
    
Private Sub AutoComboBox_KeyPress(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        AutoComplete(
Me, e)
    
End Sub

    
Public Sub AutoComplete(ByRef cb As ComboBox, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        
Dim strFindStr As String

        
If e.KeyChar = Chr(8Then  
            
If cb.SelectionStart <= 1 Then
                cb.Text 
= ""
                
Exit Sub
            
End If
            
If cb.SelectionLength = 0 Then
                strFindStr 
= cb.Text.Substring(0, cb.Text.Length - 1)
            
Else
                strFindStr 
= cb.Text.Substring(0, cb.SelectionStart - 1)
            
End If
        
Else
            
If cb.SelectionLength = 0 Then
                strFindStr 
= cb.Text & e.KeyChar
            
Else
                strFindStr 
= cb.Text.Substring(0, cb.SelectionStart) & e.KeyChar
            
End If
        
End If
        
Dim intIdx As Integer = -1
               intIdx 
= cb.FindString(strFindStr)
        
If intIdx <> -1 Then 
            cb.SelectedText 
= ""
            cb.SelectedIndex 
= intIdx
            cb.SelectionStart 
= strFindStr.Length
            cb.SelectionLength 
= cb.Text.Length
            e.Handled 
= True

        
End If
    
End Sub

End Class

重写Combobox控件,能够快速的查出Combobox匹配的每一项.
posted on 2007-06-19 14:12  小黑三  阅读(879)  评论(0编辑  收藏  举报