11
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の下部ではなく上部に配置したいと考えています。どうすればいいですか?

4

3 に答える 3

24

技術的には、テキストの先頭に挿入する場合は、「追加」ではなく「挿入」または「先頭に追加」しています。;)

SelectedText プロパティを使用して、RichTextBox の先頭にテキストを挿入できます。テストするために、簡単なデモアプリを作成しました。

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

これにより、button1 がクリックされると、RichTextBox の先頭に現在の時刻が挿入されます。

于 2009-05-12T01:18:05.287 に答える
3

私が好む最も簡単な方法は、

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

Start の前に Current DateTime を追加します。

于 2013-09-20T08:16:06.447 に答える