*Experts* DiverDan Posted February 19, 2003 *Experts* Posted February 19, 2003 (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 February 19, 2003 by divil Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* Volte Posted February 19, 2003 *Experts* Posted February 19, 2003 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 IfDon't forget to check for the characters when you paste as well. Quote
*Experts* DiverDan Posted February 19, 2003 Author *Experts* Posted February 19, 2003 Thanks VolteFace, I've got the paste validation coverd, this was the final touch. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* DiverDan Posted February 19, 2003 Author *Experts* Posted February 19, 2003 (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 February 19, 2003 by divil Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
a_jam_sandwich Posted February 19, 2003 Posted February 19, 2003 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 Quote Code today gone tomorrow!
*Experts* DiverDan Posted February 19, 2003 Author *Experts* Posted February 19, 2003 (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 February 20, 2003 by divil Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
*Experts* Volte Posted February 19, 2003 *Experts* Posted February 19, 2003 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'. Quote
*Experts* DiverDan Posted February 19, 2003 Author *Experts* Posted February 19, 2003 You're right. Thanks VolteFace Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.