创建一个事件。当某个字符串发生变化时应该发生此事件

Create an event. This event should occur when a certain string has changed

我回到很远的地方来弄清楚上下文。首先,我今天注意到,如果窗体上有多个控件,则不能使用箭头键,因为控件(例如组合框、文本框)会占用光标和焦点。因此,我创建了第二个表格(“SteuerForm”(转向表格))。对此没有控制。我还发现您只能通过 KeyDown 事件获得箭头键。 KeyDown 事件在涉及某些字符时是不利的:输出“Shiftkey”和“Oem7”而不是特定字母(例如 Ä)。这就是为什么我实际上想使用 KeyPress。最终,我写了一个class(Class_Key")。它包含一个 list(Of string) 和我必须拥有的某些键名。

现在我希望在字符串 letzte_Taste (last_key) 更改时调用 Form1 中的 Private Sub new_message()

编辑: 我想要 Class_Key Class 中的某些事件来触发主窗体中的方法 (new_message)。我怎样才能做到这一点?当字符串 letzte_Taste 从一个(例如 "Up")更改为另一个(例如 "Down")时,应该会触发该事件。

SteuerForm.vb

Public NotInheritable Class SteuerForm
    Private Sub SteuerForm_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
        If Class_Key.Tasten.Contains(e.KeyChar.ToString) Then
            Class_Key.letzte_Taste = e.KeyChar.ToString
        End If
    End Sub

    Private Sub SteuerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        Me.Location = New Point(500, 0)
    End Sub

    Private Sub SteuerForm_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If Class_Key.Tasten.Contains(e.KeyCode.ToString) Then
            Class_Key.letzte_Taste = e.KeyCode.ToString
        End If
    End Sub
End Class

Class_Key.vb

Public NotInheritable Class Class_Key
    Public Shared Tasten As New List(Of String) From {
        "Up",
        "Down",
        "Left",
        "Right",
        "Return",
        "Back",
        "Space",
        "Add",
        "Subtract",
        "a"} 'and another alphabetic characters... (I shortened this...)

    Public Shared letzte_Taste As String = ""
End Class

我做到了!这是我的解决方案。

Public NotInheritable Class Class_Key
    Public Shared Tasten As New List(Of String) From {
        "Up",
        "Down",
        "Left",
        "Right",
        "Return",
        "Back",
        "Space",
        "Add",
        "Subtract",
        "a"}

    Public Shared Property letzte_Taste As String = ""
    Public Shared Property vorletzte_Taste As String = ""
    Public Shared Event AnEvent()
    Public Shared Zaehler As UInt16 = 0
    Public Shared Sub Pruefung()
        If Not letzte_Taste = vorletzte_Taste Then
            RaiseEvent AnEvent()
            Zaehler += Convert.ToUInt16(1)
        End If
    End Sub
End Class
Public NotInheritable Class SteuerForm
    Private Sub SteuerForm_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
        If Class_Key.Tasten.Contains(e.KeyChar.ToString) Then
            Class_Key.letzte_Taste = e.KeyChar.ToString
        End If
        Class_Key.Pruefung()
        Class_Key.vorletzte_Taste = Class_Key.letzte_Taste
    End Sub

    Private Sub SteuerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        Me.Location = New Point(500, 0)
    End Sub

    Private Sub SteuerForm_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If Class_Key.Tasten.Contains(e.KeyCode.ToString) Then
            Class_Key.letzte_Taste = e.KeyCode.ToString
        End If
        Class_Key.Pruefung()
        Class_Key.vorletzte_Taste = Class_Key.letzte_Taste
    End Sub
End Class

在Form1_Load

AddHandler Class_Key.AnEvent, AddressOf neue_Nachricht

Form1

Private Sub neue_Nachricht()
    Debug.WriteLine($"caught event. Nr. {Class_Key.Zaehler}")
End Sub