Does anyone know of a nice, usable class that everyone on here could benefit from to use in populating the ComboBox
controls on a form for the Country and State fields?
I hate having to reinvent the wheel every time, and someone has probably done a very nice piece of work somewhere.
Bonus points if the existing class can return a list of State options whenever the Country is selected!
I'm currently developing this for a Windows Form (non-web application), and this project can't pull from a website.
OK, so I made one.
I built it nice and generic, so anyone should be able to use it
There is a class at the bottom called US_States
that is used as a container to store the State Name and State Abbreviation.
static class StateArray {
static List<US_State> states;
static StateArray() {
states = new List<US_State>(50);
states.Add(new US_State("AL", "Alabama"));
states.Add(new US_State("AK", "Alaska"));
states.Add(new US_State("AZ", "Arizona"));
states.Add(new US_State("AR", "Arkansas"));
states.Add(new US_State("CA", "California"));
states.Add(new US_State("CO", "Colorado"));
states.Add(new US_State("CT", "Connecticut"));
states.Add(new US_State("DE", "Delaware"));
states.Add(new US_State("DC", "District Of Columbia"));
states.Add(new US_State("FL", "Florida"));
states.Add(new US_State("GA", "Georgia"));
states.Add(new US_State("HI", "Hawaii"));
states.Add(new US_State("ID", "Idaho"));
states.Add(new US_State("IL", "Illinois"));
states.Add(new US_State("IN", "Indiana"));
states.Add(new US_State("IA", "Iowa"));
states.Add(new US_State("KS", "Kansas"));
states.Add(new US_State("KY", "Kentucky"));
states.Add(new US_State("LA", "Louisiana"));
states.Add(new US_State("ME", "Maine"));
states.Add(new US_State("MD", "Maryland"));
states.Add(new US_State("MA", "Massachusetts"));
states.Add(new US_State("MI", "Michigan"));
states.Add(new US_State("MN", "Minnesota"));
states.Add(new US_State("MS", "Mississippi"));
states.Add(new US_State("MO", "Missouri"));
states.Add(new US_State("MT", "Montana"));
states.Add(new US_State("NE", "Nebraska"));
states.Add(new US_State("NV", "Nevada"));
states.Add(new US_State("NH", "New Hampshire"));
states.Add(new US_State("NJ", "New Jersey"));
states.Add(new US_State("NM", "New Mexico"));
states.Add(new US_State("NY", "New York"));
states.Add(new US_State("NC", "North Carolina"));
states.Add(new US_State("ND", "North Dakota"));
states.Add(new US_State("OH", "Ohio"));
states.Add(new US_State("OK", "Oklahoma"));
states.Add(new US_State("OR", "Oregon"));
states.Add(new US_State("PA", "Pennsylvania"));
states.Add(new US_State("RI", "Rhode Island"));
states.Add(new US_State("SC", "South Carolina"));
states.Add(new US_State("SD", "South Dakota"));
states.Add(new US_State("TN", "Tennessee"));
states.Add(new US_State("TX", "Texas"));
states.Add(new US_State("UT", "Utah"));
states.Add(new US_State("VT", "Vermont"));
states.Add(new US_State("VA", "Virginia"));
states.Add(new US_State("WA", "Washington"));
states.Add(new US_State("WV", "West Virginia"));
states.Add(new US_State("WI", "Wisconsin"));
states.Add(new US_State("WY", "Wyoming"));
}
public static string[] Abbreviations() {
List<string> abbrevList = new List<string>(states.Count);
foreach (var state in states) {
abbrevList.Add(state.Abbreviations);
}
return abbrevList.ToArray();
}
public static string[] Names() {
List<string> nameList = new List<string>(states.Count);
foreach (var state in states) {
nameList.Add(state.Name);
}
return nameList.ToArray();
}
public static US_State[] States() {
return states.ToArray();
}
}
class US_State {
public US_State() {
Name = null;
Abbreviations = null;
}
public US_State(string ab, string name) {
Name = name;
Abbreviations = ab;
}
public string Name { get; set; }
public string Abbreviations { get; set; }
public override string ToString() {
return string.Format("{0} - {1}", Abbreviations, Name);
}
}
Updated jp2codes example, and added Canada:
public static class States {
public static List<State> los = new List<State> {
//us
new State("AL", "Alabama"),
new State("AK", "Alaska"),
new State("AZ", "Arizona"),
new State("AR", "Arkansas"),
new State("CA", "California"),
new State("CO", "Colorado"),
new State("CT", "Connecticut"),
new State("DE", "Delaware"),
new State("DC", "District Of Columbia"),
new State("FL", "Florida"),
new State("GA", "Georgia"),
new State("HI", "Hawaii"),
new State("ID", "Idaho"),
new State("IL", "Illinois"),
new State("IN", "Indiana"),
new State("IA", "Iowa"),
new State("KS", "Kansas"),
new State("KY", "Kentucky"),
new State("LA", "Louisiana"),
new State("ME", "Maine"),
new State("MD", "Maryland"),
new State("MA", "Massachusetts"),
new State("MI", "Michigan"),
new State("MN", "Minnesota"),
new State("MS", "Mississippi"),
new State("MO", "Missouri"),
new State("MT", "Montana"),
new State("NE", "Nebraska"),
new State("NV", "Nevada"),
new State("NH", "New Hampshire"),
new State("NJ", "New Jersey"),
new State("NM", "New Mexico"),
new State("NY", "New York"),
new State("NC", "North Carolina"),
new State("ND", "North Dakota"),
new State("OH", "Ohio"),
new State("OK", "Oklahoma"),
new State("OR", "Oregon"),
new State("PA", "Pennsylvania"),
new State("RI", "Rhode Island"),
new State("SC", "South Carolina"),
new State("SD", "South Dakota"),
new State("TN", "Tennessee"),
new State("TX", "Texas"),
new State("UT", "Utah"),
new State("VT", "Vermont"),
new State("VA", "Virginia"),
new State("WA", "Washington"),
new State("WV", "West Virginia"),
new State("WI", "Wisconsin"),
new State("WY", "Wyoming"),
//canada
new State("AB", "Alberta"),
new State("BC", "British Columbia"),
new State("MB", "Manitoba"),
new State("NB", "New Brunswick"),
new State("NL", "Newfoundland and Labrador"),
new State("NS", "Nova Scotia"),
new State("NT", "Northwest Territories"),
new State("NU", "Nunavut"),
new State("ON", "Ontario"),
new State("PE", "Prince Edward Island"),
new State("QC", "Quebec"),
new State("SK", "Saskatchewan"),
new State("YT", "Yukon"),
};
public static List<string> Abbreviations() {
return los.Select(s => s.Abbreviation).ToList();
}
public static List<string> Names() {
return los.Select(s => s.Name).ToList();
}
public static string GetName(string abbreviation) {
return los.Where(s => s.Abbreviation.Equals(abbreviation, StringComparison.CurrentCultureIgnoreCase)).Select(s => s.Name).FirstOrDefault();
}
public static string GetAbbreviation(string name) {
return los.Where(s => s.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)).Select(s => s.Abbreviation).FirstOrDefault();
}
public static List<State> ToList() {
return los;
}
}
public class State {
public State(string ab, string name) {
Name = name;
Abbreviation = ab;
}
public string Name { get; set; }
public string Abbreviation { get; set; }
public override string ToString() {
return string.Format("{0} - {1}", Abbreviation, Name);
}
}
public partial class State
{
public string Name { get; set; }
public string Abbreviations { get; set; }
}
/// <summary>
/// Return a static list of all U.S. states
/// </summary>
/// <returns></returns>
public IEnumerable<State> ListOfStates()
{
var states = CreateStateList();
return states.ToList();
}
private static IList<State> CreateStateList()
{
List<State> states = new List<State>();
states.Add(new State() { Abbreviations = "AL", Name = "Alabama" });
states.Add(new State() { Abbreviations = "AK", Name = "Alaska" });
states.Add(new State() { Abbreviations = "AR", Name = "Arkansas" });
states.Add(new State() { Abbreviations = "AZ", Name = "Arizona" });
states.Add(new State() { Abbreviations = "CA", Name = "California" });
states.Add(new State() { Abbreviations = "CO", Name = "Colorado" });
states.Add(new State() { Abbreviations = "CT", Name = "Connecticut" });
states.Add(new State() { Abbreviations = "DC", Name = "District of Columbia" });
states.Add(new State() { Abbreviations = "DE", Name = "Delaware" });
states.Add(new State() { Abbreviations = "FL", Name = "Florida" });
states.Add(new State() { Abbreviations = "GA", Name = "Georgia" });
states.Add(new State() { Abbreviations = "HI", Name = "Hawaii" });
states.Add(new State() { Abbreviations = "ID", Name = "Idaho" });
states.Add(new State() { Abbreviations = "IL", Name = "Illinois" });
states.Add(new State() { Abbreviations = "IN", Name = "Indiana" });
states.Add(new State() { Abbreviations = "IA", Name = "Iowa" });
states.Add(new State() { Abbreviations = "KS", Name = "Kansas" });
states.Add(new State() { Abbreviations = "KY", Name = "Kentucky" });
states.Add(new State() { Abbreviations = "LA", Name = "Louisiana" });
states.Add(new State() { Abbreviations = "ME", Name = "Maine" });
states.Add(new State() { Abbreviations = "MD", Name = "Maryland" });
states.Add(new State() { Abbreviations = "MA", Name = "Massachusetts" });
states.Add(new State() { Abbreviations = "MI", Name = "Michigan" });
states.Add(new State() { Abbreviations = "MN", Name = "Minnesota" });
states.Add(new State() { Abbreviations = "MS", Name = "Mississippi" });
states.Add(new State() { Abbreviations = "MO", Name = "Missouri" });
states.Add(new State() { Abbreviations = "MT", Name = "Montana" });
states.Add(new State() { Abbreviations = "NE", Name = "Nebraska" });
states.Add(new State() { Abbreviations = "NH", Name = "New Hampshire" });
states.Add(new State() { Abbreviations = "NJ", Name = "New Jersey" });
states.Add(new State() { Abbreviations = "NM", Name = "New Mexico" });
states.Add(new State() { Abbreviations = "NY", Name = "New York" });
states.Add(new State() { Abbreviations = "NC", Name = "North Carolina" });
states.Add(new State() { Abbreviations = "NV", Name = "Nevada" });
states.Add(new State() { Abbreviations = "ND", Name = "North Dakota" });
states.Add(new State() { Abbreviations = "OH", Name = "Ohio" });
states.Add(new State() { Abbreviations = "OK", Name = "Oklahoma" });
states.Add(new State() { Abbreviations = "OR", Name = "Oregon" });
states.Add(new State() { Abbreviations = "PA", Name = "Pennsylvania" });
states.Add(new State() { Abbreviations = "RI", Name = "Rhode Island" });
states.Add(new State() { Abbreviations = "SC", Name = "South Carolina" });
states.Add(new State() { Abbreviations = "SD", Name = "South Dakota" });
states.Add(new State() { Abbreviations = "TN", Name = "Tennessee" });
states.Add(new State() { Abbreviations = "TX", Name = "Texas" });
states.Add(new State() { Abbreviations = "UT", Name = "Utah" });
states.Add(new State() { Abbreviations = "VT", Name = "Vermont" });
states.Add(new State() { Abbreviations = "VA", Name = "Virginia" });
states.Add(new State() { Abbreviations = "WA", Name = "Washington" });
states.Add(new State() { Abbreviations = "WV", Name = "West Virginia" });
states.Add(new State() { Abbreviations = "WI", Name = "Wisconsin" });
states.Add(new State() { Abbreviations = "WY", Name = "Wyoming" });
return states.ToList();
}
VB.NET
Public Shared Function CreateStateList() As IList(Of State)
Dim states As New List(Of State)()
states.Add(New State() With {.Abbreviation = "AL", .Name = "Alabama"})
states.Add(New State() With {.Abbreviation = "AK", .Name = "Alaska"})
states.Add(New State() With {.Abbreviation = "AR", .Name = "Arkansas"})
states.Add(New State() With {.Abbreviation = "AZ", .Name = "Arizona"})
states.Add(New State() With {.Abbreviation = "CA", .Name = "California"})
states.Add(New State() With {.Abbreviation = "CO", .Name = "Colorado"})
states.Add(New State() With {.Abbreviation = "CT", .Name = "Connecticut"})
states.Add(New State() With {.Abbreviation = "DC", .Name = "District of Columbia"})
states.Add(New State() With {.Abbreviation = "DE", .Name = "Delaware"})
states.Add(New State() With {.Abbreviation = "FL", .Name = "Florida"})
states.Add(New State() With {.Abbreviation = "GA", .Name = "Georgia"})
states.Add(New State() With {.Abbreviation = "HI", .Name = "Hawaii"})
states.Add(New State() With {.Abbreviation = "ID", .Name = "Idaho"})
states.Add(New State() With {.Abbreviation = "IL", .Name = "Illinois"})
states.Add(New State() With {.Abbreviation = "IN", .Name = "Indiana"})
states.Add(New State() With {.Abbreviation = "IA", .Name = "Iowa"})
states.Add(New State() With {.Abbreviation = "KS", .Name = "Kansas"})
states.Add(New State() With {.Abbreviation = "KY", .Name = "Kentucky"})
states.Add(New State() With {.Abbreviation = "LA", .Name = "Louisiana"})
states.Add(New State() With {.Abbreviation = "ME", .Name = "Maine"})
states.Add(New State() With {.Abbreviation = "MD", .Name = "Maryland"})
states.Add(New State() With {.Abbreviation = "MA", .Name = "Massachusetts"})
states.Add(New State() With {.Abbreviation = "MI", .Name = "Michigan"})
states.Add(New State() With {.Abbreviation = "MN", .Name = "Minnesota"})
states.Add(New State() With {.Abbreviation = "MS", .Name = "Mississippi"})
states.Add(New State() With {.Abbreviation = "MO", .Name = "Missouri"})
states.Add(New State() With {.Abbreviation = "MT", .Name = "Montana"})
states.Add(New State() With {.Abbreviation = "NE", .Name = "Nebraska"})
states.Add(New State() With {.Abbreviation = "NH", .Name = "New Hampshire"})
states.Add(New State() With {.Abbreviation = "NJ", .Name = "New Jersey"})
states.Add(New State() With {.Abbreviation = "NM", .Name = "New Mexico"})
states.Add(New State() With {.Abbreviation = "NY", .Name = "New York"})
states.Add(New State() With {.Abbreviation = "NC", .Name = "North Carolina"})
states.Add(New State() With {.Abbreviation = "NV", .Name = "Nevada"})
states.Add(New State() With {.Abbreviation = "ND", .Name = "North Dakota"})
states.Add(New State() With {.Abbreviation = "OH", .Name = "Ohio"})
states.Add(New State() With {.Abbreviation = "OK", .Name = "Oklahoma"})
states.Add(New State() With {.Abbreviation = "OR", .Name = "Oregon"})
states.Add(New State() With {.Abbreviation = "PA", .Name = "Pennsylvania"})
states.Add(New State() With {.Abbreviation = "RI", .Name = "Rhode Island"})
states.Add(New State() With {.Abbreviation = "SC", .Name = "South Carolina"})
states.Add(New State() With {.Abbreviation = "SD", .Name = "South Dakota"})
states.Add(New State() With {.Abbreviation = "TN", .Name = "Tennessee"})
states.Add(New State() With {.Abbreviation = "TX", .Name = "Texas"})
states.Add(New State() With {.Abbreviation = "UT", .Name = "Utah"})
states.Add(New State() With {.Abbreviation = "VT", .Name = "Vermont"})
states.Add(New State() With {.Abbreviation = "VA", .Name = "Virginia"})
states.Add(New State() With {.Abbreviation = "WA", .Name = "Washington"})
states.Add(New State() With {.Abbreviation = "WV", .Name = "West Virginia"})
states.Add(New State() With {.Abbreviation = "WI", .Name = "Wisconsin"})
states.Add(New State() With {.Abbreviation = "WY", .Name = "Wyoming"})
Return states.ToList()
End Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With