0

My issue is that I am attempting to make the user only able to enter "a, b, c or d". I would rather have it error out if they did not enter one of those four letters than the user only being able to input those letters. I have only been able to find resources that do something similar to this with numeric data (using try catch). Any sites or hints would be great.

                If String.Compare(TextBox2.Text, "a", True) = 0 AndAlso String.Compare(TextBox21.Text, "a", True) = 0 Then
                'MessageBox.Show("A")
                totCorrect = totCorrect + corAns
            ElseIf String.Compare(TextBox2.Text, "b", True) = 0 AndAlso String.Compare(TextBox21.Text, "b", True) = 0 Then
                'MessageBox.Show("B")
                totCorrect = totCorrect + corAns
            ElseIf String.Compare(TextBox2.Text, "c", True) = 0 AndAlso String.Compare(TextBox21.Text, "c", True) = 0 Then
                'MessageBox.Show("C")
                totCorrect = totCorrect + corAns
            ElseIf String.Compare(TextBox2.Text, "d", True) = 0 AndAlso String.Compare(TextBox21.Text, "d", True) = 0 Then
                'MessageBox.Show("D")
                totCorrect = totCorrect + corAns
            Else
                totWrong = totWrong + wrgAns
                Label13.Visible = True
            End If
Brandon
  • 133
  • 1
  • 2
  • 11
  • Stupid question, but if you want the user to only select a, b, c or d, why not use a combobox instead of a textbox? Then you won't have to do all these checks... – yu_ominae Mar 27 '13 at 01:06

1 Answers1

1

This should do the trick

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim allowableChar As New List(Of Char) From {"a"c, "b"c, "c"c, "d"c}

    Call allowableChar.AddRange(allowableChar.Select(Function(c) Convert.ToChar(c.ToString().ToUpper())).ToList())

    If Not (allowableChar.Contains(e.KeyChar) OrElse e.KeyChar = Convert.ToChar(Keys.Delete) OrElse e.KeyChar = Convert.ToChar(Keys.Back)) Then
        e.Handled = True
    End If

End Sub

The Call allowableChar.AddRange(...) adds the upper case characters to the list as well. As it is now, this generates a new list every time the method executes, which is a bit wasteful... if you use this piece of code I would suggest that you the list of allowable chars a class-level variable and fill it only once in the constructor of your form.

To allow only once character of each type, use this:

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim allowableChar As New List(Of Char) From {"a"c, "b"c, "c"c, "d"c}

    Call allowableChar.AddRange(allowableChar.Select(Function(c) Convert.ToChar(c.ToString().ToUpper())).ToList())

    If Not (allowableChar.Contains(e.KeyChar) OrElse e.KeyChar = Convert.ToChar(Keys.Delete) OrElse e.KeyChar = Convert.ToChar(Keys.Back)) Then
        e.Handled = True
    Else
        If Me.TextBox1.Text.Count(Function(c) c = e.KeyChar) >= 1 Then
            e.Handled = True
        End If
    End If

End Sub
yu_ominae
  • 2,975
  • 6
  • 39
  • 76
  • That will work...I might be stretching what code in general can do but, do you think it is possible for it to only allow 1 "a" or 1 "b" etc? – Brandon Mar 27 '13 at 01:21
  • Sure, I changed the code to include that, although if you actually want to do that you could use a set of checkboxes instead of a textbox. Unless the order in which the letters are entered is important of course. – yu_ominae Mar 27 '13 at 01:26