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

Adding RadInsertSymbolDialog to ContextMenu

5 Answers 85 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Tracey
Top achievements
Rank 1
Tracey asked on 08 May 2012, 05:23 PM
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   

5 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 11 May 2012, 12:32 PM
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 >>

0
Tracey
Top achievements
Rank 1
answered on 15 May 2012, 08:52 AM
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
0
Tracey
Top achievements
Rank 1
answered on 15 May 2012, 09:19 AM
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
0
Martin Ivanov
Telerik team
answered on 17 May 2012, 10:42 AM
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 >>

0
Tracey
Top achievements
Rank 1
answered on 17 May 2012, 12:25 PM
That's great, thanks very much
Tags
RichTextBox
Asked by
Tracey
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Tracey
Top achievements
Rank 1
Share this question
or