RichTextBox how to catch CUT and PASTE?

G

Guest

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?
 
W

webmasta

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).
|
| 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?
 
W

webmasta

ooops ..

this line : if(!iData.GetDataPresent(DataFormats.Text)) { e.Handled=true; }

should be

if(iData.GetDataPresent(DataFormats.Text)) { e.Handled=true; }

sorry..




| 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).
||
|| 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?
|
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top