Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Automaticaly Match and Select ComboBox Item

0.00/5 (No votes)
2 Nov 2006 1  
Automaticaly Select Item From a Combox as the String is entered

Introduction

This is a very simple Article. As I was working in VB I used to use a Data Combox which used to match and select the Items in it as you type in the string. I don't know whether .Net has such One. I think the default combox box dos'nt has it. So I worte this code which would that process.

The Code

Match Flag
Dim Match as Boolean
TextChanged : First we will ckeck for the match flag which is set by the key press event. If its true then We will check the entered string with the items in the list by their length. Like Taking the entered string and matching it with the item only to the length of the Enterded string ie,Chand is entered, We will match It with the Item in List ChandraSekar as Chand - Chand.If a string Matches then it will be selected in the ComboBox and a select command will be sent for the remaining length of the string selected to the entered one.
  Dim i As Short
        Dim Buffer As Short
        If Match = True Then
            For i = 0 To CmBx.Items.Count - 1
                Buffer = Len(CmBx.Text)
                If StrComp(UCase(CmBx.Text), UCase(VB.Left(CmBx.Items.Item(i), Buffer))) = 0 Then
                    CmBx.Text = CmBx.Items.Item(i)
                    CmBx.Focus()
                    System.Windows.Forms.SendKeys.Send("{left " & Len(CmBx.Text) - Buffer & "}+{End}")
                    Exit Sub
                End If
            Next i
        End If
KeyPressEvent : Here we check for the key entered. If the pressed key is DEL or BACKSPACE we set the match flag to false and send the DEL key if DEL key was pressed.
 Dim KeyCode As Short = Asc(e.KeyChar)
        If KeyCode = 8 Or KeyCode = 13 Then
            If KeyCode = 13 Then
                System.Windows.Forms.SendKeys.Send("{DEL}")
            End If
            Match = False
        Else
            Match = True
        End If

Sample Image - ASComboBox1.JPG

Sample Image - ASComboBox2.JPG

Sample Image - ASComboBox3.JPG

Sample Image - ASComboBox4.JPG

Summary

This is a simple code. The Logic is also not too Complicated. I wrote this code because I did'nt find any feature like this in the Combo Box Control. If there is any in it or there is any other Combo Box Control Providing it please let me Know.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here