Lazarus

Free Pascal => Beginners => Topic started by: BIT on September 17, 2021, 06:15:11 pm

Title: TreeView Refresh
Post by: BIT 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;
Title: Re: TreeView Refresh
Post by: BIT on September 18, 2021, 04:40:31 am
OS, TARGET?
WINDOWS 7
Title: Re: TreeView Refresh
Post by: BIT 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 (
Title: Re: TreeView Refresh
Post by: Awkward 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?
Title: Re: TreeView Refresh
Post by: BIT 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
Title: Re: TreeView Refresh
Post by: BIT 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;
Title: Re: TreeView Refresh
Post by: dsiders 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.
Title: Re: TreeView Refresh
Post by: BIT 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)
Title: Re: TreeView Refresh
Post by: BIT 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.
Title: Re: TreeView Refresh
Post by: Awkward on September 18, 2021, 06:24:41 pm
btw, i hope, you have paired BeginUpdate/EndUpdate for TreeView?
Title: Re: TreeView Refresh
Post by: BIT 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.
Title: Re: TreeView Refresh
Post by: wp 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.
Title: Re: TreeView Refresh
Post by: BIT 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.
Title: Re: TreeView Refresh
Post by: BIT 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.
Title: Re: TreeView Refresh
Post by: wp 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.
Title: Re: TreeView Refresh
Post by: wp on September 19, 2021, 12:31:57 am
It works perfectly fine over here with 2.0.4, 6 etc
I don't have these versions any more. With 2.0.8, it does NOT work, but it DOES work with 1.8.4.

We should also check whether it works on Linux or mac to see wether the issue is in widgetset code.
Title: Re: TreeView Refresh
Post by: GAN on September 19, 2021, 03:22:06 am
Works fine on Linux Mint 19.3 Mate (GTK). Lazarus 2.0.8 / FPC 3.0.4.
Title: Re: TreeView Refresh
Post by: BIT on September 19, 2021, 04:26:07 pm
I'll try to load the TreeView in a separate thread, maybe something confuses when the form is loaded.
Title: Re: TreeView Refresh
Post by: BIT on October 07, 2021, 09:26:46 pm
It seems there is a lock on the window update if the size of the window is the same as the last time an update to the window was requested.

 I am not sure if this is related to the Widget or LCL code but i had to do a hack to force an update to the combo box so that it would fully paint its border.

 I have seen code in the TControl I beleve or TWinControl that does check for the current size verses the changed size request coming in and if they are the same the request gets aborted. This may have something to do with it.

 In your OnResizeEvent which comes when the form is actually visible do this.

Code: Pascal  [Select][+][-]
  1. TreeView.Width := Treeview.Width-1;
  2. TreeView.Width := Treeview.Width+1;
  3.  
The first will be a different size than currently and allow for the control to refresh and the second puts it back to where it was.

This all happens during a cache update so there is no flicker but you should see  your TreeView display as you wish.
Thanks! I thought about it, how will I get back to the project and check it out.
TinyPortal © 2005-2018