Advertisement

08.23.2007 at 02:56AM PDT, ID: 22781757
[x]
Attachment Details

VB.net TextField - limit numbers but allow Ctrl+C, Ctrl+V and Ctrl+X

Asked by rwallacej in .NET Framework 2.0

Tags: ctrl

Hi
I've got many textfields which are limited to numbers being entered with the following in the KeyPress handler (got this code from internet)

            Dim KeyAscii As Short = Asc(e.KeyChar)
            Select Case KeyAscii
                Case System.Windows.Forms.Keys.Back  '<--- this is for  backspace
                Case 46 'System.Windows.Forms.Keys.Decimal '<--- this is for  point key
                    'no decimals already and not first decimal
                    If sender.Text.IndexOf(".") = -1 And sender.Text.Length > 0 Or sender.selectionstart = 0 Then
                        Exit Sub
                    Else
                        e.Handled = True
                        TestForm.IsChangesMade = True
                        Exit Sub
                    End If
                Case 48 To 57     '<--- this is for numbers
                    Exit Sub
                Case 45     '<-- for negatives, allow at first position and only 1 negative
                    If sender.Text.IndexOf("-") = -1 And sender.Text.Length > 0 Then
                        Exit Sub
                    Else
                        e.Handled = True
                        Exit Sub
                    End If
                Case Else 'cancel key entry
                    e.Handled = True
            End Select


This works fine. However it means that keyboard shortcuts don't work.

Help please as I'd like the cut, copy and paste still to work.

Thanks in advance.Start Free Trial
 
 
[+][-]08.23.2007 at 06:16AM PDT, ID: 19753996

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: .NET Framework 2.0
Tags: ctrl
Sign Up Now!
Solution Provided By: appari
Participating Experts: 1
Solution Grade: A
 
 
[+][-]08.23.2007 at 06:18AM PDT, ID: 19754011

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.23.2007 at 08:31AM PDT, ID: 19755256

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_1_20070628