Board index » cppbuilder » TLabel don't display my Long strings.

TLabel don't display my Long strings.

Hi,
I-m using a TLabel to display a filename with it's full path name.
Sometimes, often actually, the text is too long to fit in the Label's width.
I have tried to use WordWrap = true but since a path doesn't contain any
spaces it still wot break the text into two lines.

Is there a way to find out how much of the text is going to be visible so I
can put an '\n' into the string?
Or , is there a way to find out how much width my string needs to be
displayed?

/Magnus

 

Re:TLabel don't display my Long strings.


On Wed, 9 Aug 2000 11:09:03 +0200, "Magnus Karlsson"

Quote
<magnus.karls...@sys.sigma.se> wrote:
>Is there a way to find out how much of the text is going to be visible so I
>can put an '\n' into the string?
>Or , is there a way to find out how much width my string needs to be
>displayed?

The API routine DrawText() supports replacing part of the string with
ellipses to make it fit, which is commonly seen. You could call it
yourself for a Canvas, wrap it in a simple component derived from
TGraphicControl or look for one out there which already does it.

Re:TLabel don't display my Long strings.


Quote
Magnus Karlsson wrote:
> Hi,
> I-m using a TLabel to display a filename with it's full path name.
> Sometimes, often actually, the text is too long to fit in the Label's width.
> I have tried to use WordWrap = true but since a path doesn't contain any
> spaces it still wot break the text into two lines.

> Is there a way to find out how much of the text is going to be visible so I
> can put an '\n' into the string?
> Or , is there a way to find out how much width my string needs to be
> displayed?

Are you setting the label's 'AutoSize' property to true?

else you can try this,

var
 tw: integer;
begin
 tw := Label1.Canvas.TextWidth(Edit1.Text);
 Label1.Width := tw;
 Label1.Caption := Edit1.Text;
end;

Good luck.

Re:TLabel don't display my Long strings.


Thank you!

This will do fine. Now I can insert newlines (\n) att appropriate places so
the string will display in two lines. (the size of the Label is limited by
the window.)

/Magnus

If anyone would be intrested, this is how I tried it:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   int Width,index ;
   Label1->Caption="";
   Width = Label1->Canvas->TextWidth(Edit1->Text);
   if (Width > Label1->Width)
   {
     index = Label1->Width * Edit1->Text.Length() / Width;

     while
(Label1->Canvas->TextWidth(Edit1->Text.SubString(0,index))>Label1->Width)
     {
       index--;
     }
     Label1->Caption = Edit1->Text.Insert(AnsiString("\n"),index);
   }
   else
     Label1->Caption = Edit1->Text;

Quote
}

Label1->AutoZose = false;
Label1->WordWrap = true;

Re:TLabel don't display my Long strings.


hi,

I have had the same problem, and I solved it:

  // click-event or what ever you want

  if (Label1->Canvas->TextWidth(s) > Label1->Width)
    Label1->Caption = MinimizeName(s, Label1->Canvas, Label1->Width-4);
  else
    Label1->Caption = s;

information:
  s...AnsiString (directory)
  Label1...AutoSize = true

it works fine and it's a little bit shorter (just a little bit ;-))

michael

--
---
 Michael Reichenbach
 reich...@web.de / reich...@gmx.de
 http://members.tripod.de/reichemi/
 http://members.tripod.de/MRK_Gera/

"Magnus Karlsson" <magnus.karls...@sys.sigma.se> schrieb im Newsbeitrag
news:39924ded$1_1@dnews...
| Thank you!
|
| This will do fine. Now I can insert newlines (\n) att appropriate places
so
| the string will display in two lines. (the size of the Label is limited by
| the window.)
|
| /Magnus
|
| If anyone would be intrested, this is how I tried it:
|
| void __fastcall TForm1::Button1Click(TObject *Sender)
| {
|    int Width,index ;
|    Label1->Caption="";
|    Width = Label1->Canvas->TextWidth(Edit1->Text);
|    if (Width > Label1->Width)
|    {
|      index = Label1->Width * Edit1->Text.Length() / Width;
|
|      while
| (Label1->Canvas->TextWidth(Edit1->Text.SubString(0,index))>Label1->Width)
|      {
|        index--;
|      }
|      Label1->Caption = Edit1->Text.Insert(AnsiString("\n"),index);
|    }
|    else
|      Label1->Caption = Edit1->Text;
| }
|
| Label1->AutoZose = false;
| Label1->WordWrap = true;
|
|
|

Other Threads