Numeric Textbox 2 Decimal places
I am hoping this is not inappropriate to do on here if it is I apologize in advance and will let the thread die a slow agonizing death.Here goes:
I have a little TextBox app that's supposed to only allow numeric’s positive and negative and allow 2 decimal places. I have tested it and it seems to work as intended. What I'd like is for you all to try and break it. The max characters functionality needs some work but it's not the focus of my TextBox.
'K. W. 4/19/2012'TextBox Control For'Numerics/Money entry'1 Decimal Point'2 Decimal Places'Allows The BackSpace'Handles enter key by Moving to the Next control'Set Max characters checked - this sets it to 4 if not checked allows 10 characters'The button just multiplies the textbox value by 5Public Class Form1Private maxSize As Boolean = FalsePrivate numMaxSize As IntegerPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickIf T1.Text <> "" ThenIf CDbl(T1.Text) > 0 Or CDbl(T1.Text) < 0 ThenMsgBox(CDbl(T1.Text) * 5)T1.Clear()End IfEnd IfGetNextControl(Button1, False).Focus()End SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load'Add Handler for all the Numeric only TextBoxes on the FormFor Each ctrl As Control In Me.ControlsIf TypeOf ctrl Is TextBox ThenAddHandler ctrl.KeyPress, AddressOf BoxkeypressEnd IfNext ctrlT1.MaxLength = 10End SubPrivate Sub Boxkeypress(ByVal sender As Object, ByVal e As KeyPressEventArgs)Dim txt As TextBox = CType(sender, TextBox)If Not Char.IsDigit(e.KeyChar) Then e.Handled = TrueIf e.KeyChar = "." And txt.Text.IndexOf(".") = -1 Then e.Handled = False 'allow single decimal pointIf e.KeyChar = "-" And txt.SelectionStart = 0 Then e.Handled = False 'allow negative number'Enter key move to next controlIf e.KeyChar = Chr(13) ThenGetNextControl(txt, True).Focus()'If only a decimal point is in the box clear TextBoxIf e.KeyChar = Chr(13) And txt.Text = "." Then txt.Clear()Exit SubEnd IfDim i As Integer = txt.Text.IndexOf(".")Dim len As Integer = txt.Text.Length'Allow only 2 Decimal placesIf Not e.Handled ThenIf i = -1 Thene.Handled = FalseElseIf (len - i) > 2 Then e.Handled = TrueEnd IfEnd IfIf e.KeyChar = Chr(8) Then e.Handled = False 'allow BackspaceEnd SubPrivate Sub chkMaxSize_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkMaxSize.CheckedChangedIf chkMaxSize.Checked ThenmaxSize = TrueT1.MaxLength = 4ElsemaxSize = FalseT1.MaxLength = 10End IfEnd SubEnd Classsumber : http://www.daniweb.com/software-development/vbnet/threads/434461/numeric-textbox-2-decimal-places