Windows Develop Bookmark and Share   
 index > Windows Forms General > TextBox.ShortcutsEnabled Property
 

TextBox.ShortcutsEnabled Property

I have a TextBox control in a MDI child form. The top level Form1 has menus and one of the menu items has Ctrl-Z assigned as a short cut.

My TextBox has ShortcutsEnabled set to true. When it is in focus and I type something and then I press Ctrl-Z, instead of undoing what I have typed in the TextBox, the Form1 Ctrl-Z menu item gets activated. Q: What did I do worng?

However, Ctrl-X, Ctrl-C,and Ctrl-V, which Form1 does not use, works fine as expected. Ctrl-A, which Form1 also does not use, produces a ding sound and nothing happens.

Thanks.

PS: The MSDN documentation for TextBoxBase.ShortcutsEnabled property has this cryptic note: "The TextBox control does not support shortcut keys."

K.Kong  Sunday, September 20, 2009 7:27 AM
I tested that code before I posted it, the undo of the text box worked fine. Not sure why it wouldn't work for you. You can alter it and directly call the Undo() function:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.Z)) {
if ((this.ActiveControl is TextBoxBase))
(this.ActiveControl as TextBoxBase).Undo();
else undoToolStripMenuItem.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}


Hans Passant.
  • Marked As Answer byK.Kong Monday, September 21, 2009 1:10 PM
  •  
nobugz  Monday, September 21, 2009 12:03 PM
The mistake of course is using a menu item shortcut keystroke that is used by controls. You can rescue it like this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.Z)) {
if (!(this.ActiveControl is TextBoxBase)) {
undoToolStripMenuItem.PerformClick();
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

However, considering the amount of confusion this will cause for the user, you probably should just select another shortcut key.

Hans Passant.
nobugz  Sunday, September 20, 2009 5:03 PM
Thanks.

As you have guessed, Ctrl-Z is undo. It's different contexts. Ctrl-Z while editing the text box undos what being typed. Whereas, Ctrl-Z at the application level undos what's done at the application.

Your code traps the control key nicely but it does not activate the Undo function in the TextBox control. base is the base form.

From the documentation for ProcessCmdKey I shouldn't have to do anything. "If the command key is not a menu shortcut and the control has a parent, the key is passed to the parent's ProcessCmdKey method." seems to imply that the TextBox's should process Ctrl-Z first. But it's not working that way. :(
K.Kong  Monday, September 21, 2009 3:30 AM
I tested that code before I posted it, the undo of the text box worked fine. Not sure why it wouldn't work for you. You can alter it and directly call the Undo() function:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.Z)) {
if ((this.ActiveControl is TextBoxBase))
(this.ActiveControl as TextBoxBase).Undo();
else undoToolStripMenuItem.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}


Hans Passant.
  • Marked As Answer byK.Kong Monday, September 21, 2009 1:10 PM
  •  
nobugz  Monday, September 21, 2009 12:03 PM
Sorry, I didn't realize there's an Undo() method for TextBox. Now your code works!

Thanks.
K.Kong  Monday, September 21, 2009 1:10 PM

You can use google to search for other answers

Custom Search

More Threads

• toolstrip resizes my images out of allignment
• is there a design pattern to acess main Form elements from extern class?
• In need of some help.... PLEASE!!!!
• DataSet.cs
• Processes and connections
• Create SQL Logins Using C#.net
• Can Access file operated by multiple thread simultaneously
• Getting a form to appear only once
• WebBrowser Control & Excel
• RichTextBox formatting HELP