Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2005

Tip: Looking for answers? Try searching our database.

RichTextBox how to catch CUT and PASTE?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Badal - 10 Aug 2005 17:35 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.