Click here to Skip to main content
15,880,543 members
Articles / Mobile Apps / Windows Mobile

Windows Mobile Location ComboBox Controls

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
30 May 2009CPOL3 min read 22.7K   769   6  
A collection of pre-filled Country, Region, and City comboboxes for the .NET Compact Framework.

Introduction

This article shows how to create a set of self-contained ComboBox controls that display international country, region, and city selections from pre-defined, non-database lists. The controls fit in small size DLLs that do not require Compact SQL Server to execute.

Background

I was working on a WM6 application that asked the user for demographic data such as Country, State/Province/Region, and City. The data would eventually be transmitted to a Web Service so I wanted standardized responses. Therefore, I had to provide the list of locations.

Users do not like to scroll through long lists of thousands of states and cities. I wanted the controls to be smart enough to rebuild Region and City lists when a new Country or Region was selected. If you selected "United States > Michigan", I only wanted Michigan cities on the CityComboBox.

WM6LocationComboBox/CountryRegionCity.jpg

I did not, however, want to rely on a database that required the Microsoft SQL Server Compact Framework. It did not make sense that users had to load a large framework on their mobile device just to implement a small feature. Nor did I want to integrate with an external Web data source, as internet connections are not always possible in a mobile environment.

I found a great source of countries, regions, and cities at GeoBytes.com. I did a little massaging of the data to trim it to the smallest size possible. Instead of adding it to a database, I embedded the CSV files directly into the controls so that only the DLL need be distributed.

Using the code

The solution is split into two projects. The CountryRegionComboBox project provides two ComboBox controls, Country and Region. It does not provide the City ComboBox, as that adds a considerable amount of data. You can use this assembly when you do not need cities and wish to conserve precious mobile disk space.

The CountryRegionCityComboBox includes a 408 KB list of cities. I was able to trim it down a little by combining identically named cities, listing their Region IDs together. If you need the cities and don't mind the extra size, use this assembly.

Embedding the lists

The controls read the embedded data files using System.Reflection.Assembly and the GetManifestResourceStream() method. The CSV files are set as Embedded Resources so they are included in the DLL at compile time.

C#
// Get the executing assembly
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

// Get the embedded resource file and create a stream.
Stream CountryFileStream = 
       asm.GetManifestResourceStream("CountryRegionCityControls.Countries.csv");

Cross-control events

When you select a new country, the Country control needs to fire an event that is intercepted by the Region control. The Region control will then rebuild its list to display the regions, states, provinces, etc., of the selected country.

To do this, I used static classes that fired events for each control. Since the classes are static, there is only one implementation of each that is accessable by all controls. If two controls are on the same form, one will trigger events in the other without either of them being aware of the other.

C#
public static class DetectCountryChange
{
    public static event CountryChangedDelegate CountryChanged;
    public static void FireCountryChanged(Country Country)
    {
        if (CountryChanged != null)
            CountryChanged(new CountryChangedEventArgs(Country));
    }
}

To subscribe to the CountryChanged event, you add the event subscription to the Region control's constructor:

C#
public RegionsComboBox()
{
    InitializeComponent();
    // Attach to the CountryComboBox CountryChanged event
    // to trigger region list rebuild.
    DetectCountryChange.CountryChanged += 
      new CountryChangedDelegate(DetectCountryChange_CountryChanged);
}

When the CountryComboBox changes, it calls the FireCountryChanged() method, which triggers the CountryChanged event that is intercepted by the RegionsComboBox.

History

  • 2009.05.29 - Initial submission.

License

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


Written By
Systems Engineer ThomsonReuters Tax & Accounting
United States United States
I am a Senior System Administrator for a 400+ server ASP farm. With such a large farm and limited staff, our goal is to add as much automation as possible to the system. Most of my programming consists of intelligent slack: spending 2 hours to write a program that handles a reoccurring 10 minute manual job.

Comments and Discussions

 
-- There are no messages in this forum --