Kamil
Kamil

Reputation: 644

How to disable edit first line of RichTextBox?

I set first line of richtextbox by

RichTextBox.text = "Comment:";

I want this line read only, and input from second line, and get text begin from second line.

Any suggustion? Thanks!


Thanks, I add a MouseClick event to forbid first line editable:

 private void CommentTxtBox_MouseClick(object sender, MouseEventArgs e)
    {
        int index = _commentTxtBox.SelectionStart;
        int line = _commentTxtBox.GetLineFromCharIndex(index);
        if (line == 0)
        {
            _commentTxtBox.ReadOnly = true;
        }
        else
            _commentTxtBox.ReadOnly = false;

    }

Upvotes: 0

Views: 2164

Answers (2)

TaW
TaW

Reputation: 54433

You could try this:

private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
    int line = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
    richTextBox1.ReadOnly = line == 0;
}

Not tested very hard; it lets you only edit when the selection/cursor is not on the 1st line..

Upvotes: 0

Antoine V
Antoine V

Reputation: 7204

You can use SelectionProtected

Gets or sets a value indicating whether the current text selection is protected.

For example, you have "Comment:" in the control, the user is able to append the text after it but not remove your text.

So , I make ReadOnly first 7 characters, append a break line and the rest of your control becomes modifiable:

RichTextBox.Select(0, "Comment:".Length);
RichTextBox.SelectionProtected = true;
RichTextBox.AppendText(Environment.NewLine);

Upvotes: 3

Related Questions