ContextMenu in textbox

8 posts, 0 answers
  1. NS
    NS avatar
    179 posts
    Member since:
    Jan 2007

    Posted 14 May 2009 Link to this post

    Hi,
    Would it be possible to use the ContextMenu when you right click in a silverlight textbox ?
    I would like to make a contextmenu with a "Copy" and "Paste" item in it.

    Thanks,
    Nicolas
  2. Boyan
    Admin
    Boyan avatar
    758 posts

    Posted 15 May 2009 Link to this post

    Hello Nicolas,

    Yes, it is possible to use ContextMenu on all kind of controls. Please note that in order to have context menu on right click you have to set your Silverlight application to be windowless(this is done in your .aspx file) .
     I have attached a simple example how it is done. Please let me know if you need more help.

    Kind regards,
    Boyan
    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.
  3. DevCraft banner
  4. Lance
    Lance avatar
    34 posts
    Member since:
    Sep 2007

    Posted 29 Apr 2010 Link to this post

    Hi Boyan,

    Is it possible to do this application-wide so all textboxes have the right-click copy, cut, and paste functionality rather than implement this for each textbox in my application?

    Please let me know.

    Thanks
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 29 Apr 2010 Link to this post

    Hello Tim English,

    Using single instance of RadContextMenu on multiple controls is not supported. So you cannot do application wide context menus.
    We will try to improve this in some future release.

    Sincerely yours,
    Hristo
    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.
  6. Lance
    Lance avatar
    34 posts
    Member since:
    Sep 2007

    Posted 06 May 2010 Link to this post

    Is there any way to get the parent textbox of the RadContextMenu in the Click, Opened, and Closed events so that I can share these events between the various textboxes in which I am using the RadContextMenu for the right-click cut, copy, paste functionality?  Please let me know.  Thanks.
  7. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 06 May 2010 Link to this post

    Hello Tim English,

    RadContextMenu have UIElement property that you can use to get the element on which it is attached.
    Note that if you use single instance of RadContextMenu when attach it to new control it will detach from the old one (e.g. cannot use single instance on multiple controls).

    All the best,
    Hristo
    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. Lance
    Lance avatar
    34 posts
    Member since:
    Sep 2007

    Posted 06 May 2010 Link to this post

    Hi,
    Thanks, that was what I needed.  I wanted to have several textboxes with context menus for cut, copy, and paste, but only wanted one set of functions to handle them all.  In case it helps anyone else below is my code(modified from a previous telerik example):

    The xaml copied to each textbox:

     

     

     

    <telerikNavigation:RadContextMenu.ContextMenu>

     

     

     

    <telerikNavigation:RadContextMenu Opened="ContextMenuOpened" Closed="ContextMenuClosed" >

     

     

     

    <telerikNavigation:RadMenuItem Header="Cut" Click="RadMenuItem_Click" />

     

     

     

    <telerikNavigation:RadMenuItem Header="Copy" Click="RadMenuItem_Click" />

     

     

     

    <telerikNavigation:RadMenuItem Header="Paste" Click="RadMenuItem_Click" />

     

     

     

    </telerikNavigation:RadContextMenu>

     

     

     

    </telerikNavigation:RadContextMenu.ContextMenu>

     

     

     

     


    The generic event handling functions (I am using the extension method from the telerik post with the download - ContextMenuOnTextBox_6E924F29):

     

     

    private void RadMenuItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)

     

    {

    Telerik.Windows.Controls.

     

     

     

     

    RadMenuItem item = e.OriginalSource as Telerik.Windows.Controls.RadMenuItem;

     

    Telerik.Windows.Controls.

     

     

     

     

    RadContextMenu menu = item.Parent as Telerik.Windows.Controls.RadContextMenu;

     

     

     

     

     

     

    if (menu != null)

     

    {

     

     

     

     

     

    TextBox txt = menu.UIElement as TextBox;

     

     

     

     

     

     

    if (txt != null)

     

    {

     

     

     

     

     

    switch (item.Header.ToString())

     

    {

     

     

     

     

     

    case "Cut":

     

    txt.Cut();

     

     

     

     

     

    break;

     

     

     

     

     

     

    case "Copy":

     

    txt.Copy();

     

     

     

     

     

    break;

     

     

     

     

     

     

    case "Paste":

     

    txt.Paste();

     

     

     

     

     

    break;

     

    }

    }

    }

    }

     

     

     

     

     

    private void ContextMenuOpened(object sender, RoutedEventArgs e)

     

    {

     

     

     

     

     

    // Customize the context menu depending on the selection and the clipboard content

     

     

     

     

    Telerik.Windows.Controls.

     

    RadContextMenu menu = sender as Telerik.Windows.Controls.RadContextMenu;

     

     

     

     

     

     

    if (menu != null)

     

    {

     

     

     

     

     

    TextBox txt = menu.UIElement as TextBox;

     

     

     

     

     

     

    if (txt != null)

     

    {

     

     

     

     

     

    bool hasSelection = txt.SelectionLength > 0;

     

    (menu.Items[

     

     

     

     

    0] as Telerik.Windows.Controls.RadMenuItem).IsEnabled = hasSelection; // Cut

     

     

     

     

    (menu.Items[

     

    1] as Telerik.Windows.Controls.RadMenuItem).IsEnabled = hasSelection; // Copy

     

     

     

     

     

     

    bool hasClipboardContent = !string.IsNullOrEmpty(Clipboard.Paste());

     

    (menu.Items[

     

     

     

     

    2] as Telerik.Windows.Controls.RadMenuItem).IsEnabled = hasClipboardContent; // Paste

     

     

     

     

    }

    }

    }

     

     

    private void ContextMenuClosed(object sender, System.Windows.RoutedEventArgs e)

     

    {

    Telerik.Windows.Controls.

     

     

     

     

    RadContextMenu menu = sender as Telerik.Windows.Controls.RadContextMenu;

     

     

     

     

     

     

    if (menu != null)

     

    {

     

     

     

     

     

    TextBox txt = menu.UIElement as TextBox;

     

     

     

     

     

     

    if (txt != null)

     

    {

     

     

     

     

     

    // RadMenu and RadContextMenu steal the focus in order to provide

     

     

     

     

     

     

    // keyboard navigation. That's why we need to focus the text box

     

     

     

     

     

     

    // again when the menu is closed.

     

     

     

     

    txt.Focus();

    }

    }

    }

  9. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 10 May 2010 Link to this post

    Hello Tim English,

    Just some small tips. RadContextMenu and RadMenu expose ItemClick event so you can use it instead of adding handles to all RadMenuItems.
    Or even better approach is to use RoutedCommands instead of events.

    All the best,
    Hristo
    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.
Back to Top
DevCraft banner