Lock part of text in RichTextBox

  • Thread starter Thread starter BVM
  • Start date Start date
B

BVM

Hi, friends:

Do you know how to lock some of text in RichTextBox? Because I don't want user to change what other people have already wriiten in the RichTextBox.

Thanks,

Dennis Huang
 
Hi Dennis,

Use the SelectionProtected property to mark text protected.
This sample will select all text in the richTextBox and mark it as protected.
The user can add more text, but cannot change the protected text.

richTextBox1.Text = "Hello World, this is a protected sentence.\n";
richTextBox1.SelectAll();
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionProtected = true;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionStart = richTextBox1.TextLength+1;

It also changes the protected text to red.
Note that you cannot change the text programmatically either until you unprotect the text.
 
Back
Top