I have a method which builds a help message and then sets a TLabel’s text property to that help message. However, whenever I attempt to set the text of the label, I get the following exception:
‘String index out of range. (-1) Must be >=0 and <=42’
The method is as follows:
void __fastcall TPasswordChangeForm::BuildHelpMessage()
{
String HelpMsg = “”;
if( NewPassEdit->Text.Length() < MinPasswordLength )
{
HelpMsg += “Password length too short.”;
}
else
{
HelpMsg += “Password length OK.”;
}
HelpMsg += “n”;
if( NewPassEdit->Text == ConfirmPassEdit->Text )
{
HelpMsg += “Passwords match.”;
}
else
{
HelpMsg += “Passwords do not match.”;
}
ShowMessage( HelpMsg ); //added for debugging, shows string as expected
HelpLabel->Text = HelpMsg; //exception thrown here
}
I added a ShowMessage call just to check the value of my string. It shows up just fine. I am also able to set the label to be any arbitrary value such as:
HelpLabel->Text = “This message works!”;
Am I doing something wrong as I build the HelpMsg String?
Edit: Commenting out the line which adds the n to the String fixes the problem. Similarly, the following code will cuase the exception:
String test = “this is a test”;
test += “n”;
test += “test 2”;
HelpLabel->Text = test;
What is it about the n that causes issues? How do I correctly add a new line?