The case the red code is in, is for the "." being pressed. For that case you should be limiting the user to a single ".". What you should do there is test that a "." doesn't already exist in the textbox because a number can't have more then one ".". You can do this easily using the contains method. Something like
If TextBox1.Text.Contains(".") then
e.handled = true
beep
end if
To limit the number of characters after the decimal is a little tricky. This is because a user editing the textbox isn't necessarily making a changes from left to right. They may go back to the 2nd position (which may be before the decimal) to enter a number they missed. One way to handle this would be to create the final string and then make sure you don't have to many digits after the decimal. Try adding this code in case "0" to "9".
Const MAX As Integer = 2
'Create the Final string if we allow this character
Dim temp As String = TextBox1.Text.Insert(TextBox1.SelectionStart, e.KeyChar)
'Where is the "."
Dim DecLoc As Integer = temp.IndexOf(".")
If DecLoc >= 0 AndAlso temp.Length - DecLoc - 1 > MAX Then
'To many digits after the decimal place...Don't allow digit
e.Handled = True
Beep()
End If
You'll have to change a few variable names but that code should work for you.
Enter your message below
Sign in or Join us (it's free).