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

How to detect if user selected (part of) table in RadRichTextBox

13 Answers 262 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Licenses
Top achievements
Rank 1
Licenses asked on 31 Jan 2011, 08:39 AM
Hello,

I would like to add a context menu to a RadRichTextBox with options to delete a table, insert/delete a table column and insert/delete a table row. But I want these context menu options to be visible for the user only when he has selected (part of) a table in the RadRichTextBox.

Is there a way to detect via the code if the user has selected a table, a table column or a table row?

Thanks,
Sodi

13 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 01 Feb 2011, 05:53 PM
Hi Sodi We,
The easiest way will be to use RadRichTextBox's default context menu, which includes this functionality by default. If you want to customize the default table editing commands, you can do it by providing it your custom content builder to the context menu:
public MainPage()
{
    InitializeComponent();
 
    ContextMenu contextMenu = (ContextMenu)this.radRichTextBox1.ContextMenu;
    contextMenu.ContentBuilder = new CustomMenuBuilder(this.radRichTextBox1);
}
public class CustomMenuBuilder : ContextMenuContentBuilder
{
    private RadRichTextBox radRichTextBox;
 
    public CustomMenuBuilder(RadRichTextBox radRichTextBox)
        : base(radRichTextBox)
    {
        this.radRichTextBox = radRichTextBox;
    }
 
    protected override ContextMenuGroup CreateClipboardCommands()
    {
        return base.CreateClipboardCommands();
        // or just
        //return null;
    }
 
    protected override ContextMenuGroup CreateSpellCheckingSuggestions()
    {
        return base.CreateSpellCheckingSuggestions();
        // or just
        //return null;
    }
 
    protected override ContextMenuGroup CreateTextEditCommands()
    {
        return base.CreateTextEditCommands();
        // or just
        //return null;
    }
 
    protected override ContextMenuGroup CreateTableCommands()
    {
        return new ContextMenuGroup()
        {
            new RadMenuItem()
            {
                Header = "Delete Table",
                Command = this.radRichTextBox.Commands.DeleteTableCommand
            },
            new RadMenuItem()
            {
                Header = "Insert Table Column",
                Command = this.radRichTextBox.Commands.InsertTableColumnToTheRightCommand
            },
            new RadMenuItem()
            {
                Header = "Delete Table Column",
                Command = this.radRichTextBox.Commands.DeleteTableColumnCommand
            },
            new RadMenuItem()
            {
                Header = "Insert Table Row",
                Command = this.radRichTextBox.Commands.InsertTableRowBelowCommand
            },
            new RadMenuItem()
            {
                Header = "Delete Table Row",
                Command = this.radRichTextBox.Commands.DeleteTableRowCommand
            }
        };
    }
}
You can also check out our blog post about customizing the context menu. In order to use context menu, you have to add references to Telerik.Windows.Controls.RichTextBoxUI.dll, Telerik.Windows.Controls.Navigation.dll and Telerik.Windows.Controls.dll.

Regards,
Boby
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Licenses
Top achievements
Rank 1
answered on 14 Feb 2011, 09:52 AM
Hi Boby,

Unfortunately, the default context menu of the RadRichTextBox does not work in my application for some reason.

I added references to the dll's you mentioned. I also added a reference to the Telerik.Windows.Documents.dll. In the XAML, I mentioned the following namespace declaration:

Then I created the RadRichTextBox in the XAML, like so:
<telerik:RadBusyIndicator IsBusy="{Binding IsLoadingDetail}" Style="{StaticResource BusyIndicatorStyle}">
        <Grid x:Name="LayoutRoot">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            ...
            <telerik:RadRichTextBox x:Name="radRichTextbox" Grid.Row="1" Background="Beige"></telerik:RadRichTextBox>
            ...
      </Grid>
</telerik:RadBusyIndicator>

But when I run my application and rightclick on the RadRichTextBox, the context menu does not appear. Any ideas?

Thanks
Sodi
0
Iva Toteva
Telerik team
answered on 16 Feb 2011, 05:11 PM
Hi Sodi We,

It is quite strange that the context menu does not appear in your application. Are you using library caching? If that is the case, you can find an explanation and a solution in this forum thread
To the list of required assemblies that Boby mentioned previously, I would add Telerik.Windows.Controls.RibbonBar, as it is also needed by the RichTextBoxUI assembly.
Let us know if you manage to resolve the issue. Otherwise, we would greatly appreciate any further details about your application. A sample project would be best, as it would help us track down the problem more easily.

Best wishes,
Iva
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Bob
Top achievements
Rank 1
answered on 13 May 2011, 10:22 PM
One question for the admin. I noticed in your code example you have this:

ContextMenu contextMenu = (ContextMenu)this.radRichTextBox1.ContextMenu;

Problem is that ContextMenu is System.Windows.Controls.ContextMenu, which does not have a property of ContextBuilder. Should I be referencing the ContextMenu differently? For example, when you mouse over ContextMenu, is it a System.Windows.Controls.ContextMenu, or something else?
0
Iva Toteva
Telerik team
answered on 17 May 2011, 03:05 PM
Hi Bob,

The default ContextMenu of RadRichTextBox is of type:

Telerik.Windows.Documents.UI.Extensibility.IContextMenu
 
and you need to cast it to:
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu

The actual type of the context menu is the second one, but as we support custom context menus, the type of the ContextMenu property is that of the interface.
I hope that answers your question.

Greetings,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Bob
Top achievements
Rank 1
answered on 24 May 2011, 04:33 PM
I copied your code, and the content menu item works, but the item is disabled and unclickable for some reason(screenshot attached). Noting I also tried assing IsEnabled on the new RadMenuItem and it didn't work. If you can think of anything =)

Code in initializer:
Telerik.Windows.Controls.RichTextBoxUI.ContextMenu contextMenu = (Telerik.Windows.Controls.RichTextBoxUI.ContextMenu)Editor.ContextMenu;  // Editor is my RadRichTextBox
contextMenu.ContentBuilder = new CopyOnlyContextMenu(Editor);

Code to make the context menu:
public class CopyOnlyContextMenu : ContextMenuContentBuilder



{   private RadRichTextBox radRichTextBox;   public CopyOnlyContextMenu(RadRichTextBox radRichTextBox)     : base(radRichTextBox)   {     this.radRichTextBox = radRichTextBox;   }   protected override ContextMenuGroup CreateClipboardCommands()   {     return new ContextMenuGroup()     {       new RadMenuItem()       {         Header = "Copy",         Command = this.radRichTextBox.Commands.CopyCommand       }     };   }   protected override ContextMenuGroup CreateSpellCheckingSuggestions()   {     return null;   }   protected override ContextMenuGroup CreateTableCommands()   {     return null;   }   protected override ContextMenuGroup CreateTextEditCommands()   {     return null;   } }


0
Iva Toteva
Telerik team
answered on 27 May 2011, 01:43 PM
Hi Bob,

Thank you for providing the code of your implementation.The Copy command requires that the selection is not empty and this is the only case when the RadMenuItem should appear disabled. I created a sample application using the class and everything worked as expected. You can find it attached to this post.
I have also kept another custom context menu builder in the project, which shows how you can add images to the RadMenuItems (it is commented though).

I hope this helps.

Kind regards,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Bob
Top achievements
Rank 1
answered on 30 May 2011, 04:02 PM
Yeah, that's the exact code I have and still when text is selected, the right-click Copy is still "disabled". Any other suggestions why that might be? You can see in the capture.png attached.
0
Iva Toteva
Telerik team
answered on 02 Jun 2011, 04:32 PM
Hello Bob,

The only explanation that comes to mind is that you have several editors and are assigning the content builder without instantiating a new ContextMenu for each one. Normally, only one instance of ContextMenu is created for all RadRichTextBoxes in the application and that may cause the behavior you are observing.
If you do have several editors, you have to proceed as in the code-snippet below:

rtb1.ContextMenu = new Telerik.Windows.Controls.RichTextBoxUI.ContextMenu() { ContentBuilder = new CustomMenuBuilder(rtb1) };
rtb2.ContextMenu = new Telerik.Windows.Controls.RichTextBoxUI.ContextMenu() { ContentBuilder = new CustomMenuBuilder(rtb2) };
rtb3.ContextMenu = new Telerik.Windows.Controls.RichTextBoxUI.ContextMenu() { ContentBuilder = new CustomMenuBuilder(rtb3) };

If that is not the case, we would greatly appreciate a sample demo illustrating the problem.


Greetings,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Bob
Top achievements
Rank 1
answered on 02 Jun 2011, 10:30 PM
Thanks that did the trick. Although I'm perplexed as to why, as I had two RadRichTextBoxes in two seperate user controls. I don't see why changing the context menu in one made it show up in the other, as you pass in the RichTextBox into the constructor of the ContextMenuGroup.

Cheers
0
Iva Toteva
Telerik team
answered on 06 Jun 2011, 02:47 PM
Hi Bob,

Thank you for the feedback. We will make sure to include this item in our online documentation, so that no such misunderstandings occur.

All the best,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Daní
Top achievements
Rank 1
answered on 31 Aug 2011, 05:02 PM
I've created a custom ContextMenuContentBuilder and overriden all methods but CreateClipboardCommands to force that only Clipboard commands are shown in RichTextBox's ContextMenu. 

Now I'd like to localize this commands. Some months ago I found a resource file with many of the text resources found in telerik controls, I translated it currently I set Telerik the LocalizationManager to use the translated resources file. But I suspect that resources used in RichTextBox are not present in this resources file. Do RichTextBox controls use LocalizationManager to resolve their strings? If they do, where can I get the RichTextBox resource to translate them? Thanks.
0
Iva Toteva
Telerik team
answered on 31 Aug 2011, 05:08 PM
Hello Daní,

The strings that RadRichTextBox uses are included in the Strings.resx file, distributed with the source of the controls. Most of them start with "Documents_", so you can check if they are not included in the file you've got.
In any case, there is a resource file with all strings that RadRichTextBox and its default UI use here. For more detailed information on the localization of the control you can refer to this article.

Greetings,
Iva
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
RichTextBox
Asked by
Licenses
Top achievements
Rank 1
Answers by
Boby
Telerik team
Licenses
Top achievements
Rank 1
Iva Toteva
Telerik team
Bob
Top achievements
Rank 1
Daní
Top achievements
Rank 1
Share this question
or