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

14 posts, 0 answers
  1. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 31 Jan 2011 Link to this post

    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
  2. Boby
    Admin
    Boby avatar
    595 posts

    Posted 01 Feb 2011 Link to this post

    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>>
  3. DevCraft banner
  4. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 14 Feb 2011 Link to this post

    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
  5. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 16 Feb 2011 Link to this post

    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>>
  6. Bob
    Bob avatar
    17 posts
    Member since:
    Jan 2011

    Posted 13 May 2011 Link to this post

    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?
  7. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 17 May 2011 Link to this post

    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
  8. Bob
    Bob avatar
    17 posts
    Member since:
    Jan 2011

    Posted 24 May 2011 Link to this post

    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;   } }


  9. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 27 May 2011 Link to this post

    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
  10. Bob
    Bob avatar
    17 posts
    Member since:
    Jan 2011

    Posted 30 May 2011 Link to this post

    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.
  11. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 02 Jun 2011 Link to this post

    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
  12. Bob
    Bob avatar
    17 posts
    Member since:
    Jan 2011

    Posted 02 Jun 2011 Link to this post

    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
  13. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 06 Jun 2011 Link to this post

    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
  14. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 31 Aug 2011 Link to this post

    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.
  15. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 31 Aug 2011 Link to this post

    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 >>

Back to Top
DevCraft banner