Preventing a Certain Text to be Deleted or Change in a RichTextBox

两盒软妹~` 提交于 2019-12-13 04:46:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!