2013-10-24 75 views
0

我有RichTextBox,每次载入表单时都会加载一定的默认文本。防止某些文本在RichTextBox中被删除或更改

本文例如:

// Do not Exclude this line below 

我可以在新行添加文本,如有必要删除,但绝不能此行。

如果我删除单词“下面”的“e”,它必须回复到“下面”单词。相同的添加空间或字符。

我开始了这个代码在KeyDown事件:

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中的某些文本免受删除或更改”。

感谢和更多的力量!

回答

2

不要重新发明轮子。利用SelectionProtected属性

richTextBox.Rtf = ...; 
richTextBox.SelectAll(); or richTextBox.Select(start, length)//Select that line 
richTextBox.SelectionProtected = true; 
richTextBox.SelectionLength = 0; 
+0

什么是Rtf? – Elegiac

+0

Rtf只是设置文本,从不知道,如果你已经设置了'Text' –

+0

我应该为...放在richTextBox.Rtf = ...? – Elegiac