Datagridview Keypress Event

  • VB.NET

Es gibt 14 Antworten in diesem Thema. Der letzte Beitrag () ist von Zim.

    Datagridview Keypress Event

    Hey ho
    ich bins schooon wieder

    Wie fragt man den Keypress event von einem Datagridview ab ?

    VB.NET-Quellcode

    1. Private Sub Datagrid_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DGV_Kali.KeyPress
    2. End Sub


    Reagiert nicht auf Tastendruck 8|
    Du rufst das Ereignis Datagridview1.Keydown auf und schreibst dort folgenden Code rein:

    VB.NET-Quellcode

    1. if e.keycode = keys.enter then
    2. msgbox ("Es wurde Enter gedrückt.")
    3. end if

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Jannik Schüttler“ ()

    Offensichtlich wirft nur Enter solch ein Event. :S
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Sooo ich hab mal ein wenig mit Tastatur Hooks experementiert. Geht soweit auch ganz gut.
    Aber ich kann nicht abfragen ob ich bereits ein Komma oder Punkt geschrieben hab da kommt dann immer "NullReferenceExeption..."
    Wie kann ich des denn am besten testen ohne mir merken zu müssen ob ich die Tasten schon gedrückt habe.


    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Function KeyboardHookProc(ByVal nCode As Integer, _
    2. ByVal wParam As IntPtr, _
    3. ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
    4. Dim fEatKeyStroke As Boolean
    5. If nCode = HC_ACTION Then
    6. Select Case lParam.vkCode
    7. ' Hier z.B. die Zeichen a und b nicht zulassen
    8. '(fEatKeyStroke = True)
    9. Case Keys.D0
    10. fEatKeyStroke = False
    11. Case Keys.D1
    12. fEatKeyStroke = False
    13. Case Keys.D2
    14. fEatKeyStroke = False
    15. Case Keys.D3
    16. fEatKeyStroke = False
    17. Case Keys.D4
    18. fEatKeyStroke = False
    19. Case Keys.D5
    20. fEatKeyStroke = False
    21. Case Keys.D6
    22. fEatKeyStroke = False
    23. Case Keys.D7
    24. fEatKeyStroke = False
    25. Case Keys.D8
    26. fEatKeyStroke = False
    27. Case Keys.D9
    28. fEatKeyStroke = False
    29. Case Keys.NumPad0
    30. fEatKeyStroke = False
    31. Case Keys.NumPad1
    32. fEatKeyStroke = False
    33. Case Keys.NumPad2
    34. fEatKeyStroke = False
    35. Case Keys.NumPad3
    36. fEatKeyStroke = False
    37. Case Keys.NumPad4
    38. fEatKeyStroke = False
    39. Case Keys.NumPad5
    40. fEatKeyStroke = False
    41. Case Keys.NumPad6
    42. fEatKeyStroke = False
    43. Case Keys.NumPad7
    44. fEatKeyStroke = False
    45. Case Keys.NumPad8
    46. fEatKeyStroke = False
    47. Case Keys.NumPad9
    48. fEatKeyStroke = False
    49. Case Keys.OemPeriod
    50. IfDGV_Kali.CurrentCell.Value.ToString.IndexOf(".") = -1 Then
    51. fEatKeyStroke = False
    52. Else
    53. fEatKeyStroke = True
    54. End If
    55. Case Keys.Oemcomma
    56. If DGV_Kali.CurrentCell.Value.ToString.IndexOf(",") = -1 Then
    57. fEatKeyStroke = False
    58. Else
    59. fEatKeyStroke = True
    60. End If
    61. Case Keys.Back
    62. fEatKeyStroke = False
    63. Case Keys.Delete
    64. fEatKeyStroke = False
    65. Case Else
    66. fEatKeyStroke = True
    67. End Select
    68. If fEatKeyStroke Then
    69. Return New IntPtr(1)
    70. Exit Function
    71. End If
    72. Return CallNextHookEx(mHandle, nCode, wParam, lParam)
    73. End If
    74. End Function
    Habs gelöst ;)

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Function KeyboardHookProc(ByVal nCode As Integer, _
    2. ByVal wParam As IntPtr, _
    3. ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
    4. Dim fEatKeyStroke As Boolean
    5. If nCode = HC_ACTION Then
    6. Select Case lParam.vkCode
    7. ' Hier z.B. die Zeichen a und b nicht zulassen
    8. '(fEatKeyStroke = True)
    9. Case Keys.D0
    10. fEatKeyStroke = False
    11. Case Keys.D1
    12. fEatKeyStroke = False
    13. Case Keys.D2
    14. fEatKeyStroke = False
    15. Case Keys.D3
    16. fEatKeyStroke = False
    17. Case Keys.D4
    18. fEatKeyStroke = False
    19. Case Keys.D5
    20. fEatKeyStroke = False
    21. Case Keys.D6
    22. fEatKeyStroke = False
    23. Case Keys.D7
    24. fEatKeyStroke = False
    25. Case Keys.D8
    26. fEatKeyStroke = False
    27. Case Keys.D9
    28. fEatKeyStroke = False
    29. Case Keys.NumPad0
    30. fEatKeyStroke = False
    31. Case Keys.NumPad1
    32. fEatKeyStroke = False
    33. Case Keys.NumPad2
    34. fEatKeyStroke = False
    35. Case Keys.NumPad3
    36. fEatKeyStroke = False
    37. Case Keys.NumPad4
    38. fEatKeyStroke = False
    39. Case Keys.NumPad5
    40. fEatKeyStroke = False
    41. Case Keys.NumPad6
    42. fEatKeyStroke = False
    43. Case Keys.NumPad7
    44. fEatKeyStroke = False
    45. Case Keys.NumPad8
    46. fEatKeyStroke = False
    47. Case Keys.NumPad9
    48. fEatKeyStroke = False
    49. Case Keys.OemPeriod
    50. If MainForm.DGV_Kali.CurrentCell.GetEditedFormattedValue(0, DataGridViewDataErrorContexts.ClipboardContent).ToString.IndexOf(",") = -1 And MainForm.DGV_Kali.CurrentCell.GetEditedFormattedValue(0, DataGridViewDataErrorContexts.ClipboardContent).ToString.IndexOf(".") = -1 Then
    51. fEatKeyStroke = False
    52. Else
    53. fEatKeyStroke = True
    54. End If
    55. Case Keys.Oemcomma
    56. If MainForm.DGV_Kali.CurrentCell.GetEditedFormattedValue(0, DataGridViewDataErrorContexts.ClipboardContent).ToString.IndexOf(",") = -1 And MainForm.DGV_Kali.CurrentCell.GetEditedFormattedValue(0, DataGridViewDataErrorContexts.ClipboardContent).ToString.IndexOf(".") = -1 Then
    57. fEatKeyStroke = False
    58. Else
    59. fEatKeyStroke = True
    60. End If
    61. Case Keys.Back
    62. fEatKeyStroke = False
    63. Case Keys.Delete
    64. fEatKeyStroke = False
    65. Case Keys.OemMinus
    66. If vb.left(MainForm.DGV_Kali.CurrentCell.GetEditedFormattedValue(0, DataGridViewDataErrorContexts.ClipboardContent).ToString, 1).IndexOf(",") = -1 Then
    67. fEatKeyStroke = False
    68. Else
    69. fEatKeyStroke = True
    70. End If
    71. Case Else
    72. fEatKeyStroke = True
    73. End Select
    74. If fEatKeyStroke Then
    75. Return New IntPtr(1)
    76. Exit Function
    77. End If
    78. Return CallNextHookEx(mHandle, nCode, wParam, lParam)
    79. End If
    80. End Function