Click here to Skip to main content
11,581,515 members (63,152 online)
Click here to Skip to main content

Editable History ComboBox

, 11 Feb 2004 CPOL 76.4K 430 38
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to easily simulate an editable combobox that can persist previous entries (Like Internet Explorer).

Sample Image - historycombo.jpg


The Compact Framework does not provide the option for an editable combobox, but there is an easy way to work around this limitation. The following article will explain how to create an editable combobox that remembers up to 10 previous entries (Like the address bar in Internet Explorer).

Using the code

To make a combobox editable, you simply overlay a text box on a combobox.  To make the textbox and combobox fit together seamlessly you can just add code to each combobox like so:

int dropDownButtonWidth = 14;
txtTest1.Bounds = cboTest1.Bounds;
txtTest1.Width -= dropDownButtonWidth;

Now, to complete the behavior of the editable combobox, you just need to make sure the textbox gets updated with the currently selected item from the combobox.  Refer to the code below:

private void cboTest1_SelectedIndexChanged(object sender, System.EventArgs e)
    txtTest1.Text = cboTest1.Text;

This is all you need to do to get the behavior of an editable combobox.  If you would like to create a history combobox, you will need an XML file to persist the combobox entries. The code to load the past entries is pretty straightforward.

// Load the combobox entries from the XML file.
xdoc.Load(@"\Path to comboboxes.xml");
XmlElement root = xdoc.DocumentElement;
XmlNodeList nodeList = root.ChildNodes;
ComboBox cboHistory;

for(int j=0; j<nodeList.Item(i).ChildNodes.Count; ++j)
The code to save new entries is a little more involved but not too bad.
int maxEntriesToStore = 10;
XmlTextWriter tw = new XmlTextWriter(@"\Path to comboboxes.xml", 
tw.WriteStartAttribute("name", string.Empty);

// Write the item from the text box first.
if(txtTest1.Text.Length != 0)
    maxEntriesToStore -= 1;

// Write the rest of the entries (up to 10).
for(int i=0; i < cboTest1.Items.Count && i < maxEntriesToStore; ++i)
    if(txtTest1.Text != cboTest1.Items[i].ToString())

The demo project contains code to handle 3 comboboxes and is arranged a little differently than the sample code above. Enjoy! Smile | :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
Check out my blog!

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey27-Mar-12 23:48
membermanoj kumar choubey27-Mar-12 23:48 
GeneralSample ComboBox code for .net CF Pin
Kalzon13-Feb-05 14:31
memberKalzon13-Feb-05 14:31 
GeneralNice Article Pin
jadeboy12-Feb-04 16:55
memberjadeboy12-Feb-04 16:55 
GeneralRe: Nice Article Pin
pbrooks12-Feb-04 17:09
memberpbrooks12-Feb-04 17:09 

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
Web04 | 2.8.150603.1 | Last Updated 12 Feb 2004
Article Copyright 2004 by pbrooks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid