Recent

Author Topic: [SOLVED]How to get real size after AutoSize  (Read 3739 times)

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
[SOLVED]How to get real size after AutoSize
« on: October 27, 2017, 04:50:22 pm »
Hi, everybody. I was adjusting my program GUI and I encountered a problem, When the size of CheckBox changed automatically, I want to get the real size. What should I do?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   CheckBox1.AutoSize := True;
  4.   CheckBox1.Caption := 'TTTTTTTTTTTTTTTest';
  5.   // CheckBox1.Width is a wrong value
  6.   Edit1.Left := CheckBox1.Left + CheckBox1.Width;
  7. end;
« Last Edit: October 29, 2017, 03:19:07 am by tomitomy »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: How to get real size after AutoSize
« Reply #1 on: October 27, 2017, 05:04:54 pm »
Read the excellent wiki article about Autosizing and Layout. At designtime use the anchor editor to link the left side of one control to the right side of the previous one etc. This is an amazing tool! You'll never calculate control positions like you did in the previous post.

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to get real size after AutoSize
« Reply #2 on: October 27, 2017, 05:13:25 pm »
Read the excellent wiki article about Autosizing and Layout. At designtime use the anchor editor to link the left side of one control to the right side of the previous one etc. This is an amazing tool! You'll never calculate control positions like you did in the previous post.

Thank you, wp. I'll read it seriously. Thank you very much!

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: How to get real size after AutoSize
« Reply #3 on: October 28, 2017, 03:08:35 am »
To calculate the real-estate the text will use you can use the
var
 aSize :TSize;
begin

aSize := Canvas.TextExtent('Your Text');
Clientwidth := aSize.cx;

End;
 This will return a TSize and  that has members CX, and CY, they report the width and height of canvas
needed.
The only true wisdom is knowing you know nothing

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to get real size after AutoSize
« Reply #4 on: October 28, 2017, 04:22:01 am »
To calculate the real-estate the text will use you can use the
var
 aSize :TSize;
begin

aSize := Canvas.TextExtent('Your Text');
Clientwidth := aSize.cx;

End;
 This will return a TSize and  that has members CX, and CY, they report the width and height of canvas
needed.

Thank you, jamie, but I don't konw how to get the CheckBox's  Canvas, Would it be a problem to get the CheckBox's  text size using the form's Canvas?

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: How to get real size after AutoSize
« Reply #5 on: October 29, 2017, 02:52:04 am »
Use the parent of the checkbox..

 aTSizeVariable := (CheckBox1.Parent as Tform).Canvas.TextExtent('mmm');

 ATsizevariable.cx ,,, cy etc.
The only true wisdom is knowing you know nothing

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to get real size after AutoSize
« Reply #6 on: October 29, 2017, 03:13:28 am »
Thank you, jamie, I have learnt it.

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to get real size after AutoSize
« Reply #7 on: October 29, 2017, 03:18:06 am »
Read the excellent wiki article about Autosizing and Layout. At designtime use the anchor editor to link the left side of one control to the right side of the previous one etc. This is an amazing tool! You'll never calculate control positions like you did in the previous post.

Thank you, wp, "Anchors" is very powerful!

 

TinyPortal © 2005-2018