防止在 RichTextBox 中删除或更改某些文本 [英] Preventing a Certain Text to be Deleted or Change in a RichTextBox
问题描述
我有 RichTextBox,它有一个特定的默认文本在每次表单加载时加载.
i have RichTextBox which has a certain default text to load everytime the Form Load .
此文字例如:
// Do not Exclude this line below
我可以在换行符中添加文本并在必要时删除,但不能在这一行.
i can add text in newline and delete if necessary but must not this line .
就像我在单词below"中删除e"一样,它必须回到单词below".同样添加空格或字符.
like if i delete "e" in word "below" it must comeback to word "below" . same adding space or char .
我在 Keydown 事件中开始了这段代码:
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 .
现在我的问题是如何保护 RichTextBox 中的某个文本不被删除或更改".
Now my question was "How can I protect a certain text in a RichTextBox from deleting or changing" .
感谢和更多力量!
推荐答案
不要重新发明轮子.使用 SelectionProtected 属性
richTextBox.Rtf = ...;
richTextBox.SelectAll(); or richTextBox.Select(start, length)//Select that line
richTextBox.SelectionProtected = true;
richTextBox.SelectionLength = 0;
这篇关于防止在 RichTextBox 中删除或更改某些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!