This is a migrated thread and some comments may be shown as answers.

Restrict Editing of Field Contents

3 Answers 82 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 04 Apr 2015, 11:20 PM

Hi,

How can I restrict the user from editing a field content after he inserted it? I need to allow the user to either insert or delete fields, but not change their inner content.

Thanks in advance.

3 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 07 Apr 2015, 05:10 PM
Hello William,

By default the Fields are special document elements, which have the ability to update themselves automatically and editing such fields is not a very common scenario. Could you, please, provide us more information about the scenario? Why you need to restrict the user to edit and what exactly kind of Fields you are using?

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
William
Top achievements
Rank 1
answered on 07 Apr 2015, 05:16 PM

What I mean is, once the user inserts the field, the editor by default allows him to change the field code. What I want is to not allow the user to do that, since he may invalidate the field by typing an invalid code.

Example Scenario to avoid:

1. The user inserts a MergeField

2. The document shows the Mergefield in Code view as {MERGEFIELD MyProperty}

3. The user puts the caret inside the field and changes the content from MERGEFIELD to MERGEFIELD1 by accident.

4. The field is now invalid since there is no code as MERGEFIELD1, and the editor displays the following message in Results View: Not supported field expression!.

 

0
Tanya
Telerik team
answered on 10 Apr 2015, 11:21 AM
Hi William,

The ReadOnlyRanges are a convenient way to prevent modifications of some parts of the document. You could use them to restrict the changes for the fields. More information about this feature you can find in the Read Only Ranges article in our documentation.

The protection, however, does not allow deleting the protected content. To fit your requirements, you could subscribe to the PreviewEditorKeyDown event of RadRichTextBox and check the pressed key. If the key is Delete/Backspace, check the positions of the Selection - the start position of the Selection should be smaller than the read only range's start position; the end position of the Selection should be bigger than the one of the range end. Please, refer to the snippet below for more details about the implementation:
void editor_PreviewEditorKeyDown(object sender, PreviewEditorKeyEventArgs e)
{
    if (e.Key == Key.Delete || e.Key == Key.Back)
    {
        var ranges = this.editor.Document.Selection.GetAnnotationMarkersOfType<ReadOnlyRangeStart>();
        foreach (var rangeStart in ranges.ToList())
        {
            DocumentPosition start = new DocumentPosition(this.editor.Document);
            start.MoveToInline(rangeStart);
 
            if (this.editor.Document.Selection.Ranges.First.StartPosition < start)
            {
                DocumentPosition end = new DocumentPosition(this.editor.Document);
                start.MoveToInline(rangeStart.End);
 
                if (this.editor.Document.Selection.Ranges.First.EndPosition > end)
                {
                    RadDocumentEditor toEdit = new RadDocumentEditor(this.editor.Document);
 
                    toEdit.RespectReadOnlyRanges = false;
                    toEdit.RespectNonDeletableRanges = false;
 
                    toEdit.Delete(true);
                    e.SuppressDefaultAction = true;
                }
            }
        }
    }
}

You should be aware, that using this approach, you will not be able to delete the protected range if the Selection's start/end position matches the range's start end/position. This behavior comes from the SkipPositionBefore of the ReadOnlyRange and we do not have much to do in this case.

Hope this helps.

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
RichTextBox
Asked by
William
Top achievements
Rank 1
Answers by
Tanya
Telerik team
William
Top achievements
Rank 1
Share this question
or