Case insensitive

17,417

Solution 1

You can use String.Compare method : String.Compare (String strA, String strB, Boolean ignoreCase)

Pass ignoreCase argument with true will perform case insensitive comparison.

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

Another idea is to uppercase or lowercase the input using ToUpper or ToLower.

If TextBox2.Text.ToUpper() = "A" AndAlso TextBox21.Text.ToUpper() = "A" Then
            'MessageBox.Show("A")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "B" AndAlso TextBox21.Text.ToUpper() = "B" Then
            'MessageBox.Show("B")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "C" AndAlso TextBox21.Text.ToUpper() = "C" Then
            'MessageBox.Show("C")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text.ToUpper() = "D" AndAlso TextBox21.Text.ToUpper() = "D" Then
            'MessageBox.Show("D")
            totCorrect = totCorrect + corAns
        Else
            totWrong = totWrong + wrgAns
            Label13.Visible = True
        End If

Solution 2

According to MSDN in VB.NET, you can use the Option Compare Statement by simply adding 1 line of code to your file:

Option Compare Text

If you add the above line to the beginning of your code you are telling the CLR to switch from default (Option Compare Binary) to the case-insensitive comparison as the new default for the = operator.

I don't know if there is any C# alternative.

Share:
17,417
Brandon
Author by

Brandon

Updated on September 16, 2022

Comments

  • Brandon
    Brandon over 1 year
    If TextBox2.Text = "a" AndAlso TextBox21.Text = "a" Then
            'MessageBox.Show("A")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text = "b" AndAlso TextBox21.Text = "b" Then
            'MessageBox.Show("B")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text = "c" AndAlso TextBox21.Text = "c" Then
            'MessageBox.Show("C")
            totCorrect = totCorrect + corAns
        ElseIf TextBox2.Text = "d" AndAlso TextBox21.Text = "d" Then
            'MessageBox.Show("D")
            totCorrect = totCorrect + corAns
        Else
            totWrong = totWrong + wrgAns
            Label13.Visible = True
        End If
    

    I am trying to make the letters a,b,c,d that the user enters insensitive. Tried to use the UCase, but it did not work (not sure if I am using it wrong). I am in Visual Studio 2012 and using VB. Any references would be great.