Create custom country ComboBox control
From the previous post I have describe how to get the country list from the system registry. Now I want to show You how to create a custom country ComboBox control in visual studio .NET.
first, lets create a new project, name it whatever You want. then add a user control class by right clicking on the project then select add > User control…. menu. The add new item dialog appear, then name Your custom control with something. e.g : CountryComboBox.cs. click the add button, proceed to open it in the editor window.
Go to the source code editor and replace the existing code with the code bellow.
CountryComboBox.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using net.de.common.l10n;
namespace net.de.common.form
{
///
/// A custom ComboBox control that provide a list of country.
///
public partial class CountryComboBox : ComboBox
{
///
/// Default constructor
///
public CountryComboBox()
{
InitializeComponent();
// Populate the CountryComboBox list item with list of countries
this.Items.AddRange(CountryList.getInstance().getCountries().ToArray());
}
///
/// Override the OnPaint() method from the parrent.
///
///
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
}
The CountryList object in the above code is referenced from my previous pos about “Get country list from windows registry“. You should read that topic before you continue with this one. And also don’t forget to change the namespace, so that this code can integrate in your project.
After You’ve done with the above code, Build your project then you will see the the purple gear icon on the toolbox in a tab with your project name as the tittle. Here you go, you’ve done it!.

If you want to use your custom control with another project, you have to specify the build output as Class Library. To do this, go to the solution explorer > right click the project and choose properties. In the Application tab, specify your Output type as Class Library. Save and rebuild the project.
In the bin directory under your project folder, you will see there is a file called <Your Project name>.dll. capy it to your new project then add a reference to it. Have a nice try ![]()
Leave a Reply