How to get index of ListView item on right click

5 posts, 1 answers
  1. mohammad reza
    mohammad reza avatar
    9 posts
    Member since:
    Aug 2016

    Posted 22 Sep Link to this post

    How do I get the index of a ListView item when the user right clicks on it?

    Note: I do not want to set the SelectedItem of the ListView and get SelectedIndex. 

    Thank you,

    Mohammad

  2. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    137 posts

    Posted 22 Sep Link to this post

    Hello Mohammad,

    I have moved your latest question from this thread as it is a different topic and needs a new forum thread.

    Here are three ways you can accomplish what you're looking for. I recommend option #1, but depending on what you're try to use the index for you might find the other options more suitable.

    First, make sure you're hooked into the MouseUp event:

    radListView1.MouseUp += RadListView1_OnMouseUp;


    Here's the handler for MouseUp:

    private void RadListView1_OnMouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
                // Get the VisualItem, you can use the base class (BaseListViewVisualItem) or your custom item (MyCustomVisualItem)
                var visualItem = radListView1.ElementTree.GetElementAtPoint(e.Location) as BaseListViewVisualItem;
     
                // Get the data from the visual item
                var dataItem = visualItem?.Data;
     
                if (dataItem == null) return;
     
     
                //***** Three approaches to get index ********//
     
                // ---- Approach 1 (recommended) ---- //
     
                // Get the index of the item in the RadListView
                var index = radListView1?.Items?.IndexOf(dataItem);
     
     
                // ---- Approach 2 ---- //
                // Get the index of the item in the bound collection
     
                // Get the DataBoundItem
                //var employee = dataItem?.DataBoundItem as Employee;
                //if (employee == null) return;
     
                // Get the index of the item int he original datasource
                //var index = employees.FindIndex(em => em == employee);
     
     
                // ---- Approach 3 ---- //
                // select the item programmatically and get SelectedIndex
     
                //radListView1.SelectedItem = dataItem;
                //var index = radListView1.SelectedIndex;
     
                //*********************************************//
     
                Debug.WriteLine($"SelectedIndex is: {index}");
        }
    }


    Please let us know if you have any further questions or conerns. Thank you for contacting Telerik Support.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. mohammad reza
    mohammad reza avatar
    9 posts
    Member since:
    Aug 2016

    Posted 22 Sep in reply to Lance | Tech Support Engineer, Sr. Link to this post

    thanks lance. I love telerik support.

    but when I run your code:

    the variable "visualItem " is null and is the type "RadButtonElement" not the "BaseListViewVisualItem".

    that is reasonable because I right click on my custom item button. isn't it?

  5. Answer
    Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    137 posts

    Posted 22 Sep Link to this post

    Hello Mohammad,

    You're correct. If you're going to have hit targets that are not the ListViewItem, then you can climb the element tree until you get to the BaseListViewVisualItem. 

    For example, you can use the approach where you check the type first, if it is not the type you want, then climb the element tree until you find the right type:

    // Just accept whatever type it is first
    var clickedItem = radListView1.ElementTree.GetElementAtPoint(e.Location);
     
    // Check it's type, if it's not then climb the tree until you find the base
    var visualItem = clickedItem as BaseListViewVisualItem ?? clickedItem.FindAncestor<BaseListViewVisualItem>();
     
    // Just a little extra defensive programming
    if (visualItem == null) return;


    That will work even if you right click on a button.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  6. mohammad reza
    mohammad reza avatar
    9 posts
    Member since:
    Aug 2016

    Posted 23 Sep in reply to Lance | Tech Support Engineer, Sr. Link to this post

    thanks a lot Lance.

    "FindAncestor" works greate !!!

     

Back to Top
UI for WinForms is Visual Studio 2017 Ready