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

Disable the Context Menu for a RadTextBox

6 Answers 491 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
Renato Lopes
Top achievements
Rank 1
Renato Lopes asked on 18 Jul 2008, 11:55 AM
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...

6 Answers, 1 is accepted

Sort by
0
Angel
Telerik team
answered on 21 Jul 2008, 10:03 AM
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)  
            return;  
 
        base.WndProc(ref m);  
    }  

   2. Use the above control as a hosted one:

private void DisableContextMenu(RadTextBox textBox)  
{  
    textBox.TextBoxElement.Children.Remove(textBox.TextBoxElement.TextBoxItem);  
    RadTextBoxItem textBoxItem = new RadTextBoxItem(new TextBoxNoMenu());  
    textBoxItem.Text = textBox.Text;  
    textBox.TextBoxElement.Children.Add(textBoxItem);  

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,
Angel
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Angel
Telerik team
answered on 30 Oct 2008, 09:41 AM
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)  
{  
    textBox.TextBoxElement.Children.Remove(textBox.TextBoxElement.TextBoxItem);  
    RadTextBoxItem textBoxItem = new RadTextBoxItem(new TextBoxNoMenu());  
    textBoxItem.BindProperty(RadItem.TextProperty, textBox.TextBoxElement,  
        RadItem.TextProperty, PropertyBindingOptions.TwoWay);  
    textBox.TextBoxElement.Children.Add(textBoxItem);  

Regards,
Angel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ofer
Top achievements
Rank 1
answered on 25 Jun 2009, 07:14 PM
Hi,

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!
0
Martin Vasilev
Telerik team
answered on 01 Jul 2009, 11:56 AM
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.
0
Howard Hill
Top achievements
Rank 1
answered on 11 Jan 2011, 04:49 PM
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.

Thanks!

Howard
0
Richard Slade
Top achievements
Rank 2
answered on 11 Jan 2011, 05:15 PM
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
Richard
Tags
TextBox
Asked by
Renato Lopes
Top achievements
Rank 1
Answers by
Angel
Telerik team
Ofer
Top achievements
Rank 1
Martin Vasilev
Telerik team
Howard Hill
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Share this question
or