标签:des style blog http color os io 使用 ar
System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>(); dict.Add("baidu.com", "百度"); dict.Add("goolge.com", "谷歌"); dict.Add("qq.com", "腾讯"); //绑定数据 comboBox1.DataSource = new BindingSource(dict, null); comboBox1.ValueMember = "Key";//文本对应的值 comboBox1.DisplayMember = "Value";//显示的文本 //绑定选择事件 comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged); //专门存储字符串健值的集合类 //健值都是string类型 System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary(); sd.Add("taobao.com", "淘宝"); sd.Add("tamll.com", "天猫"); sd.Add("jd.com", "京东"); comboBox2.DataSource = new BindingSource(sd, null); comboBox2.ValueMember = "Key"; comboBox2.DisplayMember = "Value"; comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
完整代码:
/****************************************************************** * 创建人:黄愿 * 创建时间:2014-9-9 11:34:39 * 说明:C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中 * Email:huangyuan413026@163.com *******************************************************************/ using System; using System.Windows.Forms; namespace HTL { public partial class BindDictionaryToDatasource : Form { public BindDictionaryToDatasource() { InitializeComponent(); } private void BindDictionaryToDatasource_Load(object sender, EventArgs e) { System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>(); dict.Add("baidu.com", "百度"); dict.Add("goolge.com", "谷歌"); dict.Add("qq.com", "腾讯"); //绑定数据 comboBox1.DataSource = new BindingSource(dict, null); comboBox1.ValueMember = "Key";//文本对应的值 comboBox1.DisplayMember = "Value";//显示的文本 //绑定选择事件 comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged); //专门存储字符串健值的集合类 //健值都是string类型 System.Collections.Specialized.StringDictionary sd = new System.Collections.Specialized.StringDictionary(); sd.Add("taobao.com", "淘宝"); sd.Add("tamll.com", "天猫"); sd.Add("jd.com", "京东"); comboBox2.DataSource = new BindingSource(sd, null); comboBox2.ValueMember = "Key"; comboBox2.DisplayMember = "Value"; comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("comboBox1信息如下:\r\n索引:" + comboBox1.SelectedIndex + ",值SelectedValue:" + comboBox1.SelectedValue + ",文本Text:" + comboBox1.Text + ",SelectedItem:" + comboBox1.SelectedItem.ToString()); } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("comboBox2信息如下:\r\n索引:" + comboBox2.SelectedIndex + ",值SelectedValue:" + comboBox2.SelectedValue + ",文本Text:" + comboBox2.Text + ",SelectedItem:" + comboBox2.SelectedItem.ToString()); } } }
namespace HTL { partial class BindDictionaryToDatasource { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.comboBox1 = new System.Windows.Forms.ComboBox(); this.comboBox2 = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Location = new System.Drawing.Point(107, 69); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 20); this.comboBox1.TabIndex = 0; // // comboBox2 // this.comboBox2.FormattingEnabled = true; this.comboBox2.Location = new System.Drawing.Point(107, 127); this.comboBox2.Name = "comboBox2"; this.comboBox2.Size = new System.Drawing.Size(121, 20); this.comboBox2.TabIndex = 1; // // BindDictionaryToDatasource // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.comboBox2); this.Controls.Add(this.comboBox1); this.Name = "BindDictionaryToDatasource"; this.Text = "BindDictionaryToDatasource"; this.Load += new System.EventHandler(this.BindDictionaryToDatasource_Load); this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.ComboBox comboBox2; } }
C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中将获取健值
标签:des style blog http color os io 使用 ar
原文地址:http://www.cnblogs.com/huangtailang/p/3962419.html