Rakha
Rakha

Reputation: 2064

Powershell (studio) - AutoComplete dropdown value clicks a button?

I'm building a GUI for a powershell script. It's a form built in Powershell studio.

I have this code which works well:

(Get-Content ".\historique.txt") | ? { $_.trim() -ne "" } | set-content ".\historique.txt"
$postes_historique = Get-Content ".\historique.txt"
$textboxPoste.AutoCompleteCustomSource.AddRange($postes_historique)

It takes what is in the "historique.txt" text file and suggests autocomplete values for the textbox like this:

enter image description here

On that texbox, i have a KEYDOWN event set up so when a user presses ENTER it clicks the button below the textbox:

$textboxPoste_KeyDown = [System.Windows.Forms.KeyEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
    if ($_.KeyCode -eq 'Enter')
    {
        $buttonConnexion.PerformClick()
    }
    
}

The strange part and my question is :

-If I click a value in the dropdown, it clicks the button. (UNWANTED BEHAVIOR)

-If I REMOVE that KeyDown enter event, it DOESN'T click the button. (Wanted behavior)

That seems very strange to me, it's as if when you click a dropdown value, the code understands it like "YOU PRESSED ENTER". What kind of weird correlation is that? I want to have both, which is being able to press down enter to click the button AND being able to choose an autocomplete value without it triggering a button click...Doesn't seem like too much to ask, no?

Thank you for your time.

Upvotes: 0

Views: 948

Answers (1)

Rakha
Rakha

Reputation: 2064

Not sure if there is another solution but the work around in this case is :

REMOVE the keydown event that clicks the button when ENTER is pressed

and

REPLACE it by setting the form's AcceptButton to your button. This way when user presses ENTER, that button is triggered and the autocomplete dropdown acts as expected.

Upvotes: -1

Related Questions