2014-03-03 81 views
0

我想從文本框中捕獲一個字符串,並根據在該字符串中找到的每個字符運行條件。下面的代碼是我在想什麼,但它dosnt工作?遍歷文本字符

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Dim s As String = TextBox1.Text 

     For Each c As Char In s 
      If c = "A" Then 
       MsgBox("Letter is A") 
      ElseIf c = "B" Then 
       MsgBox("Letter is A") 
      ElseIf c = "C" Then 
       MsgBox("Letter is C") 

      End If 
     Next 
end sub 
+0

它以什麼方式不起作用? – har07

+0

如果你可以在'TextBox1'被聚焦時擊中'A,B或C'鍵,然後按下'Button1',這將起作用。 –

+1

Doh!你的權利它的工作Stuipid離開msgbox字母B表明一個我的錯誤.... – Argon

回答

0

使用chararry拆分它,你的文本框probbly capteruring某些Unicode

Dim charArray() As Char = TextBox1.Text.ToCharArray 

也看MSDN頁面有着深刻的見解msdn

0

試試這個:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim stringword() As Char 
    stringword = TextBox1.Text.ToCharArray 
    For i = 0 To stringword.Count - 1 
     Select Case stringword(i) 
      Case "a", "A" 
       MsgBox("Letter is A") 
      Case "B", "b" 
       MsgBox("Letter is b") 
      Case "C", "c" 
       MsgBox("Letter is c") 

     End Select 
    Next 
End Sub