Source Code: Country ListView From .NET Globalization Namespace
Country ListView From .NET Globalization Namespace
This code demonstrates how to use the CultureInfo and RegionInfo classes from the .NET Globalization namespace to create a SortedDictionary list that can be bound to a ComboBox or DropDownList.
Visual Basic 2005
Imports System.Collections.Generic
Imports System.Globalization
Public Class Countries
Public Shared Function GetCounties() As SortedDictionary(Of String, String)
Dim countryList As New SortedDictionary(Of String, String)
' Iterate the Framework Cultures...
For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.FrameworkCultures)
Dim ri As RegionInfo
Try
ri = New RegionInfo(ci.Name)
Catch
' If a RegionInfo object could not be created we don't want to use the CultureInfo
' for the country list.
Continue For
End Try
' Create new country dictionary entry.
Dim newKeyValuePair As New KeyValuePair(Of String, String)(ri.EnglishName, ri.ThreeLetterISORegionName)
' If the country is not alreayd in the countryList add it...
If Not countryList.ContainsKey(ri.EnglishName) Then
countryList.Add(newKeyValuePair.Key, newKeyValuePair.Value)
End If
Next
Return countryList
End Function
End Class
C# 2005
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
public class Countries
{
public static SortedDictionary<string, string> GetCounties()
{
SortedDictionary<string, string> countryList = new SortedDictionary<string, string>();
// Iterate the Framework Cultures...
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.FrameworkCultures))
{
RegionInfo ri = null;
try
{
ri = new RegionInfo(ci.Name);
}
catch
{
// If a RegionInfo object could not be created we don't want to use the CultureInfo
// for the country list.
continue;
}
// Create new country dictionary entry.
KeyValuePair<string, string> newKeyValuePair = new KeyValuePair<string, string>(ri.EnglishName, ri.ThreeLetterISORegionName);
// If the country is not alreayd in the countryList add it...
if (!(countryList.ContainsKey(ri.EnglishName)))
{
countryList.Add(newKeyValuePair.Key, newKeyValuePair.Value);
}
}
return countryList;
}
}