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

ContextMenu in textbox

7 Answers 115 Views
Menu
This is a migrated thread and some comments may be shown as answers.
NS
Top achievements
Rank 1
NS asked on 14 May 2009, 01:19 PM
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

7 Answers, 1 is accepted

Sort by
0
Boyan
Telerik team
answered on 15 May 2009, 02:17 PM
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.
0
Lance
Top achievements
Rank 1
answered on 29 Apr 2010, 08:35 AM
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
0
Hristo
Telerik team
answered on 29 Apr 2010, 11:51 AM
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.
0
Lance
Top achievements
Rank 1
answered on 06 May 2010, 11:04 AM
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.
0
Hristo
Telerik team
answered on 06 May 2010, 04:02 PM
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.
0
Lance
Top achievements
Rank 1
answered on 07 May 2010, 12:43 AM
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();

}

}

}

0
Hristo
Telerik team
answered on 10 May 2010, 12:39 PM
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.
Tags
Menu
Asked by
NS
Top achievements
Rank 1
Answers by
Boyan
Telerik team
Lance
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or