2013-03-25 264 views
3
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 

我想讓字母a,b,c,d表示用戶輸入不敏感。試圖使用UCase,但它不起作用(不知道我是否使用它錯誤)。我在Visual Studio 2012中並使用VB。任何參考將是很好的。不區分大小寫

+1

看到這個:http://msdn.microsoft.com/en-us/library/system.string.compare(v=vs.80).aspx – David 2013-03-25 02:31:01

+0

謝謝,這是非常有用的 – Brandon 2013-03-25 12:46:07

回答

11

您可以使用String.Compare方法:String.Compare (String strA, String strB, Boolean ignoreCase)

ignoreCase參數與true將執行區分大小寫的比較。

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 

另一個想法是大寫或小寫使用ToUpperToLower輸入。

Option Compare Text 

如果上面的行添加到代碼的開頭:

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 
0

根據MSDN在VB.NET,你可以通過簡單地增加1行代碼文件使用Option Compare Statement您要讓CLR從默認(Option Compare Binary)切換到不區分大小寫的比較,作爲=運算符的新默認值。

我不知道是否有任何C#替代品。