Click here to Skip to main content
Click here to Skip to main content

Automaticaly Match and Select ComboBox Item

, 2 Nov 2006
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

ChandraSekar Maheswaran
Web Developer
India India
Hi,
Am Chandru. I'm a Software developer. Am concentrating Only on Microsoft Technologies more than others. Don't ask me why..! I usually write codes a lot.. I do them because I Like them. Am working in a software firm in India. i've done a Post Graduation in Computers.

Comments and Discussions

 
GeneralSuggestion on better code flow Pinmembermunnje2-Nov-06 9:16 
GeneralRe: Suggestion on better code flow PinmemberChandraSekar Maheswaran2-Nov-06 19:10 
GeneralToo many PinmemberJonathan [Darka]2-Nov-06 4:18 
GeneralRe: Too many PinmemberChandraSekar Maheswaran2-Nov-06 19:08 
GeneralRe: Too many PinmemberChandraSekar Maheswaran2-Nov-06 19:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 2 Nov 2006
Article Copyright 2006 by ChandraSekar Maheswaran
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid