I have a problem with the RichTextBox Control.
I need to catch CUT and PASTE (on ctrl+X and Ctrl+V).
So I subclassed RichTextBox, and overrode WndProc looking for WM_PASTE and
WM_CUT (0x0302 and 0x0300).
But I don't get these messages on Ctrl+X or Ctrl+V.
If I subclass a TextBox, I catch the messages as expected, but not in a
RichTextBox.
I do get these messages if I call the Paste() and Cut() methods directly,
but not on the shortcut keys.
Does anyone know how to catch these?
webmasta - 17 Aug 2005 08:58 GMT
try this :
private void Form1_KeyDown(object sender, KeyEventArgs e) //Form keyPreview must set to true
{
if(Control.ModifierKeys == Keys.Shift || Control.ModifierKeys == Keys.Control)
{
IDataObject iData = new DataObject();
iData = Clipboard.GetDataObject();
if(!iData.GetDataPresent(DataFormats.Text)) { e.Handled=true; } //disable ctrlV and
shift+insert for paste
}
}
|I have a problem with the RichTextBox Control.
| I need to catch CUT and PASTE (on ctrl+X and Ctrl+V).
[quoted text clipped - 10 lines]
|
| Does anyone know how to catch these?
webmasta - 17 Aug 2005 09:11 GMT
ooops ..
this line : if(!iData.GetDataPresent(DataFormats.Text)) { e.Handled=true; }
should be
if(iData.GetDataPresent(DataFormats.Text)) { e.Handled=true; }
sorry..
| try this :
|
[quoted text clipped - 24 lines]
||
|| Does anyone know how to catch these?
Chris Badal - 17 Aug 2005 17:40 GMT
Well, it turns out that this was easier than I was thinking it was.
If you add a handler to the KeyDown event, you can catch it.
//========================================================
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
// We call Cut() and Paset() directly on Ctrl+X and Ctrl+V
// then in WndProc, we will handle the cut and paste events.
if(e.Control && e.KeyCode == Keys.V )
{
Paste();
e.Handled = true;
}
else if(e.Control && e.KeyCode == Keys.X )
{
Cut();
e.Handled = true;
}
}
//========================================================
Before I tried this I was sure cut and paste behavior would remain
unchanged, but it actually works.
> ooops ..
>
[quoted text clipped - 34 lines]
> ||
> || Does anyone know how to catch these?