1)创建一个Windows窗体应用程序项目
2)从工具箱中,将一个ComboBox拖入Form1
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//各种类型女孩的列表
Listgrils = new List ();
public Form1()
{
InitializeComponent();
grils.Add(new Girl() { GrilType = "极品", Grade = 100 });
grils.Add(new Girl() { GrilType = "端庄", Grade = 80 });
grils.Add(new Girl() { GrilType = "普通", Grade = 60 });
grils.Add(new Girl() { GrilType = "恐龙", Grade = 0 });
//在combobox中显示
comboBox1.Items.AddRange(grils.ToArray());
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Girl g = (Girl)comboBox1.SelectedItem;
MessageBox.Show("女孩类型:" +
g.GrilType +
"; 级别分:" +
g.Grade.ToString());
}
}
//女孩类型描述
class Girl
{
//类型
public string GrilType { get; set; }
//级别
public int Grade { get; set; }
public override string ToString()
{
return GrilType;
}
}
}
3)运行
给Combobox付两个值,一个显示的值,一个后台得到的值。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();//创建一个数据集
dt.Columns.Add("id", typeof(String));
dt.Columns.Add("val", typeof(String));
DataRow dr = dt.NewRow();
dr[0] = "nb";
dr[1] = "hnb";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "nb1";
dr[1] = "hnb1";
dt.Rows.Add(dr);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "id";//id这个字段为显示的值
comboBox1.ValueMember = "val";//val这个字段为后台获取的值
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = comboBox1.Text;//获取显示的值
textBox2.Text = comboBox1.SelectedValue.ToString();//获取后台的值
}
}
你要用combobox吗?
这个问题太大。