Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:41:57+00:00 2026-06-01T22:41:57+00:00

Is there a way by which we can find out if a clip board

  • 0

Is there a way by which we can find out if a clip board paste event occurred in a rich text box? This event would be used in order to do certain stuff, with the pasted block of text.

thanks

Here is my code

 protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        if (m.Msg == WM_PASTE)
        {
            OnPasteOccurred();
            MessageBox.Show("Pas");
        }
        if (m.Msg == 0x000F)
        {
            if (PaintControl)
            {
                base.WndProc(ref m);
            }
            else
            {
                m.Result = IntPtr.Zero;
            }
        }
        else
        {
            base.WndProc(ref m);
        }
    }

Edit

I wish to do some syntax highlighting or indentation based on paste events, something which this particular code editor seems to be doing very efficiently. I don’t know how it is doing it. Would require help in this particular direction. I am pretty sure that there must some native Win32 code or something like that can be intercepted. I have tried tracking down keys, mouse events and it is not pretty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:41:59+00:00Added an answer on June 1, 2026 at 10:41 pm

    It’s a little bit tricky to detect a paste operation in the RichTextBox.

    First solution may be to detect the WM_PASTE message overriding the WndProc but unfortunately the control doesn’t send that message to itself when it performs a paste operation.

    Naïve detection

    To detect the keyboard events may work (you have to override the OnKeyDown function) then check if the key combinations (CTRL+V and SHIFT+INS). Something like this:

    protected override OnKeyDown(KeyEventArgs e)
    {
         bool ctrlV = e.Modifiers == Keys.Control && e.KeyCode == Keys.V;
         bool shiftIns = e.Modifiers == Keys.Shift && e.KeyCode == Keys.Insert;
    
         if (ctrlV || shiftIns)
             DoSomething();
    }
    

    It works well but you can’t catch the paste operation made using the mouse (right click to open the context menu) and the paste operations made via drag & drop. If you do not need them you can use this solution (at least it’s simply and straightforward).

    Better detection

    Assumption: when user types inside the RichTextBox he inserts one character per time. How can you use this? Well, when you detect a bigger change you detected a paste operation because user can’t type more than once character per time (ok, you can argue that it’s not always true because of Unicode surrogates). See also VB.NET version and more details about Unicode stuff.

    private int _previousLength = 0;
    
    private void richTextBox_TextChanged(object sender, EventArgs e)
    {
       int currentLength = richTextBox.Text.Length;
       if (Math.Abs(currentLength - _previousLength) > 1)
          ProcessAllLines();
    
       _previousLength = currentLength;
    }
    

    Please note that you can’t (because of how different IMEs work) use OnKeyDown (or similar). This works well only for western languages but it has problems with Unicode stuff (because, for example, String.Length property may be increased by two Char when user typed a single character. See also this post for much more details about this (well it’s a strongly suggested reading even, even if – in this case – you don’t care about it). In that post you’ll also find code for a better algorithm to determine string length. In short you have to replace:

       int currentLength = richTextBox.Text.Length;
    

    With this:

       int currentLength = StringInfo.GetTextElementEnumerator(richTextBox.Text)
           .Cast<string>()
           .Count();
    

    After all this effort you may realize that…user can even paste a single character and it may go undetected. You’re right, that’s why this is a better detection instead of a perfect solution.

    Perfect solution

    The perfect solution (if you’re running on Windows 8) of course exists, the native rich edit control sends an EN_CLIPFORMAT notification message. It’s intended to notify a rich edit control’s parent window that a paste occurred with a particular clipboard format. You can then override the WndProc of its parent to detect the WM_NOTIFY message for this notification. Anyway it’s not few lines of code, check this MSDN article for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way in JSF which can be used to find out whether
Is there any way through which we can find out if Java Garbage Collector
Is there any way where I can find out which Java options are being
Is there a way where we can find out which UI element has posted
Is there any way by which I can find out all classes which implement
Is there any way by which i can find out free port on the
Is there any way to find out which UITableViewCell is at the top of
Is there a way in any browser to find out who (which piece of
Is there any way by which we can find the current location from where
I know Guid are randomly generated, Is there any way I can find out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.