Windows Develop Bookmark and Share   
 index > Windows Forms General > Ctrl-Ins Shift-Ins
 

Ctrl-Ins Shift-Ins

Is it possible to intercept/redefine Ctrl-Ins and Shift-Ins in a TextBox Control ?
for example when I press Ctrl-C or Ctrl-V the KeyPress event is fired and is also possible tointercept Ctrl-C Ctrl-Vviaa Menu Strip

Thank you
Manuel
Manuel Marini  Friday, September 18, 2009 7:58 AM
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. I had to guess at the meaning of the shortcut keys, much like your user will.

using System;
using System.Windows.Forms;

class MyTextBox : TextBox {
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.Insert)) {
this.Paste();
return true;
}
if (keyData == (Keys.Shift | Keys.Insert)) {
this.Copy();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}

Hans Passant.
nobugz  Saturday, September 19, 2009 7:23 PM
Well... It seems thatcan intercept Ctrl-Ins or Shift-Ins only using a MenuStrip or ContextMenuStrip...
Manuel Marini  Friday, September 18, 2009 8:08 AM
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. I had to guess at the meaning of the shortcut keys, much like your user will.

using System;
using System.Windows.Forms;

class MyTextBox : TextBox {
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.Insert)) {
this.Paste();
return true;
}
if (keyData == (Keys.Shift | Keys.Insert)) {
this.Copy();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}

Hans Passant.
nobugz  Saturday, September 19, 2009 7:23 PM

Hi Manuel Marini,

Could you please tell me why you want to make these two shortcut keys? Shift + Insert key has been defined by the system. If you replace it with your own logical, will it affect user experience?

Anyway, I think nobugz’s sample code is suitable for your requirement. Override ProcessCmdKey can intercept user input in your .NET application. Since system doesn’t define “Control+Insert� you can easily handle other event such as the KeyDown event tointercept "Control+Insert" and codeyour own logical.

If I misunderstood you, please feel free to tell me.

Sincerely,

Kira Qian

Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 21, 2009 6:28 AM

Hi,

The content of the text box has to be sent to a very old communication system (telex) this system doesn't support some characters. I need to filter these characters directly when the user types themand I need also to filter the paste operation (Shift-Ins and Ctrl-V) to have WYSIWYG editing.
For example è has to become E', $ has to become USD.

I was looking for a solution with some kind of subclassing and overriding the ProcessCmdKey is perfect for my needs.

Thank you very much Kira Qian and Hans Passant.


Manuel Marini

Manuel Marini  Monday, September 21, 2009 8:53 AM

You can use google to search for other answers

Custom Search

More Threads

• Filter/Search records in DataGridView?
• picture box binding
• is there any way to scan a windows form for controls and objects ?
• Threading Query
• Unicode Handling
• disabled control w/o enable = false?
• Windows Authentication with hosted SQL Server?
• Add Method to MDIParen or Child?
• Manage the control element in a "unique" event
• How to disable treeview node for good?