Click here to Skip to main content
Click here to Skip to main content

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

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.

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

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.

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

About the Author

Mesrop

United States United States
No Biography provided

Comments and Discussions

 
Generalwe can get it from regional setting! Pinmemberharris_yer5-Jun-06 20:49 
QuestionHow do I build the Dll PinmemberDCServices28-Feb-06 4:20 
Generalthx for the code but i dont know how to call the dll in my main program Pinmembercaliguyver24-Oct-05 14:36 
GeneralRe: thx for the code but i dont know how to call the dll in my main program Pinmemberenjoycrack24-Oct-05 15:14 
GeneralNewbie problem Pinmembersrini2819-Apr-05 14:25 
GeneralRe: Newbie problem Pinmemberenjoycrack24-Oct-05 15:10 
GeneralI am getting an error PinmemberBinsumi14-Jan-05 13:10 
GeneralAdding states Pinmemberjroughgarden1-Nov-04 17:16 
GeneralHard Coded PinmemberPFunky5-Aug-03 4:36 
GeneralRe: Hard Coded PinmemberJonny Newman5-Aug-03 14:05 
GeneralRe: Hard Coded PinmemberJerry Maguire5-Aug-03 17:13 
GeneralRe: Hard Coded Pinmemberbernardoh15-Jul-05 11:41 
GeneralRe: Hard Coded Pinmemberliammclennan9-Feb-07 20:35 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 4 Aug 2003
Article Copyright 2003 by Mesrop
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid