Firemonkey:设置TLabel文本会导致字符串索引超出范围异常 [英] Firemonkey: Setting TLabel Text causes String Index out of Range Exception

查看:527
本文介绍了Firemonkey:设置TLabel文本会导致字符串索引超出范围异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法建立一个帮助消息,然后设置 TLabel 的文本属性到该帮助消息。但是,每当我尝试设置标签的文本时,我得到以下异常:

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:

'字符串索引超出范围。 (-1)必须是> = 0和< = 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 调用只是为了检查我的字符串的值。它显示了很好。我也可以将标签设置为任何任意值,例如:

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!

我做错了,因为我建立 HelpMsg String?

Am I doing something wrong as I build the HelpMsg String?

编辑:注释将\\\
添加到String的行会修复问题。类似地,以下代码将描述异常:

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?

推荐答案

目前正在更新到新的C ++ Builder,我有方法的处理错误消息输出到日志选项卡而不是ShowMessage,而不是使用字符串,虽然我使用TStringList。例如:

Currently updating to new new C++ Builder so cant play around with your snippet. I have method's for handling error messages which are output to a log tab as opposed to the ShowMessage, rather than using a string though I use a TStringList. For example:

void __fastcall TPasswordChangeForm::BuildHelpMessage()
{
    TStringList HelpMsg = new TStringList(this);

    if( NewPassEdit->Text.Length() < MinPasswordLength )
    {
        HelpMsg->Add("Password length too short.");
    }
    else
    {
        HelpMsg->Add("Password length OK.");
    }

    if( NewPassEdit->Text == ConfirmPassEdit->Text )
    {
        HelpMsg->Add("Passwords match.");
    }
    else
    {
        HelpMsg->Add("Passwords do not match."_;
    }

    ShowMessage(HelpMsg->Text); //added for debugging, shows string as expected

    HelpLabel->Text = HelpMsg->Text;  //exception thrown here
}

通过使用TStringList或TStrings(parent),当您访问Text属性时,输出对象内的字符串,回车和换行符。

By using TStringList or TStrings (parent) when you access the Text property the strings within the object are output, each separated by a carriage return and line feed.

请参阅 TStringList文档 - 希望这有一些帮助!

See the TStringList Docs here - hope this is of some help!

这篇关于Firemonkey:设置TLabel文本会导致字符串索引超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆