PowerBuilder – Selecting and scrolling to an entry in a Listview control

Posted on Wednesday, August 17th, 2011 at 8:20 pm in

The listview control in PB lacks the capability to scroll to a highlighted item within the native Powerscript. Here is an easy way to do this by simulating key strokes. First declare the following API subroutine.

keybd_event( int bVk, int bScan, int dwFlags, int dwExtraInfo) Library "user32.dll"

Next create a user event in the listview control. I called mine ue_scrolltoitem. The event has two parameters, ai_oldindex and ai_newindex. In this event put the following script.

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

This technique sets up the highlighted row as the last one in the visible portion of the control. You can modify it to ‘scroll up’ as many times as needed to put it at the top of the control should you need to do so.

Top