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

RadMaskedEditBox context menu missing

5 Answers 68 Views
MaskedEditBox
This is a migrated thread and some comments may be shown as answers.
Chet Musialowski
Top achievements
Rank 1
Chet Musialowski asked on 07 Sep 2011, 07:34 PM
I have upgraded to RadControls for WinForms  Q2 2011 (version 2011.2.11.831) from Q3 2010 SP1 (version 2010.3.10.1215)

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

Sort by
0
Peter
Telerik team
answered on 08 Sep 2011, 01:56 PM
Hi Chet Musialowski,

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

0
Chet Musialowski
Top achievements
Rank 1
answered on 09 Sep 2011, 02:18 PM
Peter,

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


0
Peter
Telerik team
answered on 14 Sep 2011, 04:29 PM
Hello 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
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
0
Chet Musialowski
Top achievements
Rank 1
answered on 14 Sep 2011, 05:30 PM
Peter,

I am unable to find  this.TextBoxItem.TextBoxControl.Undo();


Is this.TextBoxItem.ClearUndo();  
what you meant ?

Chet

0
Accepted
Peter
Telerik team
answered on 17 Sep 2011, 02:39 PM
Hello 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,

Peter
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
MaskedEditBox
Asked by
Chet Musialowski
Top achievements
Rank 1
Answers by
Peter
Telerik team
Chet Musialowski
Top achievements
Rank 1
Share this question
or