Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C++/CLI
Article

How to have an Auto-Completing ComboBox in .NET

Rate me:
Please Sign up or sign in to vote.
4.45/5 (11 votes)
2 Jun 20023 min read 113.8K   2.5K   37   11
Shows how to derive a class from ComboBox and add an Auto-Complete feature to it. The example uses Managed C++.

Introduction

This article is an elementary tutorial that shows you how you can have auto-completing combo-boxes in your Managed C++ programs. Of course it wouldn't be much of a difficulty to convert the code straight to C# or VB .NET. The technique described in this article requires you to have the final release of VS.NET. It will not work on any beta version of VS.NET including beta 2 as I found out after two days of frustration.

Screenshots

Sample Image - image01.gif

The above screenshot shows how it works for a drop down combo box.


Sample Image - image02.gif

This screenshot shows how it works for a simple combo box

Description

Basically what we do here is to derive a class from the ComboBox class. Then we override the OnKeyPress method that is called when a key is pressed. For certain keys we would like to call the base class handler. These keys are the BackSpace key which has some expected behaviour and it would not be polite of our class to mess with expected behaviour. Other keys that have pre-defined roles include the cursor keys. We also need to check the ComboBox style. If it is a drop-list combo box where the edit box is uneditable, we should not do anything funny, and instead should simply call the base class function.

if(e->KeyChar == Windows::Forms::Keys::Back ||
    e->KeyChar == Windows::Forms::Keys::Up ||
    e->KeyChar == Windows::Forms::Keys::Down ||
    e->KeyChar == Windows::Forms::Keys::Right ||
    e->KeyChar == Windows::Forms::Keys::Left ||
    DropDownStyle == ComboBoxStyle::DropDownList )
{
    ComboBox::OnKeyPress(e);
}

Okay. So we know when to call the base class. Now for all other cases what we need to do is simple. Take the existing text from the edit box. Search the combo box for an entry that starts with this text. Now we need to set the text in the combo box to this searched and found text, except that all portions of this new text that exceeds the old text should be selected. Thus the user can type any key he wants to and remove all the selected text. That's what auto-completing combo boxes are supposed to do. I have written a simple search function called FirstMatch which takes a string and returns a string.

If our search was successful we can actually drop down our combo box and the advantage is that the selection bar will automatically move to the closest match to what's in the edit box. In our case there will be an exact match of course. I was wondering how to get this effect and it was very pleasing to get it free of effort. But we must remember to check to see if the combo box has the DropDown style set. If it is a simple combo box we don't want to try dropping it down.

String *str = FirstMatch(Text);
if(str->Length)
{
    int q1 = Text->Length;
    int q2 = str->Length - Text->Length;
    Text = str;
    if(DropDownStyle == ComboBoxStyle::DropDown)
        DroppedDown = true;
    SelectionStart = q1;
    SelectionLength = q2;
}
else
{
    if(DropDownStyle == ComboBoxStyle::DropDown)
        DroppedDown = false;
}

Finally we need to inform Windows that the event has been handled. For this we need to set the Handled property of the KeyPressEventArgs object passed to us to true. If we don't set this to true, this message will be send for default processing. Since we've handled it already, there is no need for that.

e->Handled = true;

Conclusion

I've noticed that when you take Run from the Start Menu, there is an auto-completing combo box. But there is a difference there in that the dropped down list is resizable. I haven't figured out how to do that unfortunately and I'd much appreciate it if anyone could give me some pointers. I hope it won't involve any of that owner draw stuff!

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
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralMy vote of 2 Pin
Corey Fournier18-Sep-09 2:48
Corey Fournier18-Sep-09 2:48 
GeneralDemo Pin
JoyBurke23-May-06 7:32
JoyBurke23-May-06 7:32 
GeneralRe: Demo Pin
Corey Fournier18-Sep-09 2:47
Corey Fournier18-Sep-09 2:47 
QuestionGreat Works,Can you complete it with C#? Pin
Alenty8-Jun-05 15:08
Alenty8-Jun-05 15:08 
GeneralSlight Change Pin
Skute3-Feb-04 2:19
Skute3-Feb-04 2:19 
GeneralShell's Auto Complete Pin
Rama Krishna Vavilala3-Jun-02 7:59
Rama Krishna Vavilala3-Jun-02 7:59 
GeneralRe: Shell's Auto Complete Pin
Nish Nishant3-Jun-02 8:11
sitebuilderNish Nishant3-Jun-02 8:11 
GeneralRe: Shell's Auto Complete Pin
Rama Krishna Vavilala3-Jun-02 8:08
Rama Krishna Vavilala3-Jun-02 8:08 
GeneralRe: Shell's Auto Complete Pin
Nish Nishant3-Jun-02 18:20
sitebuilderNish Nishant3-Jun-02 18:20 
GeneralRe: Shell's Auto Complete Pin
James T. Johnson3-Jun-02 18:49
James T. Johnson3-Jun-02 18:49 
GeneralRe: Shell's Auto Complete Pin
Nish Nishant3-Jun-02 19:04
sitebuilderNish Nishant3-Jun-02 19:04 
James T. Johnson wrote:
Woohoo! I'm at the top

Naturally, considering that the VS .NET I used to develop this program was a gift from you Rose | [Rose]

James T. Johnson wrote:
BTW, work is going well on part two to the clipboard article I have the demo program done and I'm working on the article itself now.

Cool Smile | :)

Nish


Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.

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.