I wanted to change default textbox context menu, so I created my own menu and th开发者_开发知识库em I assigned it like that
texbox.ContextMenu = myContextMenu
However I don't know how to restore default textbox menu (in a runtime). I need myContextMenu to show only when I click textbox with right mouse button (while holding Control button). In other cases I need default textbox contextmenu to show. Is it possible ??
Here is the example given by Microsoft:
http://msdn.microsoft.com/en-us/library/ms750420.aspx
For the record, here is the way to do this using WinForms:
public partial class TextBoxContextMenuDemo : Form
{
ContextMenu mnuContextDefault;
ContextMenu mnuContextAlt;
MenuItem mnuItmAltMenuTest;
public TextBoxContextMenuDemo()
{
InitializeComponent();
InitializeAltContextMenu();
}
private void InitializeAltContextMenu()
{
mnuContextDefault = new ContextMenu();
mnuContextDefault = this.TextBox1.ContextMenu;
mnuItmAltMenuTest = new MenuItem();
mnuItmAltMenuTest.Index = -1;
mnuItmAltMenuTest.Text = "Test Menu Item";
mnuItmAltMenuTest.Click += new System.EventHandler(this.mnuItmAltMenuTest_Click);
mnuContextAlt = new ContextMenu();
mnuContextAlt.MenuItems.Add(mnuItmAltMenuTest);
}
private void TextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if ((Control.ModifierKeys == Keys.Control))
{
this.TextBox1.ContextMenu = mnuContextAlt;
TextBox1.ContextMenu.Show(TextBox1, new Point(e.X, e.Y));
}
else
{
this.TextBox1.ContextMenu = mnuContextDefault;
}
}
}
private void mnuItmAltMenuTest_Click(object sender, System.EventArgs e)
{
MessageBox.Show("You clicked the alternate test menu item!");
}
}
HTH!
It would actually be more difficult to do than it would first seem. I believe that the default context menu is part of the actual template of the control.
The simplest approach, if you only want Cut/Copy/Paste, is to create a second ContextMenu implementing those options. If you do, you can use the built in ApplicationCommands to implement not only the functionality, but also to automatically localize this ContextMenu.
You could just set the ContextMenu-Property to null. Also the OnContextMenuOpening event can help you.
精彩评论