protect RTF
-
I just discovered the "\protect" and "\protect0" RTF code word (whatever RTF people call them, code words or tags or whatever). I wanted to implement it into my little program (it has a RichTextBox control). I can get it to protect the rich text just fine (cannot be edited), but it's not unprotecting protected text at all. Can someone please take a look at my code snippet and tell me what I'm doing wrong? Note: The following code is inside my KeyDown event handler, inside an if-block that checks for the CTRL key being down, so CTRL+P makes this run.
if (e.KeyCode == Keys.P)//protection, not printing { string line = rtf.SelectedRtf; string text = rtf.SelectedText; Console.WriteLine("-----LINE-----\r\n{0}\r\n", line); if (line.IndexOf("\\protect") > -1)//has protection therein { Console.WriteLine("unprotected:"); line = line.Replace("\\protect0", ""); line = line.Replace("\\protect", ""); Console.WriteLine(line); rtf.SelectedRtf = line; } else { Console.WriteLine("protected"); int start = line.IndexOf(text, line.LastIndexOf("\\")); int end = start + text.Length; line = line.Insert(end, @"\protect0"); line = line.Insert(start - 1, @"\protect"); rtf.SelectedRtf = line; } }
-Daniel Typing too fast fro my owngood
-
I just discovered the "\protect" and "\protect0" RTF code word (whatever RTF people call them, code words or tags or whatever). I wanted to implement it into my little program (it has a RichTextBox control). I can get it to protect the rich text just fine (cannot be edited), but it's not unprotecting protected text at all. Can someone please take a look at my code snippet and tell me what I'm doing wrong? Note: The following code is inside my KeyDown event handler, inside an if-block that checks for the CTRL key being down, so CTRL+P makes this run.
if (e.KeyCode == Keys.P)//protection, not printing { string line = rtf.SelectedRtf; string text = rtf.SelectedText; Console.WriteLine("-----LINE-----\r\n{0}\r\n", line); if (line.IndexOf("\\protect") > -1)//has protection therein { Console.WriteLine("unprotected:"); line = line.Replace("\\protect0", ""); line = line.Replace("\\protect", ""); Console.WriteLine(line); rtf.SelectedRtf = line; } else { Console.WriteLine("protected"); int start = line.IndexOf(text, line.LastIndexOf("\\")); int end = start + text.Length; line = line.Insert(end, @"\protect0"); line = line.Insert(start - 1, @"\protect"); rtf.SelectedRtf = line; } }
-Daniel Typing too fast fro my owngood
This is just to help new people. In this code following line of code should be changed string line = rtf.SelectedRtf; string text = rtf.SelectedText; TO string line = rtf.Rtf; string text = rtf.Text; It works for me after making this change Thanks