Move selection highlight (in e.g. a list, grid..) upon right click?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
When we left click a grid cell, or list item, it highlights. Left click outside it and the selection jumps to the new place.

I can RIGHT click anywhere in the grid without disturbing an existing highlight, but I want it to work more like windows explorer would in terms of file selection..

i.e. if I right click inside the range, the range stays, if I right click outside the range, the range moves to a new single cell that I just right flicked.

Try it now on this page. Select some text, right click on the selection and it stays. Right click outside the selection and it goes.
In datagrid (for example), the selection can only be modified with a left click.

How can we change this so the selection behaviour of a datagrid/list is more like this text pane?



On a related note. If I left click in a textbox I can drag a selection out. If i wanted to right click and drag the selection out, would it be the same process? (It's for a custom string manipulation picker control..)
 
I solved it like:

VB.NET:
    private void SalDgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
      try {
        if (e.Button == MouseButtons.Right && !_salDgv.SelectedCells.Contains(_salDgv[e.ColumnIndex, e.RowIndex])) {
          _salDgv.CurrentCell = _salDgv[e.ColumnIndex, e.RowIndex];
     
        }
      } catch (Exception) {
        MessageBox.Show("Dont click there");
      }
    }
Warning: This is C# :)
Please use a converter if you have problems reading it
 
For a short moment, I REALLY thought that there was a question from cjard that I might be able to contribute to :D

This works for Full Row Selection:-

VB.NET:
    Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim hti As DataGridView.HitTestInfo = CType(sender, DataGridView).HitTest(e.X, e.Y)
            If hti.Type = DataGridViewHitTestType.Cell Then
                If Not DataGridView1.Rows(hti.RowIndex).Selected Then
                    ' User right clicked a row that is not selected, so throw away all other selections and select this row
                    DataGridView1.ClearSelection()
                    DataGridView1.Rows(hti.RowIndex).Selected = True
                    DataGridView1.CurrentCell = DataGridView1.Rows(hti.RowIndex).Cells(0)
                End If
            End If
        End If
    End Sub
 
Back
Top