Click here to Skip to main content
15,884,237 members
Articles / Web Development / ASP.NET
Article

Simple country dropdown component

Rate me:
Please Sign up or sign in to vote.
2.93/5 (14 votes)
3 Aug 20032 min read 127.8K   1.7K   31   13
How to create a simple country dropdown component.

Sample Image - screenshot.gif

Introduction

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.

VB
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.

VB
<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
Get
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
Get
    Return vlc
End Get
Set(ByVal Value As ValueListChoice)
    vlc = Value
    LoadItems()
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.

VB
Private Sub CountryDropDown_Init(ByVal sender As Object, _ 
    ByVal e As System.EventArgs) Handles MyBase.Init
    LoadItems()
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.

VB
Private Sub LoadItems()
Me.Items.Clear()
If Me.vlc = ValueListChoice.CountryNames Then
      Me.Items.Add("--Country--")
      Me.Items.Add("Afghanistan")
      Me.Items.Add("Albania")
      Me.Items.Add("Algeria")
      Me.Items.Add("Andorra")
      Me.Items.Add("Angola")
      Me.Items.Add("Antigua and Barbuda")
      Me.Items.Add("Argentina")
      Me.Items.Add("Armenia")
      'etc
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 = ""
     col.Add(nav)

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

End If

End Sub

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

Conclusion

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.

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalwe can get it from regional setting! Pin
harris_yer5-Jun-06 20:49
harris_yer5-Jun-06 20:49 
QuestionHow do I build the Dll Pin
DCServices28-Feb-06 4:20
DCServices28-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
caliguyver24-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
enjoycrack24-Oct-05 15:14 
GeneralNewbie problem Pin
srini2819-Apr-05 14:25
srini2819-Apr-05 14:25 
GeneralRe: Newbie problem Pin
enjoycrack24-Oct-05 15:10
enjoycrack24-Oct-05 15:10 
GeneralI am getting an error Pin
Binsumi14-Jan-05 13:10
Binsumi14-Jan-05 13:10 
GeneralAdding states Pin
jroughgarden1-Nov-04 17:16
jroughgarden1-Nov-04 17:16 
GeneralHard Coded Pin
PFunky5-Aug-03 4:36
PFunky5-Aug-03 4:36 
GeneralRe: Hard Coded Pin
Jon Newman5-Aug-03 14:05
Jon Newman5-Aug-03 14:05 
GeneralRe: Hard Coded Pin
Chris Richner5-Aug-03 17:13
Chris Richner5-Aug-03 17:13 
GeneralRe: Hard Coded Pin
bernardoh15-Jul-05 11:41
bernardoh15-Jul-05 11:41 
GeneralRe: Hard Coded Pin
liammclennan9-Feb-07 20:35
liammclennan9-Feb-07 20:35 

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.