0

I'm using C++ Builder.

I have created a panel and set the width of said panel to 350.

I am using the following code to create labels.

TLabel* tempLabel = new TLabel(this);
tempLabel->Parent = Panel6;
tempLabel->Caption = temp->Text;
tempLabel->Top = 25*i;
tempLabel->Font->Style = TFontStyles() << fsBold;
tempLabel->Font->Color = clRed;

TLabel* tempLabel2 = new TLabel(this);
tempLabel2->Parent = Panel6;
tempLabel2->Caption = temp->Title;
tempLabel2->Top = tempLabel->Top + 10;
tempLabel2->AutoSize = true;
tempLabel2->WordWrap = true;

TLabel* tempLabel3 = new TLabel(this);
tempLabel3->Parent = Panel6;
tempLabel3->Caption = temp->Message;
tempLabel3->Top = tempLabel2->Top + 10;
tempLabel3->AutoSize = true;
tempLabel3->WordWrap = true;

The result is that the captions of tempLabel2 and tempLabel3 are extending passed the 350 width of the containing panel. I have read online about setting AutoSize to true and WordWrap to true. This attempt was not successful.

What must I set so that the label text will wordwrap respective to the parent panel's width?

HappyCoding
  • 641
  • 16
  • 36
  • 1
    You are not setting the `Width` or `Height` of the labels, so how do you expect them to word-wrap relative to the Panel? The labels don't know the Panel's size, because you are not setting them accordingly. You need to turn off `AutoSize` and then set `Width` to the max width you want, and then you can calculate the `Height`s using the Win32 `DrawText/Ex()` function using the `DT_CALCRECT` flag, or the `TCanvas.TextRect()` method using the `tfCalcRect` flag. – Remy Lebeau Jul 06 '18 at 22:44
  • @RemyLebeau I'm trying the following using the second option (because I found more documentation on it) and it's not working. I'm not understanding why either. Any advice? `TRect TheRect = Rect(10,(25*i)+10,100,100);` `tempLabel2->Canvas->TextRect(TheRect, temp->Title, tfCalcRect);` – HappyCoding Jul 16 '18 at 15:03

0 Answers0