Click here to Skip to main content
15,886,689 members
Articles / Web Development / ASP.NET
Article

Automaticaly Match and Select ComboBox Item

Rate me:
Please Sign up or sign in to vote.
3.10/5 (7 votes)
2 Nov 20061 min read 56.4K   687   14   5
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
VB
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.
VB
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.
VB
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


Written By
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 Pin
munnje2-Nov-06 8:16
munnje2-Nov-06 8:16 
GeneralRe: Suggestion on better code flow Pin
ChandraSekar Maheswaran2-Nov-06 18:10
ChandraSekar Maheswaran2-Nov-06 18:10 
GeneralToo many Pin
Jonathan [Darka]2-Nov-06 3:18
professionalJonathan [Darka]2-Nov-06 3:18 
GeneralRe: Too many Pin
ChandraSekar Maheswaran2-Nov-06 18:08
ChandraSekar Maheswaran2-Nov-06 18:08 
GeneralRe: Too many Pin
ChandraSekar Maheswaran2-Nov-06 18:15
ChandraSekar Maheswaran2-Nov-06 18:15 

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

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