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

How to get index of ListView item on right click

6 Answers 2220 Views
ListView
This is a migrated thread and some comments may be shown as answers.
mohammad reza
Top achievements
Rank 1
mohammad reza asked on 22 Sep 2016, 11:30 AM

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

6 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 22 Sep 2016, 11:40 AM
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.
0
mohammad reza
Top achievements
Rank 1
answered on 22 Sep 2016, 03:48 PM

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?

0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 22 Sep 2016, 06:30 PM
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.
0
mohammad reza
Top achievements
Rank 1
answered on 23 Sep 2016, 06:45 AM

thanks a lot Lance.

"FindAncestor" works greate !!!

 

0
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 15 Aug 2017, 10:02 PM

You are the designers!

Maybe should have a specific option: radListView1..GetItemAtPoint(e.Location);

#ficaADica

Thanks,

Jefferson

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Aug 2017, 05:13 AM
Hello Jefferson, 

Thank you for writing.  

Here is a sample code snippet demonstrating how to get the ListViewDataItem by a given location:
private void radListView1_MouseDown(object sender, MouseEventArgs e)
{
    SimpleListViewVisualItem visualElement = this.radListView1.ElementTree.GetElementAtPoint(e.Location) as SimpleListViewVisualItem;
    if (visualElement != null)
    {
        ListViewDataItem item = visualElement.Data;
    }
}

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
ListView
Asked by
mohammad reza
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
mohammad reza
Top achievements
Rank 1
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or