title: TextBox Validation in VB.NET and Windows Forms
body: <p>I'm using the following code to validate the text entered by user. It works perfectly fine. But I want to add the backspace feature so as to allow the user to delete the wrongly entered number.</p>
  
  <p>I have tried a couple of things and they worked but before last digit (after the decimal point) i.e. it does not allows to delete after the number has been completely entered.
  number is being entered in the format: 12313213.45</p>
  
  <p>What shall I do?</p>
  
  <pre><code>Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
  
      'validation '
      Dim KeyAscii As Short = Asc(e.KeyChar)
      If Not ((KeyAscii &gt;= System.Windows.Forms.Keys.D0 And KeyAscii &lt;= System.Windows.Forms.Keys.D9) Or (KeyAscii = System.Windows.Forms.Keys.Back) Or Chr(KeyAscii) = "." Or (Chr(KeyAscii) Like "[ ]")) Then
          KeyAscii = 0
          TextBox5.Focus()
      End If
      If KeyAscii = 0 Then
          e.Handled = True
      End If
  
      If TextBox5.Text.IndexOf(".") &gt;= 0 And e.KeyChar = "." Then
          e.Handled = True
      End If
  
      If TextBox5.Text.IndexOf(".") &gt; 0 Then
          If TextBox5.SelectionStart &gt; TextBox5.Text.IndexOf(".") Then
              If TextBox5.Text.Length - TextBox5.Text.IndexOf(".") = 3 Then
                  e.Handled = True
              End If
          End If
      End If
  End Sub
  </code></pre>