Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LISTVIEW 1

Status
Not open for further replies.

daveatdbc

Technical User
Apr 20, 2010
7
CA
I have a listview with several items. I execute the FindItem to select and highlight a specific item. If there are many items available in the listview and the item I find/highlight is not currently in view, how can I automatically "scroll" to the found/selected item?
 
Matt,

Thanks for the response but there does not appear to be a SetTop function to call for the listview object. Can I send you a document with screen images of the issue?
 
Sorry, I was thinking of a list box. For a list view you need to use a reference to the listview item.

Code example (within the listview control)
Code:
listviewitem  lvi_1
long li_rc
// unselect old value
li_rc = This.GetItem(<old_indexvalue>, lvi_1)
lvi_1.HasFocus=FALSE
lvi_1.selected=FALSE
li_rc = This.SetItem(<old_indexvalue> , lvi_1)

// select new value
li_rc = This.GetItem(<new_indexvalue> , lvi_1)
lvi_1.HasFocus=TRUE
lvi_1.selected=TRUE
li_rc = This.SetItem(<new_indexvalue> , lvi_1)

Matt

"Nature forges everything on the anvil of time"
 
Matt,

The above code selects the item but does not scroll or make the item visible in the window. My listview has several icons and a vertical scroll bar. I am trying to scroll so that the selected item is visible in the listview "window".

I can provide a document to shows the issue if that may help.

Thanks again

David
 
Okay, try this.
First declare an external function as follows
Code:
Subroutine keybd_event( int bVk, int bScan, int dwFlags, int dwExtraInfo) Library "user32.dll"

On the listview control declare an event with two parameters (oldindex and newindex) with the following code:

Code:
listviewitem  lvi_1
long li_rc
// unselect old value
IF ai_oldindex > 0 THEN
	li_rc = This.GetItem(ai_oldindex, lvi_1)
	lvi_1.HasFocus=FALSE
	lvi_1.selected=FALSE
	li_rc = This.SetItem(ai_oldindex , lvi_1)
END IF
// select new value
li_rc = This.GetItem(ai_newindex , lvi_1)
lvi_1.HasFocus=TRUE
lvi_1.selected=TRUE
li_rc = This.SetItem(ai_newindex , lvi_1)

this.setfocus()

// simulate up arrow then down arrow to move selected item into visible portion of the control
IF ai_newindex < this.totalitems( ) AND ai_newindex > 1 THEN
	// decimal value of the virtural keyboard up arrow (0x26)
	keybd_event( 38, 1, 0, 0)
	// decimal value of the virtural keyboard down arrow (0x28)
	keybd_event(40,1,0,0)
ELSEIF ai_newindex = 1 THEN
	// decimal value of the virtural keyboard down arrow (0x28)
	keybd_event(40,1,0,0)
	// decimal value of the virtural keyboard up arrow (0x26)
	keybd_event( 38, 1, 0, 0)
END IF

Now after you manipulate the entries in the listview, you can call the above event with the index values and it will scroll that item into view.

I have posted this to my blog as well.

Matt

"Nature forges everything on the anvil of time"
 
Matt,

Not that I understand what is being done, but this code worked and I am thankful for it.

Regards

David Gauthier
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top