AcceptButton Doesn't work

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

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 weeks, 3 days ago.

  • 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: 1085
    Points: 3,651
    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 🙂

You must be logged in to reply to this topic.