Recent

Author Topic: TreeView Refresh  (Read 7415 times)

BIT

  • Full Member
  • ***
  • Posts: 158
TreeView Refresh
« on: September 17, 2021, 06:15:11 pm »
Hello! I have a problem again)
I fill the TreeView dynamic with folders and files, then I select the intem, but if the intem is at the end of the TreeView, it is not displayed. Clicking on any component in the form immediately shows the contents of the TreeView.

It does not help:

TreeView1.Refresh;
TreeView1.Update;
TreeView1.Repaint;
« Last Edit: September 17, 2021, 06:18:25 pm by BIT »

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #1 on: September 18, 2021, 04:40:31 am »

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #2 on: September 18, 2021, 03:02:02 pm »
I get the index from the INI file, it selects perfectly! But if the index is closer to -1, the TreeView is not drawn until you click on the form elements.

Code: Pascal  [Select][+][-]
  1.    Form1.TreeView1.Select(Form1.TreeView1.Items[IniF.ReadInteger('TreeViewSelected', 'Selected', Form1.TreeView1.Selected.Index)]);  

I can't understand why this is happening (
« Last Edit: September 18, 2021, 03:05:54 pm by BIT »

Awkward

  • Full Member
  • ***
  • Posts: 133
Re: TreeView Refresh
« Reply #3 on: September 18, 2021, 03:10:50 pm »
looks like Application.ProcessMessages call will not help? btw, how did you made close button for tabs?

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #4 on: September 18, 2021, 03:14:35 pm »
looks like Application.ProcessMessages call will not help? btw, how did you made close button for tabs?
https://forum.lazarus.freepascal.org/index.php/topic,56274.0.html

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #5 on: September 18, 2021, 03:21:11 pm »
looks like Application.ProcessMessages call will not help? btw, how did you made close button for tabs?
Did not help  Application.ProcessMessages;

dsiders

  • Hero Member
  • *****
  • Posts: 1045
Re: TreeView Refresh
« Reply #6 on: September 18, 2021, 05:33:57 pm »
looks like Application.ProcessMessages call will not help? btw, how did you made close button for tabs?
https://forum.lazarus.freepascal.org/index.php/topic,56274.0.html

I use the tab control from the JVCL package. It draws and handles close buttons.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #7 on: September 18, 2021, 06:05:09 pm »
if you wave the mouse over the area that needs updating does it appear after that ?
No, it does not appear if you click on any element on the form that has an event, then the TreeView is drawn.
At the moment, I solved the problem like this, added 1 item to Items through the property, before filling in the data I do TreeView1.Items.Clear; And everything starts to work)

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #8 on: September 18, 2021, 06:22:04 pm »
if you wave the mouse over the area that needs updating does it appear after that ?
Perhaps the problem is that I am passing LoadFileTreeViewPath (Form1.TreeView1);
and populate it in another unit.
PS: Possibly bad translation I hope you understand.

Awkward

  • Full Member
  • ***
  • Posts: 133
Re: TreeView Refresh
« Reply #9 on: September 18, 2021, 06:24:41 pm »
btw, i hope, you have paired BeginUpdate/EndUpdate for TreeView?

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #10 on: September 18, 2021, 06:45:01 pm »
btw, i hope, you have paired BeginUpdate/EndUpdate for TreeView?
I initially tried this method, it did not help.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: TreeView Refresh
« Reply #11 on: September 18, 2021, 07:11:44 pm »
It would be helpful if you'd extract the critical part into a separate compilable project which you can upload here.

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #12 on: September 18, 2021, 07:53:46 pm »
It would be helpful if you'd extract the critical part into a separate compilable project which you can upload here.
Do not forget to specify the path to your folder, otherwise it will load from 'C: \ WINDOWS \ System32 \'  Long.

BIT

  • Full Member
  • ***
  • Posts: 158
Re: TreeView Refresh
« Reply #13 on: September 18, 2021, 08:01:14 pm »
if you wave the mouse over the area that needs updating does it appear after that ?
Perhaps the problem is that I am passing LoadFileTreeViewPath (Form1.TreeView1);
and populate it in another unit.
PS: Possibly bad translation I hope you understand.

Change the focus via code to another control and back again...

MyOtherControl.SetFocus;
Application.ProcessMessages;
MyTreeview.SetFocus;
It didn't work that way. The general problem is when loading the form (FormCreate, FormShow). If you execute the code with Button it works fine.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: TreeView Refresh
« Reply #14 on: September 18, 2021, 10:56:16 pm »
Strange. Your code is correct (except for the missing TreeView.Items.BeginUpdate/EndUpdate which makes population of the tree so slow), and I've used similar code many times myself.

The only reason I could imagine is that populating the tree and selecting one of the last nodes are too close together, maybe to prepare scrolling for such a long list of items - I don't know.

Having this in mind I moved the population code into the OnCreate handler of the form and selected the last node later into the OnActivate or OnShow event. This way it works...

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PopulateTree;
  2. begin
  3.   TreeView1.Items.BeginUpdate;
  4.   try
  5.     LoadFileTreeViewPath('C:\WINDOWS\System32\', TreeView1, nil, True);
  6.   finally
  7.     TreeView1.Items.EndUpdate;
  8.   end;
  9. end;
  10.  
  11. procedure TForm1.FormCreate(Sender: TObject);
  12. begin
  13.   FOnShowHandled := false;
  14.   PopulateTree;
  15. end;
  16.  
  17. procedure TForm1.FormShow(Sender: TObject);
  18. begin
  19.   if not FOnShowHandled then
  20.   begin
  21.     TreeView1.Select(TreeView1.Items[TreeView1.Items.Count - 1]);
  22.     FOnShowHandled := true;
  23.   end;
  24. end;

The boolean flag FOnShowHandled is supposed to prevent that the selection jumps to the end when OnShow is later executed again for some reason.

 

TinyPortal © 2005-2018