Sara S.
Sara S.

Reputation: 1375

How to select ListViewItem with left mouse button only?

How can i prevent the right click from selecting an item in my listview both in click and double click?

Upvotes: 1

Views: 5384

Answers (3)

jm.
jm.

Reputation: 23734

Subclass the listview and add this override:

protected override void WndProc(ref Message m)
        {
            const int WM_RBUTTONUP = 0x0205;
            const int WM_RBUTTONDOWN = 0x0204;

            if ((m.Msg != WM_RBUTTONDOWN) && (m.Msg != WM_RBUTTONUP))
            {
                base.WndProc(ref m);
            }
        }

This works by ignoring messages from the right button of the mouse.

Upvotes: 1

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

You could use this code, I think it should do the work. You need to set some bool variable to indicate that the right mouse has been clicked in your MouseDown, then Clear selected items, if SelectedIndexChanged event handler fired because of the right click and then reset the indicator on MouseUp event. Check the code:

bool rightClicked = false;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        rightClicked = true;
    }
    else
    {
        rightClicked = false;
    }
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (rightClicked)
    {
        listView1.SelectedItems.Clear();
    }

}

private void listView1_MouseUp(object sender, MouseEventArgs e)
{
    rightClicked = false;
}

EDIT: This is the best I could do, it preserves the selection but flickers. the solution could be implemented using some custom drawing of the items but that requires too much time. I leave that to you.

bool rightClicked = false;
int [] lviListIndex = null;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
      if (e.Button == System.Windows.Forms.MouseButtons.Right)
      {
            rightClicked = true;
            lviListIndex = new int[listView1.SelectedItems.Count];
            listView1.SelectedIndices.CopyTo(lviListIndex, 0);
      }
      else
      {
            rightClicked = false;
      }
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
      if (rightClicked)
      {
            listView1.SelectedIndices.Clear();   
      }
}

private void listView1_MouseUp(object sender, MouseEventArgs e)
{
      if (rightClicked)
      {
            listView1.SelectedIndexChanged -= new System.EventHandler(listView1_SelectedIndexChanged);
            if (lviListIndex != null)
            {
                 foreach (int index in lviListIndex)
                 {
                      listView1.SelectedIndices.Add(index);
                 }
            }
            lviListIndex = null;
            listView1.SelectedIndexChanged += new System.EventHandler(listView1_SelectedIndexChanged);
      }
      rightClicked = false;
}

Upvotes: 2

Ravindra Bagale
Ravindra Bagale

Reputation: 17665

you can check like this if (e.Button == MouseButtons.Left)

Upvotes: -1

Related Questions