AcceptButton Doesn't work

Welcome Forums General PowerShell Q&A AcceptButton Doesn't work

  • This topic has 3 replies, 2 voices, and was last updated 4 months ago by
    Participant
    .
Viewing 3 reply threads
  • Author
    Posts
    • #179037
      Participant
      Topics: 2
      Replies: 4
      Points: 37
      Rank: Member

      Hello,

      I try to use the AcceptButton to close the window when the Enter Key is press but it not works.

      [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
      [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
      
      $windows2 = New-object System.Windows.Forms.Form
          $windows2.size = New-object System.Drawing.size(500,300) 
          $windows2.AcceptButton = $buttonOK2
          $windows2.PreviewKey = $true
          $windows2.text = "Test"
      
          $buttonOK2 = New-object System.Windows.Forms.Button
          $buttonOK2.Location = New-object System.Drawing.Size(60,55)
          $buttonOK2.size = New-object System.Drawing.Size(80,25)
          $buttonOK2.Add_Click({
              $windows2.Close()})
          $buttonOK2.Text = "Close"
      
          $windows2.Controls.Add($buttonOK2) 
      
          $userID_Textbox = New-object System.Windows.Forms.TextBox
          $userID_Textbox.Location = New-object System.Drawing.Size(55,15)
          $userID_Textbox.size = New-Object System.Drawing.Size(100,20)
          $userID_Textbox.Anchor = [System.Windows.Forms.AnchorStyles]::left -bor [System.Windows.Forms.AnchorStyles]::top
          $userID_Textbox.AutoSize = $false
          $windows2.controls.add($userID_Textbox)
      
      $windows2.ShowDialog()

      The Focus is automatically on the "Close" button but if I click on the textbox and press "Enter" nothing happen instead of close the window.

      Someone can help me ?

      Thank you.

    • #179259
      Participant
      Topics: 2
      Replies: 4
      Points: 37
      Rank: Member

      Hello,

       

      I found a how to do it.

      It missed $buttonOK2.Dialogresult = "Ok"

      I want to know now if it's possible to do it with multiples tabs.

      I have 4 tabs with one button on each tab, each buttons have a different action.

      It's possible to configure AcceptButton regarding the selectedtab and assigned to a different button ?

      Thank you.

    • #179415
      Senior Moderator
      Topics: 8
      Replies: 1155
      Points: 4,019
      Helping Hand
      Rank: Community Hero

      didn't get your question. Can you explain a little more ?

    • #179778
      Participant
      Topics: 2
      Replies: 4
      Points: 37
      Rank: Member

      Hello,

      Thank you for your answer, I found the solution 🙂

Viewing 3 reply threads
  • The topic ‘AcceptButton Doesn't work’ is closed to new replies.