在RTF文本框中将文本追加到开头 [英] Append text to the beginning in the Rich Text Box

查看:54
本文介绍了在RTF文本框中将文本追加到开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.AppendText("\r\n");
    richTextBox1.Focus();
    string s = "Enter ";
    richTextBox1.AppendText(s + "\r\n");
    richTextBox1.SelectionStart = richTextBox1.Text.Length - (s.Length +1);
    richTextBox1.SelectionLength = s.Length +1;
    richTextBox1.SelectionFont = new Font("Arial", 12, FontStyle.Bold);
    richTextBox1.DeselectAll();
    richTextBox1.SelectionStart = richTextBox1.Text.Length;
    richTextBox1.SelectionLength = richTextBox1.Text.Length;
    richTextBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular);
    richTextBox1.DeselectAll();
}

每次用户单击按钮时,我希望新的"Enter"位于RichTextBox的顶部而不是底部.我该怎么做?

Every time a user clicks on the button I want that new "Enter" to be on the top not on the bottom of the RichTextBox. How can I do it?

推荐答案

从技术上讲,如果将其插入文本的顶部,则是插入"或前置",而不是附加".;)

Technically if you're inserting it at the top of the text, you're "inserting" or "prepending", not "appending". ;)

您可以使用SelectedText属性在RichTextBox的开始处插入文本.我刚刚打开了一个快速演示应用程序对其进行了测试:

You can use the SelectedText property to insert text at the start of a RichTextBox. I just knocked up a quick demo app to test it out:

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.SelectionStart = 0;
        richTextBox1.SelectionLength = 0;
        richTextBox1.SelectedText = DateTime.Now.ToString();
    }

当单击button1时,它将在RichTextBox的开始处插入当前时间.

That inserts the current time at the start of the RichTextBox when button1 is clicked.

这篇关于在RTF文本框中将文本追加到开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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