cbUserAgent是一个combox
ArrayList list = new ArrayList(); Dictionary<string, string> useragents = new Dictionary<string, string>(); /// <summary> /// 初始化combox /// </summary> private void InitCbUserAgent() { cbUserAgent.Items.Clear(); list.Clear(); useragents.Clear(); //方法1: useragents.Add("IE7", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); useragents.Add("IE8", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); useragents.Add("IE9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); useragents.Add("IE10", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); useragents.Add("Chrome39.0", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"); useragents.Add("Firefox34.0", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"); foreach (KeyValuePair<string, string> a in useragents) { list.Add(a); } //方法2 //Hashtable myHash = null; //myHash = new Hashtable(); //myHash.Add("JIANBING", "我是中国人1"); //myHash.Add("CH", "我爱我的祖国2"); //myHash.Add("U", "你是傻大木3"); //myHash.Add("M", "我想找个MM谈恋爱4"); //myHash.Add("MM", "因为我想有人疼爱我5"); //myHash.Add("MI", "我想要个真心对我好的的女孩6"); //System.Collections.ArrayList list = new System.Collections.ArrayList(); //foreach (DictionaryEntry entry in myHash) //list.Add(entry); //绑定这该死的 下拉列表 cbUserAgent.DataSource = list; cbUserAgent.DisplayMember = "Key"; cbUserAgent.ValueMember = "Value"; //方法三: //KeyValuePair<string, string> ukp = new KeyValuePair<string, string>(); //foreach (string key in useragents.Keys) //{ // ukp = new KeyValuePair<string, string>(key, useragents[key]); // cbUserAgent.Items.Add(ukp); //} }
关于hashtable可参考:http://jambulshines.blog.163.com/blog/static/119498161200959175355/
C# winform key value型数据如何绑定ComBox (hashtable,keyvaluepair,dictionary )