14

Goal: A Menustrip with Copy and Paste and the user shall see the Shortcut-Keys.

MenuStrip blocks TextBoxes

Problem: If you have a MenuStrip and set the ShortcutKeys the are "catched" by the Menu but no longer by the Textboxes. This means you cannot use Ctrl+C / V in the Textboxes - only by Right-Click. If you remove the Shortcuts the Textboxes work fine.

Why is that? Whats the solution if I dont want to name the Entry "Copy______Ctrl+C"?

Example Project: http://www.file-upload.net/download-4098087/MenuBlocksSTRG.zip.html

MSDN is down ATM i found this links:

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
user799821
  • 218
  • 2
  • 10

4 Answers4

2

If it still matters, the simple solution might be: Show only the shortcut keys text, as in the image.

Ctrl + V

In the TextBox set ShortcutsEnabled to true. That's all!

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
2

This should work for copy, and you can take care of paste in same way:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.C) && textBox1.ContainsFocus)
        {
            Clipboard.SetText(textBox1.SelectedText);
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • Building the Clipboard is cumbersome. What if the Cursor starts not at the beginning of the textbox but in the middle of the text. What if some text is selected to copy or to replace. I choose to set the Menu Text to "Copy Ctrl+C" – user799821 Feb 10 '12 at 09:35
  • This works for copy in all cases (including the one you said), it takes selected text from textbox into clipboard. Did you try it? If you can't make paste according to this idea, report, so I'll write some code. And if you think that it is cumbersome, you asked to work around the way .net works. That is cumbersome. – Ivan Ičin Feb 10 '12 at 10:41
  • Excellent solution. Though I changed the check to a more general `this.ActiveControl is TextBox`, since the rest of my program is an image editor :) – Nyerguds Dec 08 '16 at 20:13
1

You probably have to handle things yourself in those cases.

Simple example:

private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
  if (this.ActiveControl is TextBox) {
    Clipboard.SetText(((TextBox)this.ActiveControl).SelectedText);
  } else {
    // do your menu Edit-Copy code here
  }
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e) {
  if (this.ActiveControl is TextBox) {
    ((TextBox)this.ActiveControl).SelectedText = Clipboard.GetText();
  } else {
    // do you menu Edit-Paste code here
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Building the Clipboard is cumbersome. What if the Cursor starts not at the beginning of the textbox but in the middle of the text. What if some text is selected to copy or to replace. I choose to set the Menu Text to "Copy Ctrl+C" – user799821 Feb 10 '12 at 09:36
  • did you even try to apply code from answer, or you just said it is cumbersome? because, you would realize that this is not answer for you, it requires that when your menu is clicked, it handles copy or paste from your textbox, which is not what your menu should work. – Ivan Ičin Feb 10 '12 at 10:53
  • @user799821 Not sure what you mean. The posted example does the work for you. The `SelectedText` property is returning what is currently highlighted in the text box. The "else" part of the code is to handle copy-paste functions for non-textbox controls. If the menu functions are only for the textbox controls, then you won't have to worry about the "else" part. – LarsTech Feb 10 '12 at 16:49
  • It would make the menus unintuitive, though, since people would expect those menus to handle the global copy-paste of whatever it is the program normally handles, not the one in the textbox. – Nyerguds Dec 08 '16 at 19:32
0

You need something like this?

ToolStripMenuItem Quit = new ToolStripMenuItem();
        Quit.Name = "quitToolStripMenuItem";
        Quit.Text = "&Quit";
        Quit.ShortcutKeys = Keys.Alt | Keys.F4;
        Quit.Click += new EventHandler(quitToolStripMenuItem_Click);
NoX2SR
  • 1