Click here to Skip to main content
13,193,393 members (56,929 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 3 Aug 2003

Simple country dropdown component

, 3 Aug 2003
Rate this:
Please Sign up or sign in to vote.
How to create a simple country dropdown component.

Sample Image - screenshot.gif


I recently went from site to site in search of a simple country/state dropdown component in VB.NET. I didn't want to pay for something that simple, so I finally settled for a C# component I found on CodeProject written by Paul Ingles. That component had an extra feature of detecting user's country and I thought it would unnecessarily complicate the matter. I needed something very fast and very simple, just a dropdown with names, no fancy stuff.

So I decided to create my own, and for those struggling to find a component that does just that - display country names - look no further!

Implementation details

Let's start by making sure we have the following statements at the top of our component class.

Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel

Now let's start building our component. Before we dive in, I should state that we are going to be adding an extra property that will define the value of a selected item. The values will either be country codes (for example ARM for Armenia) or full country names, just as they appear in Text property. Our control will populate the country names at design time, so you can access the Items collection and modify values if you want.

<DefaultProperty("Text"), ToolboxData("<{0}:CountryDropDown runat="server">_
 </{0}:CountryDropDown>")> Public Class CountryDropDown
Inherits System.Web.UI.WebControls.DropDownList
Public Enum ValueListChoice
     CountryCodes = 1
     CountryNames = 0
End Enum
Dim _text As String
Dim vlc As ValueListChoice
<Bindable(True), Category("Appearance"), _ 
   DefaultValue("")> Property [Text]() As String
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
<Bindable(True), Category("Appearance"), _ 
  DefaultValue("0")> Property [ValueList]() As ValueListChoice
    Return vlc
End Get
Set(ByVal Value As ValueListChoice)
    vlc = Value
End Set
End Property

Once we're done building the ValueList and Text properties (both of them will be displayed in the "Appearance" section of Properties window), we proceed to building the Init event. This event is triggered each time the control is drawn.

Private Sub CountryDropDown_Init(ByVal sender As Object, _ 
    ByVal e As System.EventArgs) Handles MyBase.Init
End Sub

The LoadItems private sub loads the list of countries based on the value of the ValueList property. We will omit listing all countries here, but you can find the full list in source code.

Private Sub LoadItems()
If Me.vlc = ValueListChoice.CountryNames Then
      Me.Items.Add("Antigua and Barbuda")
ElseIf Me.vlc = ValueListChoice.CountryCodes Then
     Dim col As New WebControls.ListItemCollection()
     Dim li As ListItem
     Dim nav As New ListItem()

     'load name / value pairs
     nav.Text = "--Country--"
     nav.Value = ""

     Dim af As New ListItem()
     af.Text = "Afghanistan"
     af.Value = "AFG"
    Dim al As New ListItem()
    al.Text = "Albania"
    al.Value = "ALB"
'add listitemcollection to the list
For Each li In col

End If

End Sub

That does it! We're all set. Build the DLL and add a reference to it from your project.


In the end, two important things...

  • Country list was taken from Yahoo! Countries.
  • CountryCode option stores ISO3166 Codes, not the IOC (Intl. Olympic Committee) codes.


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

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalwe can get it from regional setting! Pin
harris_yer5-Jun-06 20:49
memberharris_yer5-Jun-06 20:49 
QuestionHow do I build the Dll Pin
DCServices28-Feb-06 4:20
memberDCServices28-Feb-06 4:20 
Generalthx for the code but i dont know how to call the dll in my main program Pin
caliguyver24-Oct-05 14:36
membercaliguyver24-Oct-05 14:36 
GeneralRe: thx for the code but i dont know how to call the dll in my main program Pin
enjoycrack24-Oct-05 15:14
memberenjoycrack24-Oct-05 15:14 
GeneralNewbie problem Pin
srini2819-Apr-05 14:25
membersrini2819-Apr-05 14:25 
GeneralRe: Newbie problem Pin
enjoycrack24-Oct-05 15:10
memberenjoycrack24-Oct-05 15:10 
GeneralI am getting an error Pin
Binsumi14-Jan-05 13:10
memberBinsumi14-Jan-05 13:10 
GeneralAdding states Pin
jroughgarden1-Nov-04 17:16
memberjroughgarden1-Nov-04 17:16 
GeneralHard Coded Pin
PFunky5-Aug-03 4:36
memberPFunky5-Aug-03 4:36 
GeneralRe: Hard Coded Pin
Jonny Newman5-Aug-03 14:05
memberJonny Newman5-Aug-03 14:05 
GeneralRe: Hard Coded Pin
Jerry Maguire5-Aug-03 17:13
memberJerry Maguire5-Aug-03 17:13 
GeneralRe: Hard Coded Pin
bernardoh15-Jul-05 11:41
memberbernardoh15-Jul-05 11:41 
GeneralRe: Hard Coded Pin
liammclennan9-Feb-07 20:35
memberliammclennan9-Feb-07 20:35 
This article inspired me to do as suggested and create a modified version that loads the values from configuration. I also created a currency version. Both the source and binaries are free to use and available at[^] .

Liam McLennan

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.2 | Last Updated 4 Aug 2003
Article Copyright 2003 by Mesrop
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid