Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


I need something like "OnPaste event" for TextBox

I need something like "OnPaste event" for TextBox

I need something like "OnPaste event" for TextBox


I would like to write a special handling of paste operation for a TextBox. (Only some characters are allowed in this TextBox).
I have written code proving pasted text when the user presses CTRL+INS or CTRL+V. But I have no idea how to catch "paste operation" from context menu (right mouse click on the TextBox).
An Event like "OnPaste" would be perfect, but I cannot find such an event :(

Thanks for any help

RE: I need something like "OnPaste event" for TextBox

use the text changed event do to all the validation on the textbox.

"two wrongs don't make a right, but three lefts do" - the unknown sage

RE: I need something like "OnPaste event" for TextBox

Yes this is a good idea but new problem appears:
When a text is pasted I would like
A) to allow to past it or
B) to deny.

When I allow - I left the text as it is.
When I deny - I want to return to the previous text.

Have I to declare an additional String oldText? May by do you know another method?

private void txt1_TextChanged(object sender, System.EventArgs e)
    if( IsEverythingOK(txt1.Text) )
        //characters are ok - remember this new value
        //one or more characters are disallowed
        //return to the old value

RE: I need something like "OnPaste event" for TextBox

yes, you need to declare an additional string variable that holds the last correct value.

"two wrongs don't make a right, but three lefts do" - the unknown sage

RE: I need something like "OnPaste event" for TextBox

        #region constants
        private const int WM_COPY = 0x301;
        private const int WM_CUT = 0x300;
        private const int WM_PASTE = 0x302;
        private const int WM_CLEAR = 0x303;
        private const int WM_UNDO = 0x304;
        private const int EM_UNDO = 0xC7;
        private const int EM_CANUNDO = 0xC6;

        /// <summary>
        /// Capture command keys
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
                case Keys.Enter:
                        return base.ProcessCmdKey(ref msg, Keys.Tab);
                    return true;
                case Keys.Control | Keys.V:
                case Keys.Shift | Keys.Insert:
                    PostMessage(this.Handle, WM_PASTE, 0, 0);
                    return true;
                case Keys.Control | Keys.C:
                case Keys.Control | Keys.Insert:
                    PostMessage(this.Handle, WM_COPY, 0, 0);
                    return true;
                case Keys.Control | Keys.X:
                case Keys.Shift | Keys.Delete:
                    PostMessage(this.Handle, WM_CUT, 0, 0);
                    return true;
                case Keys.Control | Keys.Delete:
                    PostMessage(this.Handle, WM_CLEAR, 0, 0);
                    return true;
                case Keys.Control | Keys.Z:
                    PostMessage(this.Handle, WM_UNDO, 0, 0);
                    return true;
                    return base.ProcessCmdKey (ref msg, keyData);
        /// <summary>
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
                case WM_COPY:
                    if(this._ClipMode == ClipModeConstants.mskExcludeLiterals)
                        Clipboard.SetDataObject(RemoveFormatCharacters(base.SelectedText).Replace(this._PromptChar, ""));
                        Clipboard.SetDataObject(base.SelectedText.Replace(this._PromptChar, " "));
                case WM_PASTE:
                case WM_CLEAR:
                    base.WndProc(ref m);
                case WM_CUT:
                    this.InsertChararacter(1, "");
                //TODO: ova ne ke bide loso da se napravi
                case WM_UNDO:
                case EM_UNDO:
                    m.Result = new IntPtr(1);
                case EM_CANUNDO:
                    m.Result = new IntPtr(0);
                    base.WndProc (ref m);

I've paste complete working code. You must comment some lines where my special properties are included. I have one problem with this code and if you have a opinion make a post. I don't now how make code part for handling WM_UNDO, EM_UNDO and EM_CANUNDO, to work.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close