Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to make a ComboBox do auto complete?

Total Hit ( 3100)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
Imports System.Windows.Forms
'We import the System.Windows.Forms namespace
'so we don’t have to type it over and over again.
'you may have to reference the system.windows.forms namespace.

Public Class CompletionCombo
  Inherits ComboBox
  'We are making a combo box so instead of re-inventing the wheel
  'we just inherit it and override some of the methods

  Private _AutoComplete As Boolean = True
  'This variable is used to tell our OnTextChanged sub
  'whether or not to do the AutoComplete

  Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
    'This is fired when a key is pressed in the combo box
    _AutoComplete = e.KeyCode <> Keys.Delete And e.KeyCode <> Keys.Back
    'only do auto complete when the key pressed is not backspace or delete.
    MyBase.OnKeyDown(e)
    'Since we overrided the OnKeyDown event we must tell our "Base" class
    'that a key was pressed
  End Sub

  Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
    'This is fired when the text in the combo box has be changed
    If _AutoComplete Then
      'Only do the following if the user didn’t press the backspace or delete key
      Dim TextEntered As String = Me.Text
      'grab the current text out of the combo box
      Dim index As Integer = Me.FindString(TextEntered)
      'FindString is a method in the combo box class that we can take advantage of
      If index >= 0 Then
        'If the text does exist in the combo box’s items then
        _AutoComplete = False
        'disable the auto complete while we change the selected index
        Me.SelectedIndex = index
        'Change the selected index
        _AutoComplete = True
        're-enable the auto complete
        Me.Select(TextEntered.Length, Me.Text.Length)
        'Select only the "added" text
      End If 'If index >= 0
    End If 'If _AutoComplete
    MyBase.OnTextChanged(e)
    'Since we overrided the OnTextChanged event we must tell our "Base" class
    'that the text was changed
  End Sub
End Class


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.