Append text to the beginning in the Rich Text Box

前端 未结 3 1048
孤街浪徒
孤街浪徒 2021-01-17 13:31
private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.AppendText(\"\\r\\n\");
    richTextBox1.Focus();
    string s = \"Enter \";
    richTextBo         


        
相关标签:
3条回答
  • 2021-01-17 13:56

    Simplest way i prefer is,

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

    It will prepend Current DateTime at Start.

    0 讨论(0)
  • 2021-01-17 13:57

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

    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();
        }
    

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

    0 讨论(0)
  • 2021-01-17 14:03

    previous messages seems obsolete. I solve it by :

    Paragraph newExternalParagraph = new Paragraph();
    newExternalParagraph.Inlines.Add( new Bold(new Run("[" + hour.ToString() + ":" + minute.ToString() + "]"))
    {
        Foreground = Brushes.Yellow
    });
    _consoleText.Document.Blocks.InsertBefore(_consoleText.Document.Blocks.FirstBlock , newExternalParagraph);
    
    0 讨论(0)
提交回复
热议问题