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

Custom Command layout problem

1 Answer 109 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Ada
Top achievements
Rank 1
Ada asked on 09 Jul 2011, 02:28 PM
I implemented a custom command like MSWord change case:
It perfectly change case but after executing the selection layout  did not upddate.(especially Upper Case All,characters overlays each other).
How to force update layout for selection?
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Telerik.Windows.Documents.Commands;
namespace Commands
{
 
    public class ICommandLetterCase : Telerik.Windows.Documents.RichTextBoxCommands.RichTextBoxCommandBase
    {
 
        public ICommandLetterCase(Telerik.Windows.Controls.RadRichTextBox rtb) : base(rtb)
        {
            this.AssociatedRichTextBox.DocumentChanged += () =>
            {
                if ((this.AssociatedRichTextBox.Document != null)) {
                    rtb.Document.Selection.SelectionChanged += () => { base.OnCanExecuteChanged(); };
                }
            };
        }
 
        protected override bool CanExecuteInReadOnlyMode {
            get { return false; }
        }
        protected override bool ShouldFocusCaretAfterExecute {
            get { return true; }
        }
        protected override bool CanExecuteOverride(object parameter)
        {
            if ((this.AssociatedRichTextBox != null) && this.AssociatedRichTextBox.Document != null) {
                if (!this.AssociatedRichTextBox.Document.Selection.IsEmpty) {
                    if (this.AssociatedRichTextBox.Document.Selection.GetSelectedBoxes.First is Telerik.Windows.Documents.Layout.SpanLayoutBox) {
                        string data = this.AssociatedRichTextBox.Document.Selection.GetSelectedText();
                        return !string.IsNullOrEmpty(data) && data.Length > 0;
                    }
                }
            }
            return false;
        }
        protected override void ExecuteOverride(object parameter)
        {
            try {
                foreach (Telerik.Windows.Documents.Layout.LayoutBox sbx in this.AssociatedRichTextBox.Document.Selection.GetSelectedBoxes) {
                    if (sbx is Telerik.Windows.Documents.Layout.SpanLayoutBox) {
                        if (!sbx is Telerik.Windows.Documents.Layout.FormattingSymbolLayoutBox) {
                            int p = Convert.ToInt32(parameter);
                            dynamic data = ((Telerik.Windows.Documents.Layout.SpanLayoutBox)sbx).Text;
                            string newdata = data;
                            switch (p) {
                                case 1:
                                    //all lowercase
                                    newdata = data.ToLower;
                                    break;
                                case 2:
                                    //all uppercase
                                    newdata = data.ToUpper;
                                    break;
                                case 3:
                                    //first uppercase
                                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                                    sb.Append(data(0).ToString.ToUpper);
                                    for (i = 1; i <= data.Length - 2; i++) {
                                        if (data(i + 1) == " ") {
                                            sb.Append(data(i).ToString.ToUpper);
                                        } else {
                                            sb.Append(data(i).ToString.ToLower);
                                        }
                                    }
 
                                    sb.Append(data(data.Length - 1));
                                    newdata = sb.ToString();
                                    break;
                            }
                            var _with1 = (Telerik.Windows.Documents.Layout.SpanLayoutBox)sbx;
                            _with1.Text = newdata;
                        }
                    }
                }
            } catch (Exception ex) {
                System.Diagnostics.Debug.Assert(false, "Letter Case ICommand error");
            }
        }
    }
 
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 13 Jul 2011, 12:06 PM
Hi Ada,

You have to force the layout update of both the document and the rich text box. Adding the following two invocations at the end of the ExecuteOverride method does the trick:

this.AssociatedRichTextBox.Document.UpdateLayout();
this.AssociatedRichTextBox.UpdateEditorLayout();
All the best,
Iva
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
RichTextBox
Asked by
Ada
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or