private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("学校");
            comboBox1.Items.Add("学生");
            comboBox1.Items.Add("成绩");
        }
然后在选择控键button1
private void button1_Click(object sender, EventArgs e)
        {
            
        }
中该写什么代码获取当combox选中“学校”、“学生”、“成绩”时的值?
public const byte school = 0;
public const byte student = 1;
public const byte score = 2;
当combox选中一个字符就对应上面定义的常量0、1、2
该怎么做呢?

解决方案 »

  1.   

     MessageBox.Show(this.comboBox1.Text);
      

  2.   

    只能用SelectedIndex然后转换了。
      

  3.   

    不懂啊~~~~~
    比如在combox中选择“学校”后我得到数字“0”,不是要combox.text的显示出来
      

  4.   

    private void button1_Click(object sender, EventArgs e)
    {
                int n=-1;
                switch (comboBox1.SelectedValue.ToString())
                {
                    case "学校": n = 0; break;
                    case "学生": n = 1; break;
                    case "成绩": n = 2; break;
                }
                MessageBox.Show(n.ToString());
    }
      

  5.   

      class item
        {
            public string Txt { get; set; }
            public string value { get; set; }        public override string ToString()
            {
                return Txt;
            }
        }
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }             private void Form1_Load(object sender, EventArgs e)
            {            item school = new item() {Txt="学校",value="0" };
                item student = new item() { Txt = "学生", value = "1" };
                item score = new item() { Txt = "成绩", value = "2" };
                comboBox1.Items.Add(school);
                
                comboBox1.Items.Add(student);
                comboBox1.Items.Add(score);
            }        private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show((this.comboBox1.SelectedItem as item).value);
            }
        }行不?
      

  6.   

                
                //comboBox1.Items.Add("学校"); //自动对应SelectedIndex为0
                //comboBox1.Items.Add("学生"); //自动对应SelectedIndex为1
                //comboBox1.Items.Add("成绩"); //自动对应SelectedIndex为2
                
                //SelectedIndex是从0开始的索引
                //如果你把Item按需要的顺序插入
                //就能得到你想到的0,1,2,3的值
                //如:
                int v = comboBox1.SelectedIndex;
      

  7.   

    可以用到 KeyValuePair 进行设置,binding