问题
i have RichTextBox which has a certain default text to load everytime the Form Load .
this text for example:
// Do not Exclude this line below
i can add text in newline and delete if necessary but must not this line .
like if i delete "e" in word "below" it must comeback to word "below" . same adding space or char .
i started out this code in Keydown Event:
Regex donotexclude = new Regex(@"//\s+\s+\s+\s+\s+\s+Do\s+not\s+exclude\s+line\s+below");
MatchCollection donotexcluDE = donotexclude.Matches(rtb_JS.Text);
// if (Keys.Space &&
foreach (Match DONOTEXCLUDE in donotexcluDE)
{
if (DONOTEXCLUDE.Success)
{
var donot = DONOTEXCLUDE.Groups[0].Value.ToString();
//if (!donot.Length =>
//{
//}
}
else
{
}
}
but I dunno what code should I add or if Im doing a wrong thing in the 1st place .
Now my question was "How can I protect a certain text in a RichTextBox from deleting or changing" .
Thanks and More Power!
回答1:
Don't reinvent the wheel. Make use of SelectionProtected property
richTextBox.Rtf = ...;
richTextBox.SelectAll(); or richTextBox.Select(start, length)//Select that line
richTextBox.SelectionProtected = true;
richTextBox.SelectionLength = 0;
来源:https://stackoverflow.com/questions/19565380/preventing-a-certain-text-to-be-deleted-or-change-in-a-richtextbox