Solved

Dealing with text width (in pixels)

Posted on 2002-07-14
6
170 Views
Last Modified: 2010-04-04
I am trying to build the following.

Small form containing a panel containing a label.

The purpose of this form is to flash a message to the user on screen.  There are no buttons.  The form is closed automatically by a timer.

So far so good and I think I've got most of this working sort of satisfactorily.

The procedure creating the form is as follows.

procedure TfmMDIParent.FlashMsg(Text : string ; Duration : Integer);
begin
 fmflashmessage := TfmFlashmessage.Create(self);
 fmFlashmessage.Label1.Caption := Text;
 fmFlashmessage.Timer1.Interval := Duration;
 fmFlashmessage.ShowModal;
 FreeAndNil(fmFlashmessage);
end;


The code of the flash message form is as follows

procedure TfmFlashmessage.FormCreate(Sender: TObject);
begin
// Label1.Caption := ' ';
 timer1.enabled := true;
//fmFlashmessage.Position := poScreenCenter;
end;

procedure TfmFlashmessage.Timer1Timer(Sender: TObject);
begin
 timer1.Enabled := false;
 fmFlashmessage.Close;
end;

procedure TfmFlashmessage.FormActivate(Sender: TObject);
begin
 fmFlashmessage.Width := 8 * length(label1.Caption);
end;

My question concerns the line

 fmFlashmessage.Width := 8 * length(label1.Caption);

It seems to me that this is a very crude way of making sure that the form / panel / label are wide enough to cope with a caption of

"mmm mmm mmm mmm mmm mmm mmm mmm mmm mmm mmm mmm "

but it looks a bit silly if the caption is

"iii iii iii iii iii iii iii iii iii iii iii iii "

How do I get at the width of the text in a more sensible manner.

Thank you

John

0
Comment
Question by:jbeh
6 Comments
 
LVL 8

Accepted Solution

by:
TOndrej earned 100 total points
ID: 7152136
with fmFlashMessage do
  Width := Canvas.TextWidth(Label1.Caption) + SomeMargin;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7152143
Or set the label to AutoSize, then ask the width of it.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7152148
too late :-(
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:jbeh
ID: 7152186
Thank you all - I'll try TOndrej's solution first on the probably somewhat flimsy grounds that on the face of it this will work without having to worry about the autosize setting.

John
0
 

Author Comment

by:jbeh
ID: 7152188
Hi All - Thanks all.

Easy when you know how :-)

John
0
 

Author Comment

by:jbeh
ID: 7152190
Good quick succint (?sp) answer

Thank you.

John
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question