Format TextBox only Numerical Data and 2 decimal places, enable backspace

  • Thread starter Thread starter superlative
  • Start date Start date
S

superlative

Guest
Hi,

I have managed to format a text box to be formatted to two decimal places, and to only accept numbers.

Only problem is i need to allow the backspace so that if someone types something wrong, they can backspace and type the correct number




Private Sub txtCAmount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCAmount.KeyPress

'Allow only numberical input plus decimal.
Dim allowedChars As String = "0123456789."
If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If

End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCAmount.Leave

'Format to 2 decimal places.
txtCAmount.Text = Format(Double.Parse(txtCAmount.Text), "###,###,##0.00")

End Sub



Can someone tell me how I could modify above to allow the backspace?

Thanks.

Continue reading...
 
Back
Top