Simple country dropdown component






2.93/5 (14 votes)
Aug 4, 2003
2 min read

128816

1656
How to create a simple country dropdown component.
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.