Click here to Skip to main content
6,630,586 members and growing! (16,706 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

Editable History ComboBox

By pbrooks

Demonstrates how to easily simulate an editable combobox that can persist previous entries (Like Internet Explorer).
C#, VB 6, Windows, .NET CF, .NET, MobileVS.NET2003, Dev
Posted:11 Feb 2004
Views:60,690
Bookmarked:32 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 4.72 Rating: 3.76 out of 5
2 votes, 11.1%
1

2
2 votes, 11.1%
3
2 votes, 11.1%
4
12 votes, 66.7%
5

Sample Image - historycombo.jpg

Introduction

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)
{
    cboHistory.Items.Add(nodeList.Item(i).ChildNodes.Item(j).InnerText);    
}
        
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", 
                                     System.Text.Encoding.UTF8);
tw.WriteStartDocument();
tw.WriteStartElement("comboboxes");
tw.WriteStartElement("combobox");
tw.WriteStartAttribute("name", string.Empty);
tw.WriteString("cboTest1");
tw.WriteEndAttribute();

// Write the item from the text box first.

if(txtTest1.Text.Length != 0)
{
    tw.WriteStartElement("entry");
    tw.WriteString(txtTest1.Text);
    tw.WriteEndElement();
    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())
    {
        tw.WriteStartElement("entry");
        tw.WriteString(cboTest1.Items[i].ToString());
        tw.WriteEndElement();
    }
}

tw.WriteEndElement();
tw.WriteEndElement();
tw.Flush();
tw.Close();
        
The demo project contains code to handle 3 comboboxes and is arranged a little differently than the sample code above. Enjoy! :)

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

pbrooks


Member
Check out my blog! http://www.pagebrooks.com
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralSample ComboBox code for .net CF PinmemberKalzon115:31 3 Feb '05  
GeneralNice Article Pinmemberjadeboy17:55 12 Feb '04  
GeneralRe: Nice Article Pinmemberpbrooks18:09 12 Feb '04  

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

PermaLink | Privacy | Terms of Use
Last Updated: 11 Feb 2004
Editor: Nick Parker
Copyright 2004 by pbrooks
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project