在ListBox中实现多重选项,第一个选中就在其内容后面标识1,第二个选中就的标识2,以此类推X 1
Y 2
Z 3如果取消选项,将重排数字,就是将其后面的序号提前。X 1
Z 2

解决方案 »

  1.   

    在ListBox中实现多重选项,第一个选中就在其内容后面标识1,第二个选中就的标识2,以此类推比如:ListBox有3个选项,X,Y,Z,首先选择X,所以在X后面加1.....
    X 1
    Y 2
    Z 3如果取消选项,将重排数字,就是将其后面的序号提前。现在有3个选项都选中,现在再选择Y,,将会是下面的结果X 1
    Y
    Z 2
      

  2.   

    private Dictionary<string, int> m_AllItems = new Dictionary<string, int>();        private int m_Index = 1;string strSelText = listBox1.SelectedItem.ToString();
                if (!m_AllItems.ContainsKey(strSelText))
                {
                    m_AllItems.Add(strSelText, m_Index);
                    m_Index++;
                }
                else
                {
                    m_AllItems.Remove(strSelText);
                    foreach (string key in m_AllItems.Keys)
                    {
                        m_AllItems[key]--;
                    }
                    m_Index--;
                }            this.textBox1.Text = "";
                foreach (string strKey in m_AllItems.Keys)
                {
                    this.textBox1.Text += strKey + " " + m_AllItems[strKey].ToString() + " ";
                }
      

  3.   

    Dictionary,命名空间是什么??
      

  4.   

    是不是写在listBox1_SelectedIndexChanged事件中
      

  5.   

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        string s = "";
        if (((ListBox)sender).GetSelected(e.Index))
        {
            int j = 0;
            for (int i = 0; i <= e.Index; i++)
                if (((ListBox)sender).GetSelected(i)) j++;
            s = "    " + j.ToString();
        }
        e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString() + s,
            e.Font, new SolidBrush(e.ForeColor), e.Bounds, 
            StringFormat.GenericDefault);
        e.DrawFocusRectangle();
    }private void listBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        ((ListBox)sender).Invalidate();
    }private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.DrawMode = DrawMode.OwnerDrawFixed;
    }
      

  6.   

    private System.Collections.ArrayList SelectedItems;
            public Form1()
            {
                InitializeComponent();
                listBox1.DrawMode = DrawMode.OwnerDrawFixed;
                SelectedItems = new System.Collections.ArrayList();
            }        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();            string s = "";
                int j =                 SelectedItems.IndexOf(this.listBox1.Items[e.Index]);
                if( j>-1)
                    s = "    " + (j+1).ToString();            e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString() + s,
                    e.Font, new SolidBrush(e.ForeColor), e.Bounds,
                    StringFormat.GenericDefault);
                e.DrawFocusRectangle();
            }        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
            {
                    // 删除掉被取消选中的项
                    for (int i = 0; i < SelectedItems.Count; i++)
                    {
                        if (!this.listBox1.SelectedItems.Contains(SelectedItems[i]))
                        {
                            this.SelectedItems.RemoveAt(i);
                            i--;
                        }
                    }
                
                    // 加上被选中的项             
                    for (int i = 0; i < this.listBox1.SelectedItems.Count; i++)
                    {
                        if (!SelectedItems.Contains(this.listBox1.SelectedItems[i]))
                        {
                            SelectedItems.Add(this.listBox1.SelectedItems[i]);                        
                        }
                    }            ((ListBox)sender).Invalidate();
            }---------------------
    调试通过了,zswang(伴水清清)(专家门诊清洁工)的Draw解决的大问题。