This is a migrated thread and some comments may be shown as answers.

Mouse Right Click

10 Answers 482 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Javier Gonzalez de Aragon
Top achievements
Rank 2
Javier Gonzalez de Aragon asked on 28 May 2012, 08:05 PM
Is it possible to select an item in a ListView using the mouse right button?

Thanks,

Javier Gonzalez de Aragon

10 Answers, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 31 May 2012, 01:22 PM
Hello Javier,

Thank you for your question.

Currently, RadListView does not provide such a built-in option, but you can achieve this behavior by handling the MouseUp event of your RadListView. The following code snippet demonstrates this:
void radListView1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        BaseListViewVisualItem item = this.radListView1.ElementTree.GetElementAtPoint(e.Location) as BaseListViewVisualItem;
        if (item != null)
        {
            this.radListView1.SelectedItem = item.Data;
        }
    }
}

I hope you find this useful. In case you have any additional questions, do not hesitate to ask.

Greetings,
Ivan Todorov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Javier Gonzalez de Aragon
Top achievements
Rank 2
answered on 31 May 2012, 05:36 PM
Thanks for the reply. I'm using Visual Basic and used the following code:
If (e.Button = System.Windows.Forms.MouseButtons.Right) Then
    Dim item As BaseListViewVisualItem = TryCast(lvMovimientos.ElementTree.GetElementAtPoint(e.Location), BaseListViewVisualItem)
    If (item IsNot Nothing) Then
        lvMovimientos.SelectedItem = item.Data
    End If
End If

but nothing happens.

Any ideas?

Thanks,

Javier Gonzalez de Aragon
0
Ivan Todorov
Telerik team
answered on 01 Jun 2012, 09:43 AM
Hello Javier,

I have tested the code you have posted in VB and it works correctly. Please make sure that your event handler is properly fired. Here is the full code of the handler in VB:
Private Sub RadListView1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RadListView1.MouseUp
    If (e.Button = System.Windows.Forms.MouseButtons.Right) Then
        Dim item As BaseListViewVisualItem = TryCast(RadListView1.ElementTree.GetElementAtPoint(e.Location), BaseListViewVisualItem)
        If (item IsNot Nothing) Then
            RadListView1.SelectedItem = item.Data
        End If
    End If
End Sub

Please let me know if you are still experiencing difficulties.

Greetings,
Ivan Todorov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Javier Gonzalez de Aragon
Top achievements
Rank 2
answered on 01 Jun 2012, 06:53 PM
If I remove the TryCast, I get the following exception:

System.InvalidCastException was unhandled
  Message=Unable to cast object of type 'Telerik.WinControls.UI.DetailListViewDataCellElement' to type 'Telerik.WinControls.UI.BaseListViewVisualItem'.


Any ideas?

Regards,

Javier
0
Accepted
Ivan Todorov
Telerik team
answered on 06 Jun 2012, 07:24 AM
Hello Javier,

The code I have previously posted is valid for RadListView in ListView or IconView mode. When your RadListView is in DetailView mode, it has some more complex structure of rows, columns and cells. Here is how you can achieve the desired behavior when RadListView is in DetailView mode:
Private Sub RadListView1_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles RadListView1.MouseUp
    If e.Button = Windows.Forms.MouseButtons.Right Then
        Dim clickedCell = TryCast(Me.RadListView1.ElementTree.GetElementAtPoint(e.Location), DetailListViewDataCellElement)
 
        If clickedCell IsNot Nothing Then
            Me.RadListView1.SelectedItem = clickedCell.Row
            Me.RadListView1.CurrentColumn = clickedCell.Data
        End If
    End If
End Sub

I hope this helps. Do not hesitate to ask if you have any further questions.

Kind regards,
Ivan Todorov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Javier Gonzalez de Aragon
Top achievements
Rank 2
answered on 07 Jun 2012, 06:14 PM
Thanks, that did the trick.

Javier
0
Cesar
Top achievements
Rank 1
answered on 16 May 2014, 12:14 AM
Always Null?

Missing property?

  if (e.Button == System.Windows.Forms.MouseButtons.Right)
      {
        BaseListViewVisualItem item = this.lvwDetalle.ElementTree.GetElementAtPoint(e.Location) as BaseListViewVisualItem;
        if (item != null)
        {
          //this.radListView1.SelectedItem = item.Data;
        }
0
Stefan
Telerik team
answered on 16 May 2014, 06:55 AM
Hello Ceasar,

I am sorry, but I am unable to understand your inquiry. Can you please elaborate?

Regards,
Stefan
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Minkook
Top achievements
Rank 1
answered on 18 Jan 2016, 07:37 AM

all of comment are wrong

 For find that when i click right button in radlistview spend fucking 2 days

 this code is as below

[radview type is detail]

 private void lv_local_MouseUp(object sender, MouseEventArgs e)
        {

             var lv = sender as RadListView;

             var item = lv.ElementTree.GetElementAtPoint(e.Location) as DetailListViewDataCellElement;
                        if (item != null)
                        {
                            lv.SelectedItem = item.RowElement.Data;
                        }

         }

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Jan 2016, 01:26 PM
Hello Minkook,

Thank you for writing.

The provided solution by IvanT Todorov from 06-Jun-2012 demonstrates a similar approach for accessing the right clicked DetailListViewDataCellElement. However, it directly uses the RadListView control, but you use the sender which is similar. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ListView
Asked by
Javier Gonzalez de Aragon
Top achievements
Rank 2
Answers by
Ivan Todorov
Telerik team
Javier Gonzalez de Aragon
Top achievements
Rank 2
Cesar
Top achievements
Rank 1
Stefan
Telerik team
Minkook
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or