listBox 怎样点右键选中一行数据

解决方案 »

  1.   

    自己处理一下mousedown事件不就可以了?
      

  2.   

    知道在mousedown里写
            private void lb_planList_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    //lb_planList.SelectedIndex = ........不会哒
                }
            }
      

  3.   

    在响应mousedown事件之后可以有两套方法:
    1:模拟一次左键单击;
    2:自己计算点击位置的ITEM我给出方案2的代码:private void listBox1_MouseDown(object sender, MouseEventArgs e)
            {
                int height = 0;
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    height += listBox1.GetItemHeight(i);
                    if (e.Y <= height)
                    {
                        listBox1.SelectedIndex = i;
                        return;
                    }
                }
            }
    注意:上述代码只检测了高度信息,对于多列的listbox需要修改代码。
      

  4.   

            private void listBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                    for(int i=0;i!=listBox1.Items.Count;i++)
                    {
                       Rectangle ItemRectangle=listBox1.GetItemRectangle(i);                   if (ItemRectangle.Contains(new Point(e.X, e.Y)))
                       {
                           listBox1.SelectedIndex = i;
                           break;
                       }
                        
                    }
                   
                }
            }
      

  5.   

    看到2楼的代码让我想起来了,还需要添加上判断左右键的条件,对于左键也这样处理一次不合适:)
    private void listBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right)
                {
                  int height = 0;
                  for (int i = 0; i < listBox1.Items.Count; i++)
                  {
                    height += listBox1.GetItemHeight(i);
                    if (e.Y <= height)
                    {
                        listBox1.SelectedIndex = i;
                        return;
                    }
                  }
                }
            }