Issue with Event handler

This topic contains 2 replies, has 2 voices, and was last updated by  Xavier 3 weeks, 6 days ago.

  • Author
    Posts
  • #100812

    Xavier
    Participant

    Hi,

    i have past my day to search a solution for my probleme, i have check all the Microsoft Doc and lot's of google thing but nothing seems work (Maybe i'm just bad :/ ).

    My problem is, i want to create a form (system.window.form) who have 2 thing, a button and a textbox (ill post the code after) and when i tip something in the textbox and press 'enter' i want my event 'click' on the 'Ok' boutton..

    I don't want to use the form method acceptebutton i have succefully done what i want that way but i need to have a proper event handler.

    the following code is just an example :

    #Create the 3 thing, form, button, textbox
    $FormResearch = New-Object system.Windows.Forms.Form
    $btn_Research = New-Object system.Windows.Forms.Button
    $TextBox_champResearch = New-Object system.Windows.Forms.TextBox

    #Form setting
    $FormResearch.ClientSize = '400,407'
    $FormResearch.text = "FormTittle"
    $FormResearch.TopMost = $false
    $FormResearch.AcceptButton = $btn_Research

    #Button setting
    $btn_Research.text = "buttonTittle"
    $btn_Research.width = 106
    $btn_Research.height = 18
    $btn_Research.location = New-Object System.Drawing.Point(4,344)
    $btn_Research.Add_Click({

    #If my event work that need to print that
    write-host "ok"

    })

    #Textbox setting
    $TextBox_champResearch.width = 283
    $TextBox_champResearch.height = 20
    $TextBox_champResearch.location = New-Object System.Drawing.Point(113,344)
    $TextBox_champResearch.Font = 'Microsoft Sans Serif,10'
    $TextBox_champResearch.Add_KeyDown{{
    if ($_.KeyChar -eq 13) #Si la touche entrée est "Enter" faire :
    {
    $btn_Research.PerformClick()
    }
    }}

    $FormResearch.controls.AddRange(@($TextBox_champResearch,$btn_Research))

    [void]$FormResearch.ShowDialog()

    So i need something like that. At the start i have set the event handler on the form but it's doesnt work, after i have set it in the Textbox but not better.

    Hope u can solve my problem, the craziest thing is im sure that it work well fiew day ago...

    PS : I'm not english sry for my poor language.

    Cordally,
    Xavier.

  • #101053

    Don Jones
    Keymaster

    I'm not clear on why you don't want to use AcceptButton, as that's the actual way WinForms is meant to work. All AcceptButton does is call the Click() event of whatever button you designate, so you -are- using a "proper" event handler.

    Your only other option would be to trap the KeyDown event for the form itself, see if someone pressed Enter, and then either call your button's Click() event, or run whatever other code you want. That's a hack; it's not how WinForms is meant to work.

  • #101097

    Xavier
    Participant

    Hi,

    i have solved my issue like that :

    $form1.KeyPreview             = $True #Add form event handler before the active control
    $form1.Add_KeyDown({ #Add the handler
     if ($_.KeyCode -eq "Enter") #If the key is enter
     {
      $btn1.PerformClick(); #Event click
      $_.SuppressKeyPress = $true; #For suppress the noise
     } 
    })
    

    i want to do it in that way because it's not like if my button is an "ok" button, in my mind the AcceptButton with the cancel button is for a simple OK/Exit box there i want to handle some thing and maybe use code in the event.

    I dont know why now it's working but anyway... (I think it's because i have handle other event on the textbox and the other control an the it's made my form handler don't work...)

    Thanks you for the answer.

    Well Cordially,
    Xavier.

You must be logged in to reply to this topic.