var oldW: Integer; begin oldW := Label1.Width; Label1.Anchors := [akLeft,akRight]; Label1.AutoSize := false; Label1.WordWrap := true; 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
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.can show your lfm file, what is the anchor for TLabel for?
procedure TForm1.Button1Click(Sender: TObject); Var OldW:Integer; begin OldW := Label1.Width; Label1.WordWrap := True; Label1.Width := Label1.Width Shl 2; Caption := OldW.ToString+','+Label1.Width.Tostring; end;
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 ?
experiment with that, the Anchors don't do anything until you specify a control.
I always specify the control. Please do that yourself and see what happens...
maybe your parent control isn't what you think it is ?
change your check point code to this..
Abs(Label1.Width-OldW)>1
I confirm iteh's observation. Debugged into the LCL and found that the new Label width is set correctly by DoSetBounds, but down the short procedure, if WidthChanged and WordWrap both are true, the width is reset back to its old value by AdjustSize. When I comment AdjustSize for a test then iteh's test program behaves correctly.Thank you very much for the detailed analysis of the reason!
Unfortunately, the SetBounds routines in TControl and its descendants are extremely complicated (for my understanding) and I don't know a quick solution:
The final button "(6)..." tests the AutoSize behavior which has been annoying me for some time because it ignores WordWrap and expands an already wrapped text to a single line. It should behave like Delphi which applies the AutoSize to the height only and keeps the preset width of the multi-line label.