Assume I have an ObjectListView and one of the columns is an OLVColumn "olvcName" which displays a name as a string. When a row is selected in the ObjectListView I'd like to be able to press the 'N' key and have the selected row advance to the next row with an identical name. How can I accomplish this?
I've looked through the ObjectListView Cookbook and Getting Started as well as the demo application and don't see anything that matches this functionality. I know ObjectListView can catch key presses because I've been able to set a breakpoint in the ObjectListView.cs method HandleKeyDown() which appears to check for Keys.Space as well as index changes resulting from navigation using arrow keys. I can add code there to detect 'N' being pressed but from there I'm not sure how to accomplish the search. I feel like this should be a delegate of some kind applied to the OLVColumn olvcName.
Any assistance appreciated!
So this was an awkward one to get working!
So if you look at the
ObjectListView(OLV) you will see that it has theObjectsproperty which is an Enumerable of all of the objects you assign to it.The problem is, that this does not maintain the display order that the items/objects are shown in. In fact, as the items can be filtered then it can even have more items than are displayed.
Add to that, the fact that you can sort items and their groups, then the order gets totally messed up. So you need to use a helper Method which is part of the OLV called
GetDisplayOrderOfItemIndex(). But you first need to get the traditional item index. So you end up doing this.Next you need to try to identify which is the next item in the display order, well you still don't have a list of items in the right order. So actually we need to go through every item in display order until you find the next one which matches. So we need a helper function to help you find what is the next item in display order.
This is taken from here: https://sourceforge.net/p/objectlistview/discussion/812922/thread/850cb5fd/
Then finally, we can start to put this together into a working solution where we will;
It is still a bit of a hack, as we need to loop through all items and wouldn't be efficient on a larger dataset, but it works!
Assuming I have a data class such as below, where we will use the "Owner" property for searching.
I create my form such as
private List list;
Then my code to provide the solution is
This should work, no matter what filtering or ordering you have on the OLV.
So now, you select a row and press 'n' to move to the next row which has the same "Owner".