我有一个包含“hello”,“goodbye”和“ok”字样的数组。在 VB.NET 中,每次输入其中一个单词时,如何编写一个生成消息框的程序,没有单击一个按钮?
我做了一些研究,我遇到了keypress
事件 - 然而,这不合适,因为我的程序效率会非常低。
Visual Basic中是否有一种方法可以检测某些单词(在这种情况下,在数组中),而不仅仅是keypress
'?
答案 0 :(得分:1)
这是你如何做到的,但它带来了更多的问题......
Public Class Form1
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim words As String() = TextBox1.Text.Split(" "c)
Dim detectWords As New List(Of String) From {"hello", "goodbye", "ok"}
For Each word As String In words
If detectWords.Contains(word.ToLower) Then
MsgBox(word)
End If
Next
End Sub
End Class
使用按键事件,您可以查找回车键,然后处理它,而不是每次文本更改...
Public Class Form1
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
Dim words As String() = TextBox1.Text.Split(" "c)
Dim detectWords As New List(Of String) From {"hello", "goodbye", "ok"}
For Each word As String In words
If detectWords.Contains(word.ToLower) Then
MsgBox(word)
End If
Next
End If
End Sub
End Class