Recent

Author Topic: [SOLVED] TCustomLabel constraints in design and runtime  (Read 1000 times)

iteh

  • New Member
  • *
  • Posts: 31
[SOLVED] TCustomLabel constraints in design and runtime
« on: September 21, 2019, 02:45:24 pm »
lazarus 2.0.4 (fpc 3.0.4)

I can not resize components based on TCustomLabel (TDBText, TLabel): if I set autosize = false, anchors akLeft and akRight and wordwrap = true, then in designtime (with the mouse) and in runtime (via label1.width := 123) I cannot change the width of the component.

if wordwrap = false, then the width changes.

if anchors = akRight only (and not akLeft), then increasing the label1.width value stretches the dimensions of the component to the left (the right border does not change, and the "left" position decreases and the width increases).

is it a bug or feature?
« Last Edit: September 22, 2019, 12:26:09 am by iteh »

jamie

  • Hero Member
  • *****
  • Posts: 1997
Re: TCustomLabel constraints in design and runtime
« Reply #1 on: September 21, 2019, 06:55:05 pm »
Its not a bug that I can see,  it's just the way you are using the anchors.

 When you want to resize the control when it is anchored you need to resize the control it is anchored to, not the control itself.

  In the designer this is hard to do because the parent control is being overlaid and mouse operations are a struggling  mess. Use the OI to select the parent control then directly change the size values.

iteh

  • New Member
  • *
  • Posts: 31
Re: TCustomLabel constraints in design and runtime
« Reply #2 on: September 21, 2019, 07:11:09 pm »
but it normally resizes (even at design time) if set wordwrap = false.

all other (tedit, tmemo, ..) components (in design time too) normally resize and with akLeft + akRight anchors.

in runtime/designtime TLabel normally get new width and resize if wordwrap = false (with akLeft + akRight anchors).

the problem only with "wordwrap = true and akLeft and akRight" and only for TCustomLabel based components.

Any other combinations (not wordwrap = true and autosize = false and akLeft and akRight) of TCustomLabel is ok and other components (for example, tmemo also with wordwrap + left and right anchors) - also is ok !

only tcustomlabel has this bug (?) .. :(


demo example: Form1: TForm, Button1: TButton, Label1: TLabel, Memo1: TMemo (all with default properties) and button1.OnClick code:

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   oldW: Integer;
  4. begin
  5.   Label1.Anchors := [akLeft,akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.  
  9.   oldW := Label1.Width;
  10.   Label1.Width := oldW * 2;
  11.  
  12.   if Label1.Width = oldW then
  13.     ShowMessage('label: anchors with wordwrap - failed')
  14.   else
  15.     ShowMessage('label: anchors with wordwrap - ok');
  16.  
  17.   Label1.WordWrap := false;
  18.   oldW := Label1.Width;
  19.   Label1.Width := oldW * 2;
  20.  
  21.   if Label1.Width = oldW then
  22.     ShowMessage('label: anchors wo wordwrap - failed')
  23.   else
  24.     ShowMessage('label: anchors wo wordwrap - ok');
  25.  
  26.   Memo1.Anchors := [akLeft,akRight];
  27.   Memo1.WordWrap := true;
  28.  
  29.   oldW := Memo1.Width;
  30.   Memo1.Width := oldW * 2;
  31.  
  32.   if Memo1.Width = oldW then
  33.     ShowMessage('memo: anchors with wordwrap - failed')
  34.   else
  35.     ShowMessage('memo: anchors with wordwrap - ok');
  36.  
  37.   Memo1.WordWrap := false;
  38.   oldW := Memo1.Width;
  39.   Memo1.Width := oldW * 2;
  40.  
  41.   if Memo1.Width = oldW then
  42.     ShowMessage('memo: anchors wo wordwrap - failed')
  43.   else
  44.     ShowMessage('memo: anchors wo wordwrap - ok');
  45. end;

and I see:

label: anchors with wordwrap - failed
label: anchors wo wordwrap - ok
memo: anchors with wordwrap - ok
memo: anchors wo wordwrap - ok

(addition)

and another bug only with TCustomLabel:

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   oldW, oldPos: Integer;
  4. begin
  5.   Label1.Anchors := [akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.  
  9.   oldW := Label1.Width;
  10.   oldPos := Label1.Left;
  11.   Label1.Width := oldW * 2;
  12.  
  13.   if (Label1.Width = oldW) or (Label1.Left <> oldPos) then
  14.     ShowMessage('label: akRight with wordwrap - failed')
  15.   else
  16.     ShowMessage('label: akRight with wordwrap - ok');
  17.  
  18.   Label1.WordWrap := false;
  19.   oldW := Label1.Width;
  20.   oldPos := Label1.Left;
  21.   Label1.Width := oldW * 2;
  22.  
  23.   if (Label1.Width = oldW) or (Label1.Left <> oldPos) then
  24.     ShowMessage('label: akRight wo wordwrap - failed')
  25.   else
  26.     ShowMessage('label: akRight wo wordwrap - ok');
  27.  
  28.   Memo1.Anchors := [akRight];
  29.   Memo1.WordWrap := true;
  30.  
  31.   oldW := Memo1.Width;
  32.   oldPos := Memo1.Left;
  33.   Memo1.Width := oldW * 2;
  34.  
  35.   if (Memo1.Width = oldW) or (Memo1.Left <> oldPos) then
  36.     ShowMessage('memo: akRight with wordwrap - failed')
  37.   else
  38.     ShowMessage('memo: akRight with wordwrap - ok');
  39.  
  40.   Memo1.WordWrap := false;
  41.   oldW := Memo1.Width;
  42.   oldPos := Memo1.Left;
  43.   Memo1.Width := oldW * 2;
  44.  
  45.   if (Memo1.Width = oldW) or (Memo1.Left <> oldPos) then
  46.     ShowMessage('memo: akRight wo wordwrap - failed')
  47.   else
  48.     ShowMessage('memo: akRight wo wordwrap - ok');
  49. end;

label: akRight with wordwrap - failed
label: akRight wo wordwrap - ok
memo: akRight with wordwrap - ok
memo: akRight wo wordwrap - ok

if this is a true bug - please send someone a request in the lazarus / fpc bugtrack, I never did this :)
« Last Edit: September 21, 2019, 08:14:33 pm by iteh »

jamie

  • Hero Member
  • *****
  • Posts: 1997
Re: TCustomLabel constraints in design and runtime
« Reply #3 on: September 21, 2019, 08:26:18 pm »
Your test is flawed,

 The first test you do discovered the width isn't what it was at the start.

 Then you decide to redefine the oldW with the current width again which is now 2 * larger because of the first test you did and carry on...

 On top of all this, the TLabel is a graphics control and depends on a parent to maintain it, thus this means you need to inform the parent of the change so that it can adjust the Tlable again...

 Basically a Label.Parent.repaint or update or something.
or maybe just doing The labelBase.Update /Repaint etc..

 I don't know what to tell you except that a TCustomLabel really isn't a good control to base things on.


iteh

  • New Member
  • *
  • Posts: 31
Re: TCustomLabel constraints in design and runtime
« Reply #4 on: September 21, 2019, 09:03:55 pm »
I specifically after each change reassign oldw to the current size, each time out of four, i.e. the test is correct (in my opinion).

about TCustomLabel - we saw a problem with the standard TDBText (which is also based on TCustomLabel), and then we repeated it with TLabel as an example.

then I will look for a replacement TDBText from third-party component packages :(

jamie

  • Hero Member
  • *****
  • Posts: 1997
Re: TCustomLabel constraints in design and runtime
« Reply #5 on: September 21, 2019, 09:16:12 pm »
Please examine your code.. You did not restore the Label.Width back to its original width but simple redefined the OldW := Label.Width witch is now a different value than what it was originally. So the second cycle of your test does not have the original width
of the control before the first test..

 Since the first test failed, it is assumed the WIDTH is reporting different form the original WIDTH, but you did not account for this on your next test inline, you simply redefined the OLDW with now a different width..

 Please try to Update the control when changing the Size before referencing the size again.



iteh

  • New Member
  • *
  • Posts: 31
Re: TCustomLabel constraints in design and runtime
« Reply #6 on: September 21, 2019, 09:35:47 pm »
(Sorry for the long phrases - I use Google translator)

I thought that the absolute size is not important (and did not return to the original one), and it is only important to check that resizing does not work correctly with certain TLabel properties, but in the same conditions it works correctly for other components (TMemo).

I checked whether the size changes at all, it does not matter from which to which, but whether the expected change is going at all.

but the same problem in design time - as soon as I set autosize = false, wordwrap = true and akLeft + akRight - I can no longer resize TLabel on the form.

but it’s worth removing wordwrap (in designtime) - and everything changes normally (in designtime).

and this is only with tlabel (well, and with tdbtext and other from TCustomLabel), all other components are normal.

I changed the code to resize it from the original - this did not help.
I also added .update - it didn’t help:

Code: Pascal  [Select]
  1. var
  2.   oldW: Integer;
  3. begin
  4.   oldW := Label1.Width;
  5.   Label1.Anchors := [akLeft,akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.   Label1.Width := oldW * 2;
  9.   Label1.Update;
  10.  
  11.   if Label1.Width = oldW then
  12.     ShowMessage('label: anchors with wordwrap - failed')
  13.   else
  14.     ShowMessage('label: anchors with wordwrap - ok');
  15.   Label1.Width := oldW;
  16.  
  17.   Label1.WordWrap := false;
  18.   Label1.Width := oldW * 2;
  19.   Label1.Update;
  20.  
  21.   if Label1.Width = oldW then
  22.     ShowMessage('label: anchors wo wordwrap - failed')
  23.   else
  24.     ShowMessage('label: anchors wo wordwrap - ok');
  25. end;

label: anchors with wordwrap - failed
label: anchors wo wordwrap - ok

That is, even in designtime we can check: two TLabels on the form (label1, label2), set akLeft and akRight for both labels, autosize = false for both labels and wordwrap = true for label1, wordwrap = false for label2.

then I try to change the width value for both in the OI - where wordwrap = true it does not change.
and only on TLabels (and TDBText), on all other components without problems..

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: TCustomLabel constraints in design and runtime
« Reply #7 on: September 21, 2019, 09:56:20 pm »
Code: Pascal  [Select]
  1. var
  2.   oldW: Integer;
  3. begin
  4.   oldW := Label1.Width;
  5.   Label1.Anchors := [akLeft,akRight];
  6.   Label1.AutoSize := false;
  7.   Label1.WordWrap := true;
  8.   Label1.Width := oldW * 2;
This code does not work. Anchors and Width may be conflicting requirements. Setting both left and right anchors of the label means that the left and right edges of the label always have a constant distance to the label's parent - whatever you type as Width will be ignored because it is calculated from the parent's width. If you want to specify the width explicitly you must anchor the label only at one side and keep the other side floating.

BTW, it is not a good idea to turn off label autosizing because the label may be truncated when your program runs in a different operating system or when text changes (maybe due to translation).

[EDIT]
Sorry - talking nonsense here... I was confusing with Align=alTop or Align= alBottom.
« Last Edit: September 21, 2019, 10:54:16 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

iteh

  • New Member
  • *
  • Posts: 31
Re: TCustomLabel constraints in design and runtime
« Reply #8 on: September 21, 2019, 10:03:06 pm »
This code does not work. Anchors and Width may be conflicting requirements. Setting both left and right anchors of the label means that the left and right edges of the label always have a constant distance to the label's parent - whatever you type as Width will be ignored

I was confused that:
1) for other (tedit, tmemo, ..) components everything works fine (the anchor to both edges does not interfere with manually changing the width)
2) for tlabel also works fine if set wordwrap to false
3) for all components (even if there is an anchor left and right) we can change the sizes in designtime - for all but tlabel / tdbtext / other from tcustomlabel.

and the problem with resizing in designtime for tlabel, anchored to left+right, only if wordwrap = true.
« Last Edit: September 21, 2019, 10:07:22 pm by iteh »

jamie

  • Hero Member
  • *****
  • Posts: 1997
Re: TCustomLabel constraints in design and runtime
« Reply #9 on: September 21, 2019, 10:06:06 pm »
I don't know what to tell you, it seems to work on my end except for maybe 1 pixel off for the after calculation.
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   OldW:Integer;
  4. begin
  5.   OldW := Label1.Width;
  6.   Label1.WordWrap := True;
  7.   Label1.Width := Label1.Width Shl 2;
  8.   Caption := OldW.ToString+','+Label1.Width.Tostring;
  9. end;                                                    
  10.  

No matter what Combination I come up with, all that does for the first time around is the second width value is always 1 higher than the original width, but this happens only once because I can keep cycling that code and the adjusted value remains constant..
 
 This is only one PIXEL, you are talking about something totally different..

 Btw. a Tpanel is parenting the Tlabel control and I can adjust the width of the Panel and the label will follow through.
 Also I setup the anchors in the OI using the anchor editor and selecting the control it to anchor to.

 I noticed you did not specify a any controls to anchor to ?


iteh

  • New Member
  • *
  • Posts: 31
Re: TCustomLabel constraints in design and runtime
« Reply #10 on: September 21, 2019, 10:16:51 pm »
I don't know what to tell you, it seems to work on my end except for maybe 1 pixel off for the after calculation.
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   OldW:Integer;
  4. begin
  5.   OldW := Label1.Width;
  6.   Label1.WordWrap := True;
  7.   Label1.Width := Label1.Width Shl 2;
  8.   Caption := OldW.ToString+','+Label1.Width.Tostring;
  9. end;                                                    
  10.  
can show your lfm file, what is the anchor for TLabel for?

I anchored only through the checkbox akLeft and akRight in OI, without the anchor editor. maybe this is the case..

Also I setup the anchors in the OI using the anchor editor and selecting the control it to anchor to.

 I noticed you did not specify a any controls to anchor to ?

no, I did not specify the control to which the anchor for the label goes. only the fact of the presence of an anchor.

I am ready to agree with all the arguments that I am doing something wrong, but I am very confused that the same actions and properties in other components work differently. the problem is with tcustomlabel and its "followers".

can you tell me - where in the LCL source code can I find the code that is responsible for redrawing TCustomLabel in designtime? to see there why the resizing (even in designtime) depends on the wordwrap parameter.

iteh

  • New Member
  • *
  • Posts: 31
Re: TCustomLabel constraints in design and runtime
« Reply #11 on: September 21, 2019, 10:24:13 pm »
please can you see what is wrong with the label properties in my attached project ?

archive from projects - here for such simple parameters I can neither increase the width of label1 in designtime nor even just set a new value for width in designtime.

but at the same time I normally set the same parameters and sizes for memo1 in the same project.

and if I remove the checkbox from wordwrap (for label1 in OI) - I can also immediately resize label1 in designtime.

where is the mistake ?

jamie

  • Hero Member
  • *****
  • Posts: 1997
Re: TCustomLabel constraints in design and runtime
« Reply #12 on: September 21, 2019, 10:25:14 pm »
If you are doing this at runtime, its a detailed step..

Use Code completion on the Control or a control and you'll see AnchorSideLeft, Top,Right etc.

Its a TAnchorSide class which I believe you can find in the help file but that field determines the control in which that side is connected to, also there is a SIDE property for you you need to specify of which side of the control you are attaching to.

 If you experiment with the Anchor editor you can get this to work in the OI..

 Right click on the control when it is setting on the form and you'll see an anchor editor.
 
 experiment with that, the Anchors don't do anything until you specify a control.

iteh

  • New Member
  • *
  • Posts: 31
Re: TCustomLabel constraints in design and runtime
« Reply #13 on: September 21, 2019, 10:30:43 pm »
experiment with that, the Anchors don't do anything until you specify a control.

it's true ? I almost never specify a control, because by default (empty, if not specified) everything works well too - the binding goes to the parent component, which is what the anchor editor says: "left empty for anchoring to parent".

I specify only akLeft and akRight, but I don’t specify which control to attach to - and an empty field (indicating only the fact of binding, without specifying the component) means "anchor to the parent component", isn’t that so? or are we talking about different things.

jamie

  • Hero Member
  • *****
  • Posts: 1997
Re: TCustomLabel constraints in design and runtime
« Reply #14 on: September 21, 2019, 10:33:16 pm »
I always specify the control. Please do that yourself and see what happens...


maybe your parent control isn't what you think it is ?
« Last Edit: September 21, 2019, 10:36:32 pm by jamie »