2014-03-03 83 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