|
Hello,
Visual Studio 2008 SP1/Vista
I've notice that whenI add a MenuStrip control to aForm wirh the 'Insert Standard Items' selected, things get weird withshort cut keys.
For example, if I add a RichTextBox control to a Form (with no MenuStrip), I can cut and paste code to and from the RichTextBox using Ctrl-C and Ctrl-V.
When I add a MenuStrip with 'Insert Standard Items' selected, I can no longer use Ctrl-C and Ctrl-V to cut and paste in the RichTextBox. I have to useCtrl-Shift-C and Ctrl-Shift-V.
I assume this is because the Insert Standard Items option adds menu itemsfor cut and paste with Ctrl-C and Ctrl-V automatically set as their shortcut keys. So somehowthe RichTextBox control never recieves the Ctrl-C and Ctrl-V keypress.
How does one handle this if one wants the the standard Ctrl-C and Ctrl-V combinations to work at both the level of the MenuStrip and the RichTextbox.?
I hope thats relatively clear. The behavior is easy enough to duplcate just by simply dropping a MenuStrip with 'Insert Standard Items' selected and a RichTextBox control on a form.
Thanks,
Bob | | Bob Bedell Sunday, April 12, 2009 4:05 AM | A bunch of Googling led me to the following solution. My RichTextBox control is in a SplitContianer control. Once the "recursive" FindFocusedControl method determines that the RichTextBox is the active control, I can callthe RichTextBox'sCopy and Paste methods in the Click events of the Copy and Paste MenuItem controls that have their shortcut keys set to Ctrl-C and Ctrl-V. Now Ctrl-C and Ctrl-V work as expected directly on text in the RichTextBox as well.
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
Control focuedControl = FindFocusedControl(this.ActiveControl);
RichTextBox rtb = focuedControl as RichTextBox;
if (rtb != null)
{
rtb.Copy();
};
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
Control focuedControl = FindFocusedControl(this.ActiveControl);
RichTextBox rtb = focuedControl as RichTextBox;
if (rtb != null)
{
rtb.Paste();
};
}
public Control FindFocusedControl(Control control)
{
ContainerControl container = control as ContainerControl;
while (container != null)
{
control = container.ActiveControl;
container = control as ContainerControl;
}
return control;
}
- Marked As Answer byLing WangMSFT, ModeratorMonday, April 20, 2009 12:14 PM
-
| | Bob Bedell Sunday, April 12, 2009 4:56 AM | Awkward problem, the shortcuts in the menu items see the key first. One possible approach I see is to handle the shortcut key at the form level. Reset the ShortCutKeys property of the menu item and set its ShortCutKeyDisplay property. Then add this code to the form: protected override bool ProcessDialogKey(Keys keyData) { if (keyData == (Keys.Control | Keys.V)) { pasteToolStripMenuItem_Click(this, EventArgs.Empty); return true; } // etc... return base.ProcessDialogKey(keyData); }
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorMonday, April 20, 2009 12:14 PM
-
| | nobugz Sunday, April 12, 2009 2:37 PM | A bunch of Googling led me to the following solution. My RichTextBox control is in a SplitContianer control. Once the "recursive" FindFocusedControl method determines that the RichTextBox is the active control, I can callthe RichTextBox'sCopy and Paste methods in the Click events of the Copy and Paste MenuItem controls that have their shortcut keys set to Ctrl-C and Ctrl-V. Now Ctrl-C and Ctrl-V work as expected directly on text in the RichTextBox as well.
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
Control focuedControl = FindFocusedControl(this.ActiveControl);
RichTextBox rtb = focuedControl as RichTextBox;
if (rtb != null)
{
rtb.Copy();
};
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
Control focuedControl = FindFocusedControl(this.ActiveControl);
RichTextBox rtb = focuedControl as RichTextBox;
if (rtb != null)
{
rtb.Paste();
};
}
public Control FindFocusedControl(Control control)
{
ContainerControl container = control as ContainerControl;
while (container != null)
{
control = container.ActiveControl;
container = control as ContainerControl;
}
return control;
}
- Marked As Answer byLing WangMSFT, ModeratorMonday, April 20, 2009 12:14 PM
-
| | Bob Bedell Sunday, April 12, 2009 4:56 AM | But if anyone can think of how to do the same with a TreeNode in Edit mode, that is, paste new text into a TreeNode one is editing using the same Ctrl-V strategy as above, I'd love to here it. TreeNodes don't appear to have Copy and Paste methods.
Thanks,
Bob | | Bob Bedell Sunday, April 12, 2009 5:11 AM | Awkward problem, the shortcuts in the menu items see the key first. One possible approach I see is to handle the shortcut key at the form level. Reset the ShortCutKeys property of the menu item and set its ShortCutKeyDisplay property. Then add this code to the form: protected override bool ProcessDialogKey(Keys keyData) { if (keyData == (Keys.Control | Keys.V)) { pasteToolStripMenuItem_Click(this, EventArgs.Empty); return true; } // etc... return base.ProcessDialogKey(keyData); }
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorMonday, April 20, 2009 12:14 PM
-
| | nobugz Sunday, April 12, 2009 2:37 PM | Thanks for the reply. I'll experiment with your suggesstion when I get the chance.
It is an awkward problem, and gets increasingly awkward as one adds more controls. I thought I'd use the Clipboard object's static GetDataObject and SetDataObject methods as a possible solution to the TreeNode edit piece. Unfortunately, doing so fires the TreeViews AfterLabelEdit event, which I'm already using for other purposes, and things get even murkier. May just opt for Ctrl-Shift-Copy and Paste, or disabling the MenuStrip shortcuts,instead of trying to rewire every control on the form.
Thanks again,
Bob | | Bob Bedell Monday, April 13, 2009 8:20 AM |
|