我有一个方法可以构建帮助消息,然后将TLabel
's text 属性设置为该帮助消息。但是,每当我尝试设置标签的文本时,都会出现以下异常:
'String index out of range. (-1) Must be >=0 and <=42'
方法如下:
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
}
我添加了一个ShowMessage
调用只是为了检查我的字符串的值。它显示得很好。我还可以将标签设置为任意值,例如:
HelpLabel->Text = "This message works!";
HelpMsg
我在构建字符串 时做错了什么吗?
编辑:注释掉将 \n 添加到字符串的行可以解决问题。同样,以下代码将引发异常:
String test = "this is a test";
test += "\n";
test += "test 2";
HelpLabel->Text = test;
引起问题的 \n 是什么?如何正确添加新行?