VB - 如何检测正在输入的特定单词?
VB - how to detect certain words being typed?
我有一个包含单词 "hello"、"goodbye" 和 "ok" 的数组。在 VB.NET 中,我如何编写一个程序,在每次输入其中一个单词时生成一个消息框,WITHOUT a按钮被点击?
我做了一些研究,发现了 keypress
事件 - 但是,这不合适,因为我的程序会变得非常低效。
Visual Basic 中是否有一种方法可以检测要检测的特定单词(在本例中为数组中的单词),而不仅仅是 keypress
'?
你是这样做的,但它带来了更多的问题...
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
我有一个包含单词 "hello"、"goodbye" 和 "ok" 的数组。在 VB.NET 中,我如何编写一个程序,在每次输入其中一个单词时生成一个消息框,WITHOUT a按钮被点击?
我做了一些研究,发现了 keypress
事件 - 但是,这不合适,因为我的程序会变得非常低效。
Visual Basic 中是否有一种方法可以检测要检测的特定单词(在本例中为数组中的单词),而不仅仅是 keypress
'?
你是这样做的,但它带来了更多的问题...
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