Jump to content
Xtreme .Net Talk

Eliminating multiple "." or "-" in textbox


Recommended Posts

  • *Experts*
Posted (edited)

I'm trying to eliminate multiple instances of dot and minus in a textbox. My code is obviously not working but is as follows:

 

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

Edited by divil

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • *Experts*
Posted

Try it like this:

        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.

  • *Experts*
Posted (edited)

Opps, this still does not stop multiple instances of dot and minus. I implemented your suggestion as follows:

 

   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

Edited by divil

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

   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

Code today gone tomorrow!
  • *Experts*
Posted (edited)

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:

 

   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

Edited by divil

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • *Experts*
Posted
Opps, this still does not stop multiple instances of dot and minus. I implemented your suggestion as follows:

 

   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'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...