将文本附加到富文本框的开头

时间:2009-05-12 01:03:18

标签: c# richtextbox

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底部而不是底部。我该怎么做?

3 个答案:

答案 0 :(得分:21)

从技术上讲,如果你将它插入文本的顶部,你就是“插入”或“前置”,而不是“附加”。 ;)

您可以使用SelectedText属性在RichTextBox的开头插入文本。我刚刚敲了一个快速的演示应用来测试它:

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

单击button1时,会在RichTextBox的开头插入当前时间。

答案 1 :(得分:3)

我最喜欢的方式是,

    private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.Text = DateTime.Now.ToString() + richTextBox1.Text;
    }

它将在开始时添加当前日期时间。

答案 2 :(得分:0)

以前的消息似乎已过时。我可以通过以下方法解决:

{ value = 1, text = "Email Draft" },
{ value = 2, text = "Email Sent" },
{ value = 3, text = "Email Deleted" }
相关问题