Keydown einer anderen Form

  • VB.NET

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von cool_Jack.

    Keydown einer anderen Form

    Hallo!
    Ich habe ein Problem.
    Ich möchte das Keydown event einer Form zb.form1 aufrufen, sodass ich nicht alles in die andere Form zb. form2 übertragen muss, da das ganzschön viel ist. Nun habe ich es versucht mit: Form1.Keydown(sender,e) im keydownevent der form2. Da sagt mir VB ich solle das Raisevent nutzen. Nur was ist das?
    Gruß Chris
    Eventuell bringt dich folgendes Beispiel aus der VS-Hilfe auf Ideen:

    VB.NET-Quellcode

    1. Imports System.Windows.Forms
    2. Imports System.Security.Permissions
    3. Public Class Form1
    4. Inherits System.Windows.Forms.Form
    5. ' Declare the controls contained on the form.
    6. Private WithEvents button1 As MyMnemonicButton
    7. Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    8. Public Sub New()
    9. MyBase.New()
    10. ' Set KeyPreview object to true to allow the form to process
    11. ' the key before the control with focus processes it.
    12. Me.KeyPreview = True
    13. ' Add a MyMnemonicButton.
    14. button1 = New MyMnemonicButton
    15. button1.Text = "&Click"
    16. button1.Location = New System.Drawing.Point(100, 120)
    17. Me.Controls.Add(button1)
    18. ' Initialize a ListBox control and the form itself.
    19. Me.ListBox1 = New System.Windows.Forms.ListBox
    20. Me.SuspendLayout()
    21. Me.ListBox1.Location = New System.Drawing.Point(8, 8)
    22. Me.ListBox1.Name = "ListBox1"
    23. Me.ListBox1.Size = New System.Drawing.Size(120, 95)
    24. Me.ListBox1.TabIndex = 0
    25. Me.ListBox1.Text = "Press a key"
    26. Me.ClientSize = New System.Drawing.Size(292, 266)
    27. Me.Controls.Add(Me.ListBox1)
    28. Me.Name = "Form1"
    29. Me.Text = "Form1"
    30. Me.ResumeLayout(False)
    31. End Sub
    32. ' The form will handle all key events before the control with
    33. ' focus handles them. Show the keys pressed by adding the
    34. ' KeyCode object to ListBox1. Ensure the processing is passed
    35. ' to the control with focus by setting the KeyEventArg.Handled
    36. ' property to false.
    37. Private Sub Form1_KeyDown(ByVal sender As Object, _
    38. ByVal e As KeyEventArgs) Handles MyBase.KeyDown
    39. ListBox1.Items.Add(e.KeyCode)
    40. e.Handled = False
    41. End Sub
    42. <System.STAThreadAttribute()> Public Shared Sub Main()
    43. Application.Run(New Form1)
    44. End Sub
    45. End Class
    46. ' This button is a simple extension of the button class that overrides
    47. ' the ProcessMnemonic method. If the mnemonic is correctly entered,
    48. ' the message box will appear and the click event will be raised.
    49. Public Class MyMnemonicButton
    50. Inherits Button
    51. ' This method makes sure the control is selectable and the
    52. ' mneumonic is correct before displaying the message box
    53. ' and triggering the click event.
    54. <System.Security.Permissions.UIPermission( _
    55. System.Security.Permissions.SecurityAction.Demand, Window:=UIPermissionWindow.AllWindows)> _
    56. Protected Overrides Function ProcessMnemonic( _
    57. ByVal inputChar As Char) As Boolean
    58. If (CanSelect And IsMnemonic(inputChar, Me.Text)) Then
    59. MessageBox.Show("You've raised the click event " _
    60. & "using the mnemonic.")
    61. Me.PerformClick()
    62. Return True
    63. End If
    64. Return False
    65. End Function
    66. End Class


    Gruß,

    f0x
    Nein, ehrlich gesagt, sagt mir das garnichts.
    Was soll das überhaupt darstellen?
    EDIT (kein Doppelpost^^):

    INOPIAE schrieb:

    Was hälst Du davon den Code Deines KeyDown-Ereignises in ein seperate Klasse auszulagern und dann in den einzelnen Formularen einzubinden?


    Oh man, wieso fällt mir das nie ein? Nagut ich stehe unter Zeitdruck.
    Danke, so werde ich es machen.
    In form2:

    VB.NET-Quellcode

    1. Public Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2. Debug.Print(e.KeyValue.ToString)
    3. End Sub

    PUBLIC!! ist wichtig ...

    in form1_load:

    VB.NET-Quellcode

    1. AddHandler Me.KeyDown, AddressOf Form2.Form2_KeyDown

    nicht vergessen, keypreview auf true zu setzen ...
    versteh zwar gerade nicht worauf du hinauswillst, aber ...

    EINE Prozedure kann das Ziel von beliebig vielen Events sein.
    JEDE Form deren Event(s) woanders behandelt werden sollen muss natürlich so ein AddHandler statement haben.

    Der eigentliche Eventhandler braucht natürlich nicht mal in einer Form sein. Hauptsache die "Signatur" stimmt.
    Ich hab jetzt selber nochmal getüftelt.
    Man muss es nochnichmal so machen wie du oben.
    Es geht auch so:
    In die Form2_Shown:

    VB.NET-Quellcode

    1.   AddHandler Me.KeyDown, AddressOf Form1.Form1_KeyDown

    Dann Keypriv. auf True
    FERTIG - Dann funzt es. Auch bei einem Mdiparent, da wirkt das Keypriv. auf die einzelnen Childforms wie bei einer normalen Form die Controls.
    Danke!