RichTextBox C# Set caret location winforms

后端 未结 4 344
情书的邮戳
情书的邮戳 2021-01-22 15:38

I am building a chat application where the user enters its text to a richtextbox.

In the rich text box there is an initial text which says: \"Me: \".

Now, when

相关标签:
4条回答
  • 2021-01-22 15:58

    Use the Select method:

    public void Select(
        int start,
        int length
    )
    
    richTextBoxUserText.Select(richTextBoxUserText.TextLength, 0);
    
    0 讨论(0)
  • 2021-01-22 16:01

    Winforms: RichTextBox.SelectionStart & set RichTextBox.SelectionLength to 0.

    WPF: RichTextBox.CaretPosition

    0 讨论(0)
  • 2021-01-22 16:07

    Found it googling on the property SelectionProtected

    richTextBoxUserText.Text = INITIAL_TEXT;
    richTextBoxUserText.SelectAll();
    richTextBoxUserText.SelectionColor = Color.Red;
    richTextBoxUserText.SelectionProtected = true;
    richTextBoxUserText.SelectionLength = 0;
    richTextBoxUserText.SelectionStart = richTextBoxUserText.TextLength + 1;
    
    0 讨论(0)
  • 2021-01-22 16:08

    You can set the caret position with the SelectionStart and SelectionLength properties of the rich text box. Set SelectionLength to 0 and then set SelectionStart to the location where you want the caret to appear.

    The documentation for SelectionStart says:

    If no text is selected in the control, this property indicates the insertion point, or caret, for new text.


    The Win32 API function SetCaretPos is much too low level for your needs.

    0 讨论(0)
提交回复
热议问题