Eliminating multiple "." or "-" in textbox

DiverDan

Well-known member
Joined
Jan 16, 2003
Messages
645
Location
Sacramento, CA
User Rank
*Experts*
Im trying to eliminate multiple instances of dot and minus in a textbox. My code is obviously not working but is as follows:

Code:
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:
Code:
        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
Dont 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:

Code:
    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:
Code:
    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,

Its 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:

Code:
    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:
Originally posted by DiverDan
Opps, this still does not stop multiple instances of dot and minus. I implemented your suggestion as follows:

Code:
    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
Youre 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