Mirror

How to send a pageup or pagedown to a TListBox (Views: 718)


Problem/Question/Abstract:

I would like to control the pageup event of a TListBox by listbox1keydown(self,vk_next,[]); , but it produces an error message. Why?

Answer:

Calling the onKeyDown event handler directly accomplishes nothing. To make the control scroll you have to send either the key or a scroll message to the control itself. For the key that would take the following form:


procedure PostKey(hWindow: HWND; key: Word);
begin
  if IsWindow(hWindow) then
  begin
    PostMessage(hWindow, WM_KEYDOWN, key, MakeLong(0, MapVirtualKey(key, 0)));
    PostMessage(hWindow, WM_KEYUP, key, MakeLong(0, MapVirtualKey(key, 0) or $C0000000));
  end;
end;

PostKey(listbox.handle, VK_NEXT);

Since PostKey puts the messages into the message loop they will not get processed unless your code falls back to the message loop or calls Application.ProcessMessages. You could replace the PostMessage with a SendMessage in this case since the key yields no character.

Sending a scroll message directly would look like this:

listbox.perform(WM_VSCROLL, SB_PAGEDOWN, 0);
listbox.perform(WM_VSCROLL, SB_ENDSCROLL, 0);

<< Back to main page