Windows Develop Bookmark and Share   
 index > Windows Forms General > How to enable Ctrl+C, Ctrl+V, Ctrl+X in a text box?
 

How to enable Ctrl+C, Ctrl+V, Ctrl+X in a text box?

 

Hi,

I've already set the ShortcutsEnabled property to true but still cannot use keyboard shortcuts to do "copy", "cut", "paste" ,"select all", "undo" , "redo", etc.

Did I miss anything?

 

Thanks in advance

ps. i'm using VC#

kari0002  Thursday, May 11, 2006 9:21 AM
Hi,

TextBox by default can work with clipboard shortcuts, Ctrl-X for Cut, Ctrl-V for Paste and Ctrl-C for Copy. Alternatively, the control has methods you can use to perform these clipboard operations:



textBox1.Cut();
textBox1.Copy();
textBox1.Paste();



As for SelectAll, Undo and Redo, textBox has methods for this operations as well:



textBox1.Undo();
textBox1.Undo(); // Second Undo will be the Redo operation
textBox1.SelectAll();



But since TextBox can only Undo the last operation, there is no Redo method; If you need Redo, just invoke Undo for the second time. Also, you can get Undo() method to execute by Ctrl-Z shortcut key.

Hope this help,

-chris
Chris Vega  Tuesday, May 23, 2006 5:21 AM
Anybody can help me?
kari0002  Monday, May 15, 2006 12:44 AM
Hi,

TextBox by default can work with clipboard shortcuts, Ctrl-X for Cut, Ctrl-V for Paste and Ctrl-C for Copy. Alternatively, the control has methods you can use to perform these clipboard operations:



textBox1.Cut();
textBox1.Copy();
textBox1.Paste();



As for SelectAll, Undo and Redo, textBox has methods for this operations as well:



textBox1.Undo();
textBox1.Undo(); // Second Undo will be the Redo operation
textBox1.SelectAll();



But since TextBox can only Undo the last operation, there is no Redo method; If you need Redo, just invoke Undo for the second time. Also, you can get Undo() method to execute by Ctrl-Z shortcut key.

Hope this help,

-chris
Chris Vega  Tuesday, May 23, 2006 5:21 AM

I think what he means is, when he focuses the text box and presses Ctrl+C on the keyboard, nothing happens. Nothing is copied to the clipboard.

Im facing this problem myself now - Copy, Cut and Paste all work, but pressing Ctrl+A to seelct all does NOT, yet if i right click, i can choose SelectAll from the textbox's context menu

In a VB.net program i'm writing, usiong Ctrl+C to copy, Ctrl+X to cut and Ctrl+V to paste dont work at all in the textbox when the app is running. Why is there a discrepancy? What can be done to get the keys working?

Surely i dont ahve to write an event handler for every text box, just to trap Ctrl+A?

cjard  Wednesday, May 31, 2006 8:36 AM

Incidentally, I discovered why this was:

The MDI parent form was consuming the keypress events and preventing them reaching the MDI child. preventing the consumption was the clue to having Ctrl combinstions start working on my MDI child once again

cjard  Thursday, September 14, 2006 11:25 AM

Would you be able to explain in greater detail how you were able to prevent the MDI Parent from consuming the keypress event? I have the same problem and can not figure out how to fix it.

Thanks.

wabs27  Thursday, September 21, 2006 10:16 PM

You can solve the problem by overiding ProcessCmdKey in the MDI parent form and using the sendmessage api function to reroute the messages to the active child form:

First declare the SendMessage api function as follows;

Private Declare Auto Function SendMessage Lib "user32" ( ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr ) As IntPtr

Then overide ProcessCmdKey in the MDI parent:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

SendMessage(Me.ActiveMdiChild.Handle, msg.Msg, msg.WParam, msg.LParam)

'Return MyBase.ProcessCmdKey(msg, keyData)

End Function

Good luck!

  • Proposed As Answer bymag78 Saturday, September 12, 2009 9:57 PM
  •  
Olusola Abiodun  Wednesday, October 25, 2006 1:26 AM
Hi,
I had tried with the solution provided by you above but it is not solving mine issue. On MDI Child Form, Cut, Copy and Paste is not working. I had written these lines of codes in my MDI Parent. Does we need to write something else to activate the keyboard shortcuts for these functionality.

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

protected override bool ProcessCmdKey(ref Message message,Keys keydata)
{
SendMessage(this.ActiveMdiChild.Handle, message.Msg, message.WParam, message.LParam);
return base.ProcessCmdKey(ref message, keydata);
}

Kindly help me in fixing this issue.

Thanks & Regards,
Nitin

Nitin Kumar Srivastava  Friday, May 15, 2009 12:50 PM
It works perfect! Thank you
mag78  Saturday, September 12, 2009 9:58 PM

You can use google to search for other answers

Custom Search

More Threads

• Deployment problem?
• Button is not Covered by Form in MDIParent
• row color change on datagridview after databinding
• Transparent MDIChild window
• Treeview nodes level ? and TreeView Instant Search ?
• The destination thread no longer exists.
• Using WebBrowser Control to highlight links
• Inherit from Dataset?
• does not contain a definition AutoSizeMode.EnableAutoSize
• Multiline Textbox