按下Keys.Control时,列表框中的C#选择索引始终为0

时间:2017-04-03 19:25:59

标签: c# listbox controls selectedindex ctrl

我的C#中的WindowsFormApplication出了问题。我想选择要删除的项目,但是当我按下Ctrl并单击列表框时,selectedIndex始终为0.没有Ctrl就可以了。请帮我。非常感谢你。

  private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Control.ModifierKeys == Keys.Control)
        {
            if (listBox1.SelectedItem != null)
            {
                    listSelectedItems.Add(listBox1.SelectedIndex);
                    drawSelectedItems();
                    Console.WriteLine(listBox1.SelectedIndex);              
            }
            else
            {
                drawSelectedItems();

            }
            Console.WriteLine(listBox1.SelectedIndex);
        }
        else
        {
            listSelectedItems.Clear();
            Rectangle rc = listBox1.GetItemRectangle(listBox1.SelectedIndex);

            LinearGradientBrush brush = new LinearGradientBrush(
                rc, Color.Transparent, Color.White, LinearGradientMode.ForwardDiagonal);
            Graphics g = Graphics.FromHwnd(listBox1.Handle);

            g.FillRectangle(brush, rc);
            Console.WriteLine(listBox1.SelectedIndex);
        }

    }

0 个答案:

没有答案
相关问题