怎样判断comboBox是选择当前项的上面项还是选择当前项的下面项  

解决方案 »

  1.   

    有属性得,取得当前项得Index,然后再取得新项得Index,比大小
      

  2.   

    要选择项的index是comboBox.SelectedIndex,那么当前comboBox里面显示的内容的index呢?? 怎么获取
      

  3.   

    combobox.text是当前得内容,然后遍历所有得Item,找到当前项,然后取其Index,更简单得方法就不知道了
      

  4.   

    using System;
    using System.Windows.Forms;class Test : Form
    {
      int OldSelectedIndex = -1;  Test()
      {
        ComboBox cbb = new ComboBox();
        cbb.Parent = this;
        cbb.Items.AddRange(new string[]{"AAA","BBB","CCC","DDD"});
        cbb.SelectedIndexChanged += new EventHandler(CbbSelectedIndexChanged);
      }
      
      void CbbSelectedIndexChanged(object o, EventArgs e)
      {
        int SelectedIndex = (o as ComboBox).SelectedIndex;
        string s = string.Format("选择前的索引:{0}\r\n选择后的索引:{1}\r\n选择的是当前项的{2}面项",
          OldSelectedIndex, SelectedIndex, SelectedIndex > OldSelectedIndex ? "下" : "上");
        MessageBox.Show(s);
        OldSelectedIndex = SelectedIndex;
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  5.   

    楼上的强 学习了
    string.format 都还不会 呵呵
      

  6.   

    using System;
    using System.Windows.Forms;class Test : Form
    {
      int index0 = -1;  Test()
      {
        ComboBox cbb = new ComboBox();
        cbb.Parent = this;
        cbb.Items.AddRange(new string[]{"AAA","BBB","CCC","DDD"});
        cbb.SelectedIndexChanged += new EventHandler(CbbSelectedIndexChanged);
      }
      
      void CbbSelectedIndexChanged(object o, EventArgs e)
      {
        int index = (o as ComboBox).SelectedIndex;
        string s = string.Format("选择前的索引:{0}\r\n选择后的索引:{1}\r\n选择的是当前项{2}",
          index0, index, index == index0 ? "" : (index < index0 ? "的上面项" : "的下面项"));
        MessageBox.Show(s);
        index0 = index;
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }