Click here to Skip to main content
6,305,776 members and growing! (17,269 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Windows Forms     Intermediate

How to have an Auto-Completing ComboBox in .NET

By Nishant Sivakumar

Shows how to derive a class from ComboBox and add an Auto-Complete feature to it. The example uses Managed C++.
C++/CLI, VC7.NET 1.0, Win2K, WinXP, Dev
Posted:2 Jun 2002
Views:73,094
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
15 votes for this article.
Popularity: 5.03 Rating: 4.28 out of 5
1 vote, 11.1%
1

2

3

4
8 votes, 88.9%
5

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

About the Author

Nishant Sivakumar


Member
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralDemo PinmemberJoyBurke8:32 23 May '06  
GeneralGreat Works,Can you complete it with C#? Pinmemberkv400016:08 8 Jun '05  
GeneralSlight Change PinmemberSkute3:19 3 Feb '04  
GeneralShell's Auto Complete PinmemberRama Krishna8:59 3 Jun '02  
GeneralRe: Shell's Auto Complete PinmemberNish - Native CPian9:11 3 Jun '02  
GeneralRe: Shell's Auto Complete PinmemberRama Krishna9:08 3 Jun '02  
GeneralRe: Shell's Auto Complete PinmemberNish - Native CPian19:20 3 Jun '02  
GeneralRe: Shell's Auto Complete PinsubeditorJames T. Johnson19:49 3 Jun '02  
GeneralRe: Shell's Auto Complete PinmemberNish - Native CPian20:04 3 Jun '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jun 2002
Editor: Andrew Peace
Copyright 2002 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project