Wenn du Shortcuts für die Menuitems definiert hast, musst du prüfen ob die Routine über Contextmenu aufgerufen wurde, wenn nicht muss es um das aktive Control handeln, da nur dieses Control die Tastatur-Eingabe bekommt. Es geht zB so
Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles CutToolStripMenuItem.Click
If ContextMenuStrip1.SourceControl IsNot Nothing Then
CType(ContextMenuStrip1.SourceControl, TextBox).Cut()
Else
CType(Me.ActiveControl, TextBox).Cut()
End If
End Sub (um ContextMenu aufzurufen muss das Control nicht unbedingt den Fokus haben).
Wenn du viele unterschiedliche Controls hast kannst du eine solche Routine programmieren
Private Function GetControl(ByVal cm As ContextMenuStrip, ByVal ct As Type) _
As Control
Dim ctl As Control = cm.SourceControl
If cm.SourceControl Is Nothing Then
ctl = Me.ActiveControl
End If
If ctl Is Nothing Then Return Nothing
If ctl.GetType Is ct Then Return ctl
Return Nothing
End Function
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
Dim tb As TextBox = CType(GetControl(ContextMenuStrip1, GetType( _
TextBox)), TextBox)
If tb IsNot Nothing Then tb.paste()
End Sub Oder noch ein wenig raffinierter mit generischer Funktion:
Private Function GetControl(Of T As Control)(ByVal cm As ContextMenuStrip) _
As T
Dim ctl As Control = cm.SourceControl
If cm.SourceControl Is Nothing Then
ctl = Me.ActiveControl
End If
If ctl Is Nothing Then Return Nothing
If TypeOf ctl Is T Then Return CType(ctl, T)
Return Nothing
End Function
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
Dim tb As TextBox = GetControl(Of TextBox)(ContextMenuStrip1)
If tb IsNot Nothing Then tb.paste()
End Sub Zum Teil Geschmackssache, denke ich. Allerdings wenn du einfach "Ctrl-X" als Text angibst für das MenuItem behandelt TextBox ganz alleine diese Shortcuts, und du musst gar keinen zusätzlichen Code haben.
________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |