Eliminating multiple "." or "-" in textbox

DiverDan

Contributor
Joined
Jan 16, 2003
Messages
645
Location
Sacramento, CA
I'm trying to eliminate multiple instances of dot and minus in a textbox. My code is obviously not working but is as follows:

Visual Basic:
Private Sub tbTemperature_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbTemperature.KeyPress
        Dim i, charsInBox As Short
        Dim charactor As String
        charsInBox = Len(tbTemperature.Text)
       If Not AscW(e.KeyChar) <= 47 And Not AscW(e.KeyChar) >= 58 Or AscW(e.KeyChar) = 8 Or AscW(e.KeyChar) = 46 Or Asc(e.KeyChar) = 45 Then
                    For i = 0 To charsInBox - 1
                charactor = tbTemperature.Text.Substring(i, 1)
                If charactor = "." Then e.Handled = True
                If charactor = "-" Then e.Handled = True
            Next
           e.Handled = False
            Else
        e.Handled = True
        End If
    End Sub

How can I eliminate multiple instances of dot and minus?

Thanks
 
Last edited by a moderator:
Try it like this:
Visual Basic:
        Dim key As Char = e.KeyChar

        If key = "." Or key = "-" Then
            If TextBox1.Text.IndexOf(key) >= 0 Then
                e.Handled = True
            End If
        End If
Don't forget to check for the characters when you paste as well.
 
Opps, this still does not stop multiple instances of dot and minus. I implemented your suggestion as follows:

Visual Basic:
    Private Sub tbTemperature_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbTemperature.KeyPress
        Dim key As Char = e.KeyChar
        If Not AscW(key) <= 47 And Not AscW(key) >= 58 Or AscW(key) = 8 Or AscW(key) = 46 Or Asc(key) = 45 Then
            If key = "." Or key = "-" Then
                If tbTemperature.Text.IndexOf(key) >= 0 Then
                    e.Handled = True
                End If
            End If
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
#End Region

Multiple dots and minuses still occur
 
Last edited by a moderator:
Visual Basic:
    Private Sub tbTemperture_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbTemperature.KeyPress
        If Not AscW(e.KeyChar) <= 47 And Not AscW(e.KeyChar) >= 58 _
            Or AscW(e.KeyChar) = 8 Or AscW(e.KeyChar) = 46 Or Asc(e.KeyChar) = 45 Then
            If e.KeyChar = "." Then
                If TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False Else e.Handled = True
            ElseIf e.KeyChar = "-" Then
                If TextBox1.Text.IndexOf("-") = -1 Then e.Handled = False Else e.Handled = True
            End If
        Else
            e.Handled = False
        End If
    End Sub

Give that a try

Andy
 
Thanks Andy,

It's getting closer by perventing multiple instances of dot and minus, but it does allow letters and not just numbers to be input. But I used your ideas and everything works well now. The modifided code is:

Visual Basic:
    Private Sub tbTemperture_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbTemperature.KeyPress
        If Not AscW(e.KeyChar) <= 47 And Not AscW(e.KeyChar) >= 58 _
            Or AscW(e.KeyChar) = 8 Or AscW(e.KeyChar) = 46 Or Asc(e.KeyChar) = 45 Then
            If e.KeyChar = "." Then
                If tbTemperature.Text.IndexOf(".") = -1 Then
                    e.Handled = False
                Else : e.Handled = True
                    Exit Sub
                End If
            ElseIf e.KeyChar = "-" Then
                If tbTemperature.Text.IndexOf("-") = -1 Then
                    e.Handled = False
                Else : e.Handled = True
                    Exit Sub
                End If
            End If
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

This prevents multiple instances of dot and minus and allows only numbers into the textbox field.

Once again, thanks to Andy and VolteFace
 
Last edited by a moderator:
DiverDan said:
Opps, this still does not stop multiple instances of dot and minus. I implemented your suggestion as follows:

Visual Basic:
    Private Sub tbTemperature_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbTemperature.KeyPress
        Dim key As Char = e.KeyChar
        If Not AscW(key) <= 47 And Not AscW(key) >= 58 Or AscW(key) = 8 Or AscW(key) = 46 Or Asc(key) = 45 Then
            If key = "." Or key = "-" Then
                If tbTemperature.Text.IndexOf(key) >= 0 Then
                    e.Handled = True
                End If
            End If
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
#End Region

Multiple dots and minuses still occur
You're setting e.Handled = True inside the code that I gave you, and then
setting it right back to false afterwards. Take out the 'e.Handled = False'
line before the 'Else'.
 
Back
Top