Disable the Context Menu for a RadTextBox

7 posts, 0 answers
  1. Renato Lopes
    Renato Lopes avatar
    13 posts
    Member since:
    Sep 2007

    Posted 18 Jul 2008 Link to this post

    How can I disable the default Context Menu for a RadTextBox?

    I want to use my custom Context Menu, but it always shows the "copy, paste cute..." menu...

  2. Angel
    Angel avatar
    121 posts

    Posted 21 Jul 2008 Link to this post

    Hi Renato Lopes,

    The context menu is a feature of the hosted MS TextBox and it can be stopped from showing only with a workaround.

    For example, this can be done by ignoring the WM_RBUTTONDOWN message:

       1. Inherit the base text box control that is used for hosting and ignore the right button down:

    public class TextBoxNoMenu : HostedTextBoxBase  
        private const int WM_RBUTTONDOWN = 0x204;  
        protected override void WndProc(ref Message m)  
            if (m.Msg == WM_RBUTTONDOWN)  
            base.WndProc(ref m);  

       2. Use the above control as a hosted one:

    private void DisableContextMenu(RadTextBox textBox)  
        RadTextBoxItem textBoxItem = new RadTextBoxItem(new TextBoxNoMenu());  
        textBoxItem.Text = textBox.Text;  

    You can call anywhere DisableContextMenu(): in the constructor of the form, in button click event, etc.
    You can even save the old host item and reuse it if you wish to re-enable the context menu.

    Note that the code that is given here will work only with the upcoming Q2 2008 release (due this week).
    In the previous releases HostedTextBoxBase class was not public.

    All the best,
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Angel
    Angel avatar
    121 posts

    Posted 30 Oct 2008 Link to this post

    Hello Renato Lopes,

    This is just a follow up to the previous post.

    There is a synchronization line for the Text property in DisableContextMenu(). In order to preserve the connection between the used two Text properties, binding should be used.

    The corrected code is given below:

    private void DisableContextMenu(RadTextBox textBox)  
        RadTextBoxItem textBoxItem = new RadTextBoxItem(new TextBoxNoMenu());  
        textBoxItem.BindProperty(RadItem.TextProperty, textBox.TextBoxElement,  
            RadItem.TextProperty, PropertyBindingOptions.TwoWay);  

    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. Ofer
    Ofer avatar
    14 posts
    Member since:
    Apr 2009

    Posted 25 Jun 2009 Link to this post


    What about your WinForms 2009 SP1 with VS 2008 SP1?

    Your solution disables the context-menu but creates a problem - 

    Now the textbox doesn't show line-breaks!

    I use it to show the content of email messages.

    So how to diable the context menu?

    I tried txtbXX.ContextMenu = null or txtbXX.ContextMenu = new ContextMEnu() but I still get the default context menu!
  5. Martin Vasilev
    Martin Vasilev avatar
    1061 posts

    Posted 01 Jul 2009 Link to this post

    Hi Ofer,

    Thank you for writing. Actually you cannot remove the ContextMenu by using the ContextMenu property, because it comes from hosted MS TextBox element inside in the RadTextBox control. You can stop it only by using the work-around provided in this topic. If you want to show break-lines, you can set AcceptsReturn property to true. Stopping context menu, does not influence this feature. Do not hesitate to contact me again if you have other questions.

    Kind regards,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Howard Hill
    Howard Hill avatar
    4 posts
    Member since:
    Apr 2010

    Posted 11 Jan 2011 Link to this post

    Good morning,

    I was using this method to disable the context menu and it works great. However I noticed that none of the properties set on the original text box transfer over. (Or at least MaxLength does not.)

    Also I noticed that the TextChanging event does fire BUT setting e.Cancel to true is ignored letting in characters I want to skip.

    Is there an easy way to get all the properties and events wired up correctly? Some forms have several RadTextBoxes that I need to override.


  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 11 Jan 2011 Link to this post

    Hello Howard,

    This may be an easier way to get rid of the context menu on a RadTextBox (just give it a dummy context menu)
    Me.RadTextBox1.TextBoxElement.TextBoxItem.HostedControl.ContextMenu = New ContextMenu()

    Hope that hels
Back to Top