-We should only be able to enter ISBN's in this format, 0-1234567-8-9 or 0-1234567-8-X.
-What I have now automatically inserts hyphens at a certain text length but doesn't let me delete the hyphens that have been added (besides the last one)
-So how would I be to delete the hyphens? I think I have to delete two characters (one character being a hyphen) at once in order to remove the hyphen.
-Also, how do I allow a number or an "x" to be added to the 13 position in the string?

Private Sub txtISBN_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtISBN.KeyPress
        If txtISBN.TextLength = 1 Then
        txtISBN.Text = txtISBN.Text + "-"
        txtISBN.SelectionStart = Len(txtISBN.Text)
    Else
        If txtISBN.TextLength = 9 Then
        txtISBN.Text = txtISBN.Text + "-"
        txtISBN.SelectionStart = Len(txtISBN.Text)
    Else
        If txtISBN.TextLength = 11 Then
        txtISBN.Text = txtISBN.Text + "-"
        txtISBN.SelectionStart = Len(txtISBN.Text)
    End If
    End If
    End If
End Sub

Edited 4 Years Ago by veeeeebeeeee: typo

I don't get it. You add "-" to text and then you want to delete it?
Anyways, you could try using String's replace function.

Sorry, I meant to say that I want the user to be able to backspace if they enter a wrong number by mistake.

I suggest that you use the MaskedTextBox control with an initial mask of "#-#######-#-#".

Then in it's KeyPress Event handler:

Private Sub MaskedTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MaskedTextBox1.KeyPress
   If MaskedTextBox1.SelectionStart = 12 Then
     If Char.IsLetter(e.KeyChar) Then
        e.KeyChar = Char.ToUpper(e.KeyChar) ' make  it uppercase
        ' set mask to allow a character
        MaskedTextBox1.Mask = "#-#######-#-A"
        ' Only allow "X"
        If Char.ToUpper(e.KeyChar) <> "X"c Then
           e.KeyChar = "."c 'set to invalid character
        End If
     Else
        MaskedTextBox1.Mask = "#-#######-#-#"
     End If

   End If
End Sub
This article has been dead for over six months. Start a new discussion instead.