VB.NET 2010 - A way to prevent certain characters from being copy pasted inside of a textbox?

I have a simple method to prevent keypresses:

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") Then
            e.Handled = True
            Exit Sub
        End If

Which goes in the keypress area. This will allow only 0 to 9 to be added.  However, it seems like you can copy paste other characters inside the text box. I know I can do things such as looping through each character each time the text is changed, but this seems like it would be a not a very efficient way of handling this issue. Pretty much the goal here is to always make sure the characters 0 to 9 are allowed, while still keeping functions like PASTING in tact without having it disabled completely.

This can go for a-Z, etc. Thanks!
Who is Participating?
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
you can handle the TextChanged event and check the Text property against a regular expression as I shown in http://www.emoreau.com/Entries/Articles/2003/03/Creating-your-own-Windows-Custom-Control.aspx
You can set the property ShortcutsEnabled to false.

The way the context menu and the keyboard shortcuts are disabled.
Didn't read correctly, sorry
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

JaccoConnect With a Mentor Commented:
Here is something working in C#.

All keys are exepted. And the cursorpos after last keypress is continuosly stored.

The new resulting string is accepted or rejected.

When rejected the last good entry is put back and the corresponding cursor position.

(Didn't test with selected parts and pasting illegal)
string prevok = "";
        int prevstrt = 0;
        bool inevent = false;

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            prevstrt = textBox1.SelectionStart;

        private void textBox1_TextChanged(object sender, EventArgs e)
            if (inevent) return;
            inevent = true;
                var sb = new StringBuilder();
                foreach (var c in textBox1.Text)
                    if (c >= '0' && c <= '9')
                if (textBox1.Text != sb.ToString())
                    textBox1.Text = prevok;
                    textBox1.SelectionStart = prevstrt;
                    prevstrt = textBox1.SelectionStart;
                    prevok = textBox1.Text;
                inevent = false;

Open in new window

Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You can trap the WM_PASTE message and act based on the pending input:
VallerianiAuthor Commented:
Thank you. I think the regex was the best option here so it can handle everything. I was able to kill off the coding around keypress, and just use a regEX in textchange to prevent everything, including pasting.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.