Lazarus

Programming => General => Topic started by: Hansvb on March 07, 2023, 06:00:57 pm

Title: Another question about creating components dynamically
Post by: Hansvb on March 07, 2023, 06:00:57 pm
Hi,

Another question about creating components dynamically.
I create a panel and then a splitter runtime. The spliiter always seems to be placed first and then the panel. eventhough I create the panel first and then the splitter.

How can i place the splitter on the right side of the panel?



Title: Re: Another question about creating components dynamically
Post by: wp on March 07, 2023, 06:17:26 pm
Set the Left property of the newly added, left-aligned control to a very large value (Parent.Width, 9999, even MaxInt seems to work) -- this makes the control the rightmost control of all left-aligned controls:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if Panel2.ControlCount > 0 then
  4.     with TSplitter.Create(self) do
  5.     begin
  6.       Align := alLeft;
  7.       Left := MaxInt;
  8.       Parent := Panel2;
  9.     end;
  10.   with TPanel.Create(Self) do
  11.   begin
  12.     Align := alLeft;
  13.     Left := MaxInt;
  14.     Parent := Panel2;
  15.   end;
  16. end;
Title: Re: Another question about creating components dynamically
Post by: Hansvb on March 07, 2023, 06:26:46 pm
Code: Pascal  [Select][+][-]
  1. Left := MaxInt;
  2.  

Gives a range check error but 
Code: Pascal  [Select][+][-]
  1. Left := 9999;

does the trick.
Title: Re: Another question about creating components dynamically
Post by: Hansvb on October 12, 2024, 07:11:17 pm
An old post, but I am again encountering the problem that dynamically created components are incorrectly placed on the form.
As an example: a page control with 2 tabs. 5 panels and splitters are created dynamically on both tabs. The panels and splitters are neatly placed in the correct order on the visible tab. On the tab that is not yet visible, the panels are placed in the reverse order. How can you create components in the correct order and place them on a parent component that is not yet visible? 

I found this, but unfortunately it doesn't get me much further.
Quote
https://wiki.freepascal.org/Example:_Anchors._How_to_reliably_align_dynamically_created_controls_under_changing_visibility
It's probably something simple but I don't see it. (I have attached an example).

Title: Re: Another question about creating components dynamically
Post by: cdbc on October 12, 2024, 07:49:35 pm
Hi Hans
Indeed a puzzling situation, without seemingly reason...  %)
Upon playing with it, I found that this works:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   PageControl1.ActivePage:= TabSheet1;
  4.   BuilPanelsAndSplitters(TabSheet1);
  5.   PageControl1.ActivePage:= TabSheet2;
  6.   BuilPanelsAndSplitters(TabSheet2);
  7.   PageControl1.ActivePage:= TabSheet1;
  8. end;
...and at least on my lappy, there's no flicker  :D
It's as far from elegant, as you can get ...I know  ;D
Regards Benny
Title: Re: Another question about creating components dynamically
Post by: Hansvb on October 12, 2024, 08:15:40 pm
Hi Benny, That is indeed some kind of workaround. Another would be to reverse the creation order for a non-visible tabsheet. (Not tested). But I hoped it could be done better. I don't understand the difference between a visible and invisible tab. By the way, I'm on Windows, I thought it might be a Windows thing but you are on linux so that's not it.
Title: Re: Another question about creating components dynamically
Post by: jamie on October 12, 2024, 08:21:38 pm
beginUpdateBounds ?

...
EndUpdateBounds ..etcc
Title: Re: Another question about creating components dynamically
Post by: wp on October 12, 2024, 08:29:41 pm
In the attachment there is a modification of your project which seems to work. The main point is that a splitter needs to be freely moveable. Therefore, a vertical splitter must not be anchored horizontally, a horizontal splitter likewise. Instead,  the neighboring controls must be anchored to the splitter sides. Another point is that anchoring requires the controls not the be aligned - but a splitter is left-aligned by default, you must set its Align to alNone. And I also noticed that panel widths seem to be reset to zero for the tabs which are not yet shown; moving the command setting the panel width to another place fixes this issue.
Title: Re: Another question about creating components dynamically
Post by: Hansvb on October 12, 2024, 09:08:56 pm
I stopped for today. Will look at it tomorrow.
Title: Re: Another question about creating components dynamically
Post by: Hansvb on October 13, 2024, 11:10:21 am
I built it into my tool. It works well. Thank you.
There is a lot I didn't know yet.

Edit...
I later found out that it only works well if all panels fit within the parent. If you adjust 5 to 15 then something goes wrong. The panels that do not fit "disappear" at the right side
Title: Re: Another question about creating components dynamically
Post by: Hansvb on October 13, 2024, 12:28:00 pm
What if I put a scrollbox on the last tab (with the 5 panels). The scroll box is then the parent for the panels. The scroll box never seems to become active because the panels always fit.
TinyPortal © 2005-2018