Mouse Right Click

11 posts, 1 answers
  1. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 28 May 2012 Link to this post

    Is it possible to select an item in a ListView using the mouse right button?

    Thanks,

    Javier Gonzalez de Aragon
  2. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 31 May 2012 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 31 May 2012 Link to this post

    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
  5. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 01 Jun 2012 Link to this post

    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 >>
  6. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 01 Jun 2012 Link to this post

    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
  7. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 06 Jun 2012 Link to this post

    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 >>
  8. Javier Gonzalez de Aragon
    Javier Gonzalez de Aragon avatar
    35 posts
    Member since:
    Mar 2010

    Posted 07 Jun 2012 Link to this post

    Thanks, that did the trick.

    Javier
  9. Cesar
    Cesar avatar
    6 posts
    Member since:
    Apr 2014

    Posted 15 May 2014 in reply to Ivan Todorov Link to this post

    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;
            }
  10. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 16 May 2014 Link to this post

    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.
     
  11. 민국
    민국 avatar
    2 posts
    Member since:
    Nov 2015

    Posted 18 Jan in reply to Ivan Todorov Link to this post

    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;
                            }

             }

     

  12. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 18 Jan Link to this post

    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
Back to Top
UI for WinForms is Visual Studio 2017 Ready