检查富文本框文本区域是否已满

本文关键字:文本 是否 检查 区域 | 更新日期: 2023-09-27 18:35:39

我想在richtexbox中显示一大块文本,但限制长度以适应GUI上显示的文本区域。然后,用户点击下一步按钮,它会擦除文本区域,并继续在富文本框中显示更多文本。有点像文本区域的自定义滚动条,但是一种能够判断文本区域上是否剩余空间的方法,如果文本字体变大或变小,它将是动态的?当文本区域已满时是否有事件侦听器?

检查富文本框文本区域是否已满

我找到了答案。您可以使用:

Size textSize = TextRenderer.MeasureText(richTextBox1.Text, richTextBox1.Font);

对于文本的大小,请检查OnContentResized事件MeasureText()是否大于富文本框的高度。

If WinForms。溢出检测,以防有人复制一堆文本。

    private void richTextBox1_TextChanged(object sender, EventArgs e) {
        if(richTextBox1.Text.Length ==maxlength){
            //textboxfull
        }
        else if (richTextBox1.Text.Length > maxlength) {
            //textbox over flow
        }
    }

如果 WPF

标记:

<RichTextBox Name="rtbEditor" TextChanged="rtbEditor_TextChanged" />

法典:

private void rtbEditor_TextChanged(object sender, TextChangedEventArgs e) {
    string myText = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd).Text;
    if (myText.Length == maxlength) {
        //textboxfull
    }
    else if (myText.Length > maxlength) {
        //textbox over flow
    }
}