Isaac Bolinger
Isaac Bolinger

Reputation: 7388

How do you detect incoming text ctrl-v pasting in a winforms textbox?

I'm making my own commentbox control that inherits from a winforms textbox. One of the things it does is prevent users from entering any characters if the limit has been hit. So on the keypress event I just grab the incoming key (excepting delete and backspace) and add it on to what's in the textbox already, then check and see what the length is. If it is over, then I just set the e.Handled = true and the keypress is halted. This scheme fails me though on an incoming ctrl-v paste event. It registers as a single keychar coming in. So the paste won't come through if I'm right at the limit, but it will go over if there is one character of room left and the paste has two or more characters for instance.

I just need a way to detect that this paste is coming in, and when its coming in I need to know what the length of the string is so I can either stop it from happening or paste as much of it as possible in there.

Thanks!

Upvotes: 0

Views: 793

Answers (1)

Isaac Bolinger
Isaac Bolinger

Reputation: 7388

This is such an idle question. You can just set the MaxLength property of the textbox and it prevents all of this. Duh.

Upvotes: 3

Related Questions