Link to home
Start Free TrialLog in
Avatar of Steve Williams
Steve WilliamsFlag for United States of America

asked on

Numeric with Decimal Only Values Allowed in Some Textboxes on Form

I'm Currently using Visual Studio Community 2015, Visual Basic.

I created a Form that has Multiple Textboxes. I decided on a SubRoutine that only allows numeric values with a decimal in the textbox. If I add this to a keypress event, it works like a charm. But I do not want to have to duplicate this code for each textbox that I want to control.  And I do not want this control invoked on all my text boxes just the ones that require it. Here is my Subroutine:

Private Sub txtTextbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTextBox1.KeyPress
    Dim FullStop As Char
    FullStop = "."

    ' if the '.' key was pressed see if there already is a '.' in the string
    ' if so, dont handle the keypress
    If e.KeyChar = FullStop And txtTextBox1.Text.IndexOf(FullStop) <> -1 Then
        e.Handled = True
        Return
    End If

    ' If the key aint a digit
    If Not Char.IsDigit(e.KeyChar) Then
        ' verify whether special keys were pressed
        ' (i.e. all allowed non digit keys - in this example
        ' only space and the '.' are validated)
        If (e.KeyChar <> FullStop) And
           (e.KeyChar <> Convert.ToChar(Keys.Back)) Then
            ' if its a non-allowed key, dont handle the keypress
            e.Handled = True
            Return
        End If
    End If
End Sub

Open in new window


I was thinking of maybe creating a function from this code and then calling out the function for each textbox_keypress event that requires it.

Any help would be greatly appreciated!
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Steve;

Just add a handles for each text box event you want this code to handle. as shown below. All the TextBox that appear in the comma separated list will be handled by this event.
Private Sub txtTextbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTextBox1.KeyPress, txtTextBox2.KeyPress, ...

Open in new window

Avatar of Steve Williams

ASKER

Trideep Patel, Thanks for the answer that quick and easy!
I know the accepted solution is quick and easy but this is definitely not the way to go. Your code will end up being a cabbage patch garden soon. Build it the better way and save yourself a lot of headache in the future. Having confidence that you can modify your code in the future without breaking something else is more valuable. Going forward you will not be able to change the names of those textboxes. This is just not something you want.
Dear Richard Lee,

I think you never have used this approach.
Textbox name change will not at all be a problem as it will automatically be changed in the code.
I know bad code when I see it and while this is quick its a hack.