enter key press

Welcome Forums General PowerShell Q&A enter key press

Viewing 2 reply threads
  • Author
    Posts
    • #187828
      Participant
      Topics: 13
      Replies: 21
      Points: 204
      Rank: Participant

      hi all,

      I've built a GUI with a few textbox and I thought I could use the Enter key for either of those, as in;

      $comboPCName_KeyPress = [System.Windows.Forms.KeyPressEventHandler]{
      #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
      if ($_.KeyChar -eq 13)
      {
      $buttonConnect.PerformClick()
      }
      }
      $textboxFilterAD_KeyPress = [System.Windows.Forms.KeyPressEventHandler]{
      #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
      if ($_.KeyChar -eq 13)
      {
      $buttonGetADGroupsList.PerformClick()
      }
      }
      But if the focus is on textboxFilterAD and I press Enter, the $comboPCName_KeyPress function is executed instead of $textboxFilterAD_KeyPress like I wish.
      Is there something I can do to have the correct function called when I press Enter, depending on the selected control?
      Thank you!
    • #187843
      Participant
      Topics: 10
      Replies: 1284
      Points: 1,084
      Helping Hand
      Rank: Community Hero

      This is more of a .NET forms question, not really Powershell, but my assumption is that the Enter (and Esc) button have a separate events happening for AcceptButton. You may be able to even make it work more 'as designed' if you use the onFocus() event to set the AcceptButton to the required control rather than using KeyPress to change it. If you look at the second link, they are setting the AcceptButton and not parsing KeyPress events, but regardless you should be searching for .NET forms forums to find your answer if the below doesn't work for you.

      https://stackoverflow.com/questions/487920/winforms-acceptbutton-not-working
      https://stackoverflow.com/questions/602876/changing-the-acceptbutton-depending-on-active-control

    • #187846
      Participant
      Topics: 13
      Replies: 21
      Points: 204
      Rank: Participant

      I was able to make it worth with what you gave me 🙂

      Many thanks!

Viewing 2 reply threads
  • You must be logged in to reply to this topic.