Solved

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

Posted on 2011-02-21
6
685 Views
Last Modified: 2012-05-11
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!
0
Comment
Question by:Valleriani
6 Comments
 
LVL 10

Expert Comment

by:Jacco
ID: 34943086
You can set the property ShortcutsEnabled to false.

The way the context menu and the keyboard shortcuts are disabled.
0
 
LVL 10

Expert Comment

by:Jacco
ID: 34943094
Didn't read correctly, sorry
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 300 total points
ID: 34943168
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Assisted Solution

by:Jacco
Jacco earned 100 total points
ID: 34943324
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;
            try
            {
                var sb = new StringBuilder();
                foreach (var c in textBox1.Text)
                    if (c >= '0' && c <= '9')
                        sb.Append(c);
                if (textBox1.Text != sb.ToString())
                {
                    textBox1.Text = prevok;
                    textBox1.SelectionStart = prevstrt;
                }
                else
                {
                    prevstrt = textBox1.SelectionStart;
                    prevok = textBox1.Text;
                }
            }
            finally
            {
                inevent = false;
            }
        }

Open in new window

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 34944353
You can trap the WM_PASTE message and act based on the pending input:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_23143054.html#20841606
0
 
LVL 7

Author Closing Comment

by:Valleriani
ID: 34950671
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.

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now