Adding RadInsertSymbolDialog to ContextMenu

6 posts, 0 answers
  1. Tracey
    Tracey avatar
    34 posts
    Member since:
    Feb 2011

    Posted 08 May 2012 Link to this post

    Hi

    I want to add the ability the insert a characted via the RadRichTextBox context menu.

    I have created a custom ContextMenuContentBuilder like so:
    public class CustomRichTextBoxContextMenu : ContextMenuContentBuilder
        {
            private RadRichTextBox radRichTextBox;
            public RadInsertSymbolDialog insertsymboldialog = new RadInsertSymbolDialog();
            public CustomRichTextBoxContextMenu(RadRichTextBox radRichTextBox)
                : base(radRichTextBox)
            {
                this.radRichTextBox = radRichTextBox;
                this.radRichTextBox.InsertSymbolWindow = insertsymboldialog;
            }
            protected override ContextMenuGroup CreateTextEditCommands()
            {
                RadMenuItem insertCharacter = new RadMenuItem() { Header = "Insert Character" };
                insertCharacter.Click += new Telerik.Windows.RadRoutedEventHandler(insertCharacter_Click);
                ContextMenuGroup customContextMenuGroup = new ContextMenuGroup();
                customContextMenuGroup.Add(insertCharacter);
                return customContextMenuGroup;
            }
      
            private void insertCharacter_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                insertsymboldialog.ShowDialog();
            }
        }

    and attached it to the RadRichTextBox (this.Title) like so: 
    Telerik.Windows.Controls.RichTextBoxUI.ContextMenu contextMenu = (Telerik.Windows.Controls.RichTextBoxUI.ContextMenu)Title.ContextMenu;
    contextMenu.ContentBuilder = new CustomRichTextBoxContextMenu(this.Title);

    This opens up the RadInsertSymbolDialog but when I click a symbol on the RadInsertSymbolDialog I get an error.
    "Message: System.NullReferenceException: Object reference not set to an instance of an object"

    I have just upgraded to the late version of the Telerik controls.

    Can you let me know what I am doing wrong.
    Many Thanks   
  2. Martin Ivanov
    Admin
    Martin Ivanov avatar
    104 posts

    Posted 11 May 2012 Link to this post

    Hello Tracey,

    In case you want to use the insert symbol dialog method, you have to use its Show() method and pass it a callback method parameter to specify an action for symbol insertion. Here is a sample replacement of insertCharacter_Click method, using the default implementation.

    private void insertCharacter_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                Span currentSpan = this.radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan;
     
                var insertTextCallback = new Action<char, FontFamily>(
                        (char symbol, FontFamily font) =>
                        {
                            Span span = new Span();
                            if (currentSpan.Style != null)
                            {
                                span.CopyPropertiesFromStyle(currentSpan.Style);
                                span.Style = null;
                            }
     
                            span.FontFamily = font;
                            span.Text = symbol.ToString();
     
                            this.radRichTextBox.InsertInline(span);
                        });
     
                insertsymboldialog.Show(insertTextCallback, currentSpan.FontFamily, this.radRichTextBox);
            }

    Otherwise, you can simply call RadRichTextBox.ShowInsertSymbolDialog method:

    this.radRichTextBox.ShowInsertSymbolWindow();

    Write us back if you encounter any other issues.

    All the best,
    Martin
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Tracey
    Tracey avatar
    34 posts
    Member since:
    Feb 2011

    Posted 15 May 2012 Link to this post

    That's brill thanks works a treat. Just have one more question.

    How do I get rid of the Hyperlink item in the Context Menu?

    Many Thanks
    Tracey
  5. Tracey
    Tracey avatar
    34 posts
    Member since:
    Feb 2011

    Posted 15 May 2012 Link to this post

    And also,

    I am trying to show an icon next to my Insert Character menu item but it is not showing

    I am doing the following:
    BitmapImage img = new BitmapImage(new Uri("/Images/charicon.png", UriKind.Relative));
    RadMenuItem insertCharacter = new RadMenuItem() { Header = "Insert Character", Icon = img };

    Could you let me know what I am doing wrong

    Thanks again
  6. Martin Ivanov
    Admin
    Martin Ivanov avatar
    104 posts

    Posted 17 May 2012 Link to this post

    Hello Tracey,

    You can use the following code to create the image:
    Image img = new Image() { Source = new BitmapImage(new Uri("/CustomContextMenuApplication;component/Images/charicon.png", UriKind.Relative)) };

    where "CustomContextMenuApplication" is the name of your project. Don't forget to set the image build action to resource, when using your own images.

    In order to remove the hyperlink item, override the method that adds it in the CustomRichTextBoxContextMenu and have it return null:

    protected override ContextMenuGroup CreateHyperlinkCommands(bool forExistingHyperlink)
    {
        return null;
    }

    Don't hesitate to contact us if you have other questions.

    All the best,
    Martin
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  7. Tracey
    Tracey avatar
    34 posts
    Member since:
    Feb 2011

    Posted 17 May 2012 Link to this post

    That's great, thanks very much
Back to Top
DevCraft banner