INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!

*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.

Jobs

Jobs from Indeed

I need something like "OnPaste event" for TextBox

I need something like "OnPaste event" for TextBox

(OP)
Hi

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

(OP)
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
    oldTxt=txt1.Text;
    }
    else
    {
        //one or more characters are disallowed
        //return to the old value
        txt1.Text=oldTxt;
    }
}

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;
        #endregion

        /// <summary>
        /// Capture command keys
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch(keyData)
            {
                case Keys.Enter:
                    if(this._TabOnEnter)
                        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;
                default:
                    return base.ProcessCmdKey (ref msg, keyData);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            switch(m.Msg)
            {
                case WM_COPY:
                    if(this._ClipMode == ClipModeConstants.mskExcludeLiterals)
                        Clipboard.SetDataObject(RemoveFormatCharacters(base.SelectedText).Replace(this._PromptChar, ""));
                    else
                        Clipboard.SetDataObject(base.SelectedText.Replace(this._PromptChar, " "));
                    break;
                case WM_PASTE:
                case WM_CLEAR:
                    base.WndProc(ref m);
                    break;
                case WM_CUT:
                    Clipboard.SetDataObject(base.SelectedText);
                    this.InsertChararacter(1, "");
                    break;
                //TODO: ova ne ke bide loso da se napravi
                case WM_UNDO:
                case EM_UNDO:
                    m.Result = new IntPtr(1);
                    break;
                case EM_CANUNDO:
                    m.Result = new IntPtr(0);
                    break;
                default:
                    base.WndProc (ref m);
                    break;
            }
        }

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!

Resources

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