How to scroll a TTreeView programmatically?
Asked Answered
T

1

6

A. Create a Delphi VCL Forms application.

B. Put a TTreeView on the form, name it tvTest and fill it with items and set the size of the Treeview, so scrollbars are visible on the TreeView, for example:

enter image description here

C. Put a button on the form and in its click handler write this code:

  procedure TForm1.btnScrollClick(Sender: TObject);
  begin
    tvTest.ScrollBy(tvTest.Width, 0);
  end;

D. Now run the program and click the button. Supposedly the horizontal scrollbar should scroll from left to right. But nothing happens. Why?

So how can I make the scrollbars (and with the scrollbars of course the content) scroll from left to right, from right to left, down or up?

Delphi 10.1 Berlin Update 2
Windows 7 x64 SP1

EDIT: When I use this code (similar to Sami's suggestion):

tvTest.ScrollBy(-3, -3);

...I get this piece of modern art:

enter image description here

Tabu answered 29/11, 2016 at 20:8 Comment(2)
try tvTest.ScrollBy(3, 3); and click the button for 5 time you will see whyHabakkuk
This makes only the CONTENT of the TreeView scroll, not the scrollbars. But after having clicked the button a few times, I dragged the form partially outside of the screen area and when I dragged it back the TreeView content was again where it was before. So the TreeView was not really scrolled, but only it SEEMED scrolled and when repainted it was like before!! Why? And how can I really scroll the Treeview?Tabu
T
7

To scroll a TreeView send it (or Perform) WM_VSCROLL and/or WM_HSCROLL messages.

tvTest.Perform(WM_HSCROLL, MakeWParam(SB_LINERIGHT, 0), 0);

or

tvTest.Perform(WM_VSCROLL, MakeWParam(SB_LINEDOWN, 0), 0);

See the corresponding documentations of messages for parameters.

ScrollBy is VCL's wrapper for ScrollWindow API, it shifts the contents of a control. It's a shortcut to paint a part of the client of a control that is scrolled, revealed (empty) parts should be additionally painted. It is normally used by internal implementation of a control. Not what you're looking for.

Tranquillize answered 29/11, 2016 at 20:50 Comment(5)
Thank you, this works, but it only scrolls a very small amount each time I press the button, regardless of which numbers I put instead of the zeros. But I need to scroll it all the way from left to right and from right to left. How can this be done?Tabu
Send multiple messages or use SB_PAGE__ constants. You're welcome.Tranquillize
Thank you very much! I also looked up SB_PAGERIGHT in Winapi.Windows. But always when I load Winapi.Windows into the IDE's editor the IDE crashes!! That's why it took so long to respond. Does anybody know why the IDE crashes when loading the Winapi.Windows unit?Tabu
@user1580348: I just opened one of the VCL demo projects (SplitView) and then opened WinAPI.Windows by clicking on it and then hitting Ctrl+Enter, and it opened fine, and I can scroll and page through it and search. Perhaps you need more RAM on your machine, or fewer files open in the IDE.Phiona
@KenWhite: I have 16 GB RAM, plenty of space on C:, and only a few files loaded in the IDE. I also open WinAPI.Windows by clicking on it and then hitting Ctrl+Enter. Several big files in the uses list also take very long time to load, most other files are loaded very fast.Tabu

© 2022 - 2024 — McMap. All rights reserved.