防止在富文本框中删除或更改特定文本
本文关键字:文本 删除 | 更新日期: 2023-09-27 18:36:39
我有RichTextBox,它有一定的默认文本,每次加载表单时都会加载。
例如,此文本:
// Do not Exclude this line below
我可以在换行符中添加文本并在必要时删除,但不能删除这一行.
就像如果我删除单词"下面"中的"e"一样,它必须回到单词"下面". 同样添加空格或字符.
我在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
{
}
}
但我不知道我应该添加什么代码,或者我是否在第一名做错了事情.
现在我的问题是"如何保护富文本框中的某个文本不被删除或更改"。
感谢和更多的力量!
不要重新发明轮子。利用选择受保护的属性
richTextBox.Rtf = ...;
richTextBox.SelectAll(); or richTextBox.Select(start, length)//Select that line
richTextBox.SelectionProtected = true;
richTextBox.SelectionLength = 0;