In Q3 2010, the RadMaskedTextBox supports a right click menu. In Q2 2011, it does not.
Am I doing something wrong ? (my code hasn't changed). I noticed that it still does exist on the RadTextBox.
If it was removed (or is a bug), how do I recreate/enable the right click functionality.
Thanks,
Chet
5 Answers, 1 is accepted
Thank you for writing.
Using the default context menu in RadMaskedEditBox may override our validation logic and that is why we decided to disable it. Nevertheless, it is easy to implement a custom context menu. All you need to do is to handle the MouseDown event and show the context menu. Here is a sample:
radMaskedEditBox1.MaskedEditBoxElement.TextBoxItem.HostedControl.MouseDown +=
new
MouseEventHandler(HostedControl_MouseDown);
private
void
HostedControl_MouseDown(
object
sender, MouseEventArgs e)
{
if
(((
int
)(e.Button & System.Windows.Forms.MouseButtons.Right)) != 0)
{
ContextMenu menu =
new
System.Windows.Forms.ContextMenu();
MenuItem copy =
new
MenuItem(
"C&opy"
);
menu.MenuItems.Add(copy);
copy.Click +=
new
EventHandler(copy_Click);
MenuItem paste =
new
MenuItem(
"&Paste"
);
menu.MenuItems.Add(paste);
copy.Click +=
new
EventHandler(copy_Click);
paste.Click +=
new
EventHandler(paste_Click);
menu.Show(
this
.radMaskedEditBox1,
new
Point(10));
}
}
private
void
paste_Click(
object
sender, EventArgs e)
{
this
.radMaskedEditBox1.Value = Clipboard.GetText();
}
private
void
copy_Click(
object
sender, EventArgs e)
{
Clipboard.SetText(
this
.radMaskedEditBox1.SelectedText);
}
I hope this helps. Do not hesitate to contact us if you have further questions.
Kind regards,
Peter
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 >>
Thanks for quick the response. If I do implement the context menu, what possible issues will I run into ?
Also, in doing my research (in the Telerik source code), I found that there are Cut(), Copy(), Paste(), Clear() and SelectAll() methods at the
radMaskedEditBox1.MaskedEditBoxElement.TextBoxItem
level. I put together some code (BaseTextBox class) to use this and it appears to work, but I want to verify which is the correct way.
this.MaskedEditBoxElement.TextBoxItem.MouseDown +=new System.Windows.Forms.MouseEventHandler(TextBoxItem_MouseDown);
void TextBoxItem_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
RadContextMenu contextMenu = new RadContextMenu();
contextMenu.Items.Add(new Telerik.WinControls.UI.RadMenuItem()
{
Text = "Cut",
Name = "Cut"
,
Enabled = ((sender as RadTextBoxItem).SelectionLength > 0)
});
contextMenu.Items["Cut"].Click += new System.EventHandler(this.ContextMenuMenuItem_Click);
contextMenu.Items.Add(new Telerik.WinControls.UI.RadMenuItem()
{
Text = "Copy",
Name = "Copy"
,
Enabled = ((sender as RadTextBoxItem).SelectionLength > 0)
});
contextMenu.Items["Copy"].Click += new System.EventHandler(this.ContextMenuMenuItem_Click);
contextMenu.Items.Add(new Telerik.WinControls.UI.RadMenuItem()
{
Text = "Paste",
Name = "Paste"
,
Enabled = System.Windows.Forms.Clipboard.ContainsText()
});
contextMenu.Items["Paste"].Click += new System.EventHandler(this.ContextMenuMenuItem_Click);
contextMenu.Items.Add(new Telerik.WinControls.UI.RadMenuItem()
{
Text = "Delete",
Name = "Delete"
,
Enabled = ((sender as RadTextBoxItem).SelectionLength > 0)
});
contextMenu.Items["Delete"].Click += new System.EventHandler(this.ContextMenuMenuItem_Click);
contextMenu.Items.Add(new Telerik.WinControls.UI.RadMenuSeparatorItem());
contextMenu.Items.Add(new Telerik.WinControls.UI.RadMenuItem()
{
Text = "Select All",
Name = "Select All"
,
Enabled = ((sender as RadTextBoxItem).SelectionLength > 0)
});
contextMenu.Items["Select All"].Click += new System.EventHandler(this.ContextMenuMenuItem_Click);
System.Drawing.Point p = (sender as Telerik.WinControls.UI.RadTextBoxItem).PointToScreen(e.Location);
contextMenu.Show(p.X, p.Y);
}
}
void ContextMenuMenuItem_Click(object sender, EventArgs e)
{
switch (((Telerik.WinControls.UI.RadMenuItem)(sender)).Name.ToUpper())
{
case "UNDO":
//this.radMaskedEditBox1.MaskedEditBoxElement.TextBoxItem.;
break;
case "CUT":
this.MaskedEditBoxElement.TextBoxItem.Cut();
break;
case "COPY":
this.MaskedEditBoxElement.TextBoxItem.Copy();
break;
case "PASTE":
this.MaskedEditBoxElement.TextBoxItem.Paste();
break;
case "DELETE":
this.MaskedEditBoxElement.TextBoxItem.Clear();
break;
case "SELECT ALL":
this.MaskedEditBoxElement.TextBoxItem.SelectAll();
break;
default:
break;
}
}
There is one odd behavior doing it the way I did, and that is that the context menu doesn't always go away after clicking a menu item.
Thanks,
Chet
Your implementation is very complete and useful.
You can avoid the issue with the context menu that does not always close after clicking the new menu item by manually calling the ClosePopup method:
contextMenu.DropDown.ClosePopup(RadPopupCloseReason.Mouse);
As for the issue where the user can paste text in the RadMaskedEditBox and this text is not validated for some mask types (IP Mask for example), you can try to avoid this with calling Validate(text) after Paste operation:
case
"PASTE"
:
this
.TextBoxItem.Paste();
if
(!
this
.Provider.Validate(
this
.Text))
{
this
.TextBoxItem.TextBoxControl.Undo();
}
In addition, I have updated your Telerik Points for your community efforts. Do not hesitate to contact us if you have other questions.
Best wishes,
Peter
the Telerik team
I am unable to find this.TextBoxItem.TextBoxControl.Undo();
what you meant ?
Is this.TextBoxItem.ClearUndo();
Chet
Please accept our apologies for the introduced inconvenience. TextBoxControl is an internal property and you should use:
((TextBox)
this
.MaskedEditBoxElement.TextBoxItem.HostedControl).Undo();
I hope this helps. If you have additional questions, feel free to write back.
Regards,
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>