2

In a Windows Forms application I would like to edit plain text (formatting is currently not required) but keep some parts of the text non-editable. E.g. start with "ABC" to which the user may append or prepend additional text or delete "ABC" entirely but always keep "ABC" as a non-editable atom.

My first thought was to use a RichTextBox and add an RTF field ({\field...}) but it seems like RichTextBox does not support fields.

Another approach could be to add the non-editable text as a picture. However, I have not found a way to reliably prevent resizing the image. (Question asked here before: Remove the ability to resize an image pasted into a richtextbox and add an event to that image. C#)

Do you have any suggestions how to get this to work using the fields or pictures? Or any other approach to design the desired behaviour?

Community
  • 1
  • 1
Paul B.
  • 2,394
  • 27
  • 47

4 Answers4

4

Use the SelectionProtected property.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for the advice, I was not aware of this property. I encountered some challenges (cannot add text at the beginning of the text box if it starts with SelectionProtected, cannot delete entire selection) but I will look into this more thoroughly. – Paul B. Jan 09 '12 at 07:47
0

My solution for the problem: Insert the text as an image using IRichEditOle. This way the text is treated like a single character which is exactly what I wanted. http://www.codeproject.com/KB/edit/MyExtRichTextBox.aspx explains how to use IRichEditOle.

Paul B.
  • 2,394
  • 27
  • 47
0

Unfortunately I don't think you're going to find a great answer here with WinForms. What you're really asking for is much closer to a full blown editor vs. a small edit box which is what RichTextBox is there to provide (plus a bit of display).

If the display was very simple perhaps you can get away with it by putting a label directly next to the RichTextBox, removing it's border and changing it's background to match. I'm not 100% sure you could get the display seem less or not (is possible with WPF though).

Here though you're describing a bit more complex behavior with the adding and deleting. In order to get this to work I think you may need to create a new custom edit control. That's a pretty large step in WinForms though and probably not what you're looking to do.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

Not sure if you are looking at maybe using a 3rd party control but the DevExpress Rich Text Editor control for WinForms has what I think you might be looking for...

Document Protection

Our document protection model is based on the RTF 1.9.1 specification (read-only password protection and protection exceptions sections) and is therefore compatible with the Microsoft® Word® document protection feature.

To enable/disable document protection, use ProtectDocumentCommand / UnprotectDocumentCommand commands. You will be prompted for a password.

If document protection is enabled, then no content in the document shall be editable, except for the ranges with permissions. They can be modified by users with identities specified via RangePermission.UserName or RangePermission.Group properties. For a particular range being editable, AuthenticationOptions.UserName should be equal to RangePermission.UserName value OR AuthenticationOptions.Group should be equal to the RangePermission.Group value. A special case is a group "Everyone" - ranges marked with this group are editable regardless of the username or group specified via Authentication Options.

RangePermission is a special type of bookmark used to control which users may edit a particular region of a document.

Scott Wylie
  • 4,725
  • 2
  • 36
  • 48
  • I have seen that the WinForms Rich Editor also supports document fields which should do the trick. Using it seems like cracking the nut with a sledgehammer in my case yet I might come back to it if I can't find a nutcracker. :) – Paul B. Jan 09 '12 at 07:59