jbeh
asked on
Dealing with text width (in pixels)
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(sel f);
fmFlashmessage.Label1.Capt ion := Text;
fmFlashmessage.Timer1.Inte rval := 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.Timer1Time r(Sender: TObject);
begin
timer1.Enabled := false;
fmFlashmessage.Close;
end;
procedure TfmFlashmessage.FormActiva te(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
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
begin
fmflashmessage := TfmFlashmessage.Create(sel
fmFlashmessage.Label1.Capt
fmFlashmessage.Timer1.Inte
fmFlashmessage.ShowModal;
FreeAndNil(fmFlashmessage)
end;
The code of the flash message form is as follows
procedure TfmFlashmessage.FormCreate
begin
// Label1.Caption := ' ';
timer1.enabled := true;
//fmFlashmessage.Position := poScreenCenter;
end;
procedure TfmFlashmessage.Timer1Time
begin
timer1.Enabled := false;
fmFlashmessage.Close;
end;
procedure TfmFlashmessage.FormActiva
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or set the label to AutoSize, then ask the width of it.
too late :-(
ASKER
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
John
ASKER
Hi All - Thanks all.
Easy when you know how :-)
John
Easy when you know how :-)
John
ASKER
Good quick succint (?sp) answer
Thank you.
John
Thank you.
John