title: Allow certain characters in certain positions in text box (vb.net)
body: <p>For example I only the second character in each line to be an x, while the 3nd to 10th character must be a hex digit.
  At the moment I use a Select Case, then check the position of the caret (using textbox.selectionstart) and see if the key being pressed is a "legal" character.</p>
  
  <p>Is there a better way of doing this as it slows down on large amounts of text.</p>
  
  <p>This is the code I have at the moment:</p>
  
  <pre><code> Select Case TextBox1.SelectionStart
       Case TextBox1.GetFirstCharIndexOfCurrentLine + 1 
           If Not e.KeyChar = "x" Then
               e.Handled = True
           End If
       Case (TextBox1.GetFirstCharIndexOfCurrentLine + 2) To (TextBox1.GetFirstCharIndexOfCurrentLine + 9) 
           Dim allowedchars As String = "abcdefABCDEF0123456789" &amp; vbCrLf &amp; Chr(Keys.Back)
           If allowedchars.Contains(e.KeyChar) Then
               e.Handled = False
           Else
               e.Handled = True
           End If
   End Select
  </code></pre>