我有一个显示基本教育细节的组合框,它有三个类别:学士、文凭和学校教育。我需要给出这三个类别,因为每个标题都有不同的值。是否可以在c#中的windows窗体中执行此操作?
发布于 2014-09-25 17:51:18
您希望获得与显示的文本(学士、文凭和学校教育)不同的值,对吗?
如果是这样的话,您可以按照如下方式实现:
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
comboBox1.Items.Add(new MyComboItem("Bachelor",0));
comboBox1.Items.Add(new MyComboItem("Diploma", 1));
comboBox1.Items.Add(new MyComboItem("Schooling", 2));
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (comboBox1.SelectedIndex >= 0) {
MyComboItem item = (MyComboItem)comboBox1.SelectedItem;
MessageBox.Show("value of " + item.Text + " is : " + item.Value);
}
}
}
public class MyComboItem {
private string text;
private int value;
public string Text { get { return this.text; } }
public int Value { get { return this.value; } }
public MyComboItem(string text, int value) {
this.text = text;
this.value = value;
}
public override string ToString() {
return this.text;
}
}
}https://stackoverflow.com/questions/26033540
复制相似问题