0

I have a simple form in which I set the text of a TLabel like so:

__fastcall TFacChoiceForm::TFacChoiceForm(TComponent* Owner) : TBaseForm(Owner)
{

    CaptionLabel->Text = "Hello User";
    FacSearchBar->SetFocus();
}

About fifty percent of the time, the text shows up in the TLabel just fine. However, the other fifty percent of the time the TLabel displays random garbled text. I noticed that if I tab through the controls on the form, the text will eventually reset itself. I have checked that even when the text is shown garbled, if I look at the TLabels' text in the debugger it is exactly what I would expect it to be. I'm not sure where to look in order to fix this.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
James Hogle
  • 3,010
  • 3
  • 22
  • 46
  • 1
    If I remember correctly, you shouldn't be doing that in the constructor. Best to use the `OnCreate()` event handler. – Leonardo Herrera May 28 '15 at 14:57
  • Moved the code to the OnCreate() handler, but I still get the same garbled text half of the time. – James Hogle May 28 '15 at 15:00
  • Try creating an [SSCCE](http://sscce.org/) and show it here. – Leonardo Herrera May 28 '15 at 15:04
  • You should simply put `Text = 'Hello User'` under `object CaptionLabel` in the 'dfm' file. – barak manos May 28 '15 at 15:07
  • @barakmanos The text is actually dynamic and says "Hello, *Username*" however I just set the text to "Hello User" for my testing, and to make the code here simpler. – James Hogle May 28 '15 at 15:09
  • And... what happened? – barak manos May 28 '15 at 15:15
  • Maybe I misunderstood. I assumed putting the text in the dfm file would essentially hard code that text into the label whereas I need it dynamically set. – James Hogle May 28 '15 at 15:21
  • 2
    @LeonardoHerrera IIRC, using OnCreate on C++Builder is not recommended. The correct place is the constructor. What version of C++Builder are you using? Stuff like that sometimes means "unicode garbage", but that would depend if the compiler is indeed one of the ones that have Unicode. – Rodrigo Gómez May 28 '15 at 17:20
  • James, if your text is actually dynamic then this question is moot. Show us some code that reproduces this behavior. The code you have shown us cannot put garbage in the form by its own. – Leonardo Herrera May 28 '15 at 18:33
  • This code also reproduces the behavior. I removed the dynamic bit before posting on here to rule that out as a factor. Regardless of whether I set the text from a variable, or directly set it, I get the same behavior. – James Hogle May 28 '15 at 18:39
  • @JamesHogle: Again, which version of C++Builder are you actually using? Can you provide a screenshot of what the garbage actually looks like? Do you have the same problem if you set the text in the `OnShow` event instead of the constructor? – Remy Lebeau May 28 '15 at 21:44
  • @LeonardoHerrera there's a possible project setting causing OnCreate run before the constructor, leading to problems. Using the constructor is safe because , since your class is derived from TForm, the base class constructor runs first which loads all the controls. – M.M Jun 15 '15 at 07:33

1 Answers1

1

I'm not sure why this solved the issue, but increasing the height of the label prevented the garbled text from appearing.

James Hogle
  • 3,010
  • 3
  • 22
  • 46