Tác giả: Sưu tầm
Mô tả: Có những lúc chúng ta cần 1 danh sách tất cả các bang của nước Mỷ để người dùng chọn thì đoạn code sau sẽ giúp bạn đở tốn công. Cũng có thể dựa vào mô hình này để viết tất cả thành phố trong 1 nước hoặc tất cả quận trong 1 thành phố ở VN.
Mã: Chọn hết
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Collections;
-
- /// <summary>
- /// Summary description for USState
- /// </summary>
- public class USState
- {
- private string _ShortName;
- private string _LongName;
-
- public USState()
- {
- //
- // TODO: Add constructor logic here
- //
- }
-
- public USState(string LongName, string ShortName)
- {
- _ShortName = ShortName;
- _LongName = LongName;
- }
-
- public string ShortName
- {
- get
- {
- return _ShortName;
- }
- }
-
- public string LongName
- {
- get
- {
- return _LongName;
- }
- }
-
- public override string ToString()
- {
- return this.ShortName + " - " + this.LongName;
- }
-
- public static ArrayList getStates()
- {
- ArrayList arrUSState = new ArrayList();
-
- arrUSState.Add(new USState("Select a State", ""));
- arrUSState.Add(new USState("ALABAMA", "AL"));
- arrUSState.Add(new USState("ALASKA", "AK"));
- arrUSState.Add(new USState("ARIZONA", "AZ"));
- arrUSState.Add(new USState("ARKANSAS", "AR"));
- arrUSState.Add(new USState("CALIFORNIA", "CA"));
- arrUSState.Add(new USState("COLORADO", "CO"));
- arrUSState.Add(new USState("CONNECTICUT", "CT"));
- arrUSState.Add(new USState("DELAWARE", "DE"));
- arrUSState.Add(new USState("GEORGIA", "GA"));
- arrUSState.Add(new USState("HAWAII", "HI"));
- arrUSState.Add(new USState("IDAHO", "ID"));
- arrUSState.Add(new USState("ILLINOIS", "IL"));
- arrUSState.Add(new USState("INDIANA", "IN"));
- arrUSState.Add(new USState("IOWA", "IA"));
- arrUSState.Add(new USState("KANSAS", "KS"));
- arrUSState.Add(new USState("KENTUCKY", "KY"));
- arrUSState.Add(new USState("LOUISIANA", "LA"));
- arrUSState.Add(new USState("MAINE", "ME"));
- arrUSState.Add(new USState("MARYLAND", "MD"));
- arrUSState.Add(new USState("MASSACHUSETTS", "MA"));
- arrUSState.Add(new USState("MICHIGAN", "MI"));
- arrUSState.Add(new USState("MINNESOTA", "MN"));
- arrUSState.Add(new USState("MISSISSIPPI", "MS"));
- arrUSState.Add(new USState("MISSOURI", "MO"));
- arrUSState.Add(new USState("MONTANA", "MT"));
- arrUSState.Add(new USState("NEBRASKA", "NE"));
- arrUSState.Add(new USState("NEVADA", "NV"));
- arrUSState.Add(new USState("NEW HAMPSHIRE", "NH"));
- arrUSState.Add(new USState("NEW JERSEY", "NJ"));
- arrUSState.Add(new USState("NEW MEXICO", "NM"));
- arrUSState.Add(new USState("NEW YORK", "NY"));
- arrUSState.Add(new USState("NORTH CAROLINA", "NC"));
- arrUSState.Add(new USState("NORTH DAKOTA", "ND"));
- arrUSState.Add(new USState("OHIO", "OH"));
- arrUSState.Add(new USState("OKLAHOMA", "OK"));
- arrUSState.Add(new USState("OREGON", "OR"));
- arrUSState.Add(new USState("PENNSYLVANIA", "PA"));
- arrUSState.Add(new USState("RHODE ISLAND", "RI"));
- arrUSState.Add(new USState("SOUTH CAROLINA", "SC"));
- arrUSState.Add(new USState("SOUTH DAKOTA", "SD"));
- arrUSState.Add(new USState("TENNESSEE", "TN"));
- arrUSState.Add(new USState("TEXAS", "TX"));
- arrUSState.Add(new USState("UTAH", "UT"));
- arrUSState.Add(new USState("VERMONT", "VT"));
- arrUSState.Add(new USState("VIRGINIA", "VA"));
- arrUSState.Add(new USState("WASHINGTON", "WA"));
- arrUSState.Add(new USState("WEST VIRGINIA", "WV"));
- arrUSState.Add(new USState("WISCONSIN", "WI"));
- arrUSState.Add(new USState("WYOMING", "WY"));
-
- // Sort Dropdownlist
- arrUSState.Sort(new ListItemComparer());
-
- return arrUSState;
- }
-
- private class ListItemComparer : IComparer
- {
- public int Compare(object obj1, object obj2)
- {
- return ((USState)obj1)._ShortName.CompareTo(((USState)obj2)._ShortName);
- }
- }
- }
- /*------------- Exsample -----------*/
- //comboBox1.DataSource = USState.getStates();
- //comboBox1.DisplayMember = "LongName";
- //comboBox1.ValueMember = "ShortName";