Need to run a function when the text is changed on a ComboBox

Hi, I’m trying to create this PS form with a ComboBox. When the text changes in the ComboBox I’ve been trying to run a function using $CBDrives.Add.TextChanged, but nothing seems to fire when the text is changed. Does anyone happen to know of a way to get something to fire when the text in a ComboBox changes, or when the end user changes from one option to another in the ComboBox?

Here’s a sample that is supposed to display a message box. That fails as well.

Thanks in advance!

======================================================

Load external assemblies

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

#Get the screen resolution dimensions for dynamic main form sizing
$res = Get-CimInstance CIM_VideoController | Select CurrentHorizontalResolution, CurrentVerticalResolution

#Main Form Instantiation**
$YAYMainForm = new-object System.Windows.Forms.form
$YAYMainForm.ClientSize = new-object System.Drawing.Size(($res.CurrentHorizontalResolution / 2), ($res.CurrentVerticalResolution / 1.75))
$YAYMainForm.FormBorderStyle = 1
$YAYMainForm.MaximizeBox = $false
$YAYMainForm.Name = “YAYMainForm”
$YAYMainForm.BackColor = “#eeeeee
$YAYMainForm.Text = “Test”

#END Main Form
$CBDrives = New-Object System.Windows.Forms.ComboBox
$CBDrives.Top = 75
$CBDrives.left = 130
$CBDrives.AutoSize = $true
#$CBDrives.Visible = $false
[Void] $CBDrives.Items.Add(‘Test 1’)
[Void] $CBDrives.Items.Add(‘Test 2’)
[Void] $CBDrives.Items.Add(‘Test 3’)
$CBDrive.add.TextChanged({
[void][System.Windows.Forms.MessageBox]::Show(“CBDrive text changed.”)
})

$YAYMainForm.Controls.Add($CBDrives)
$YAYMainForm.Add_FormClosing( { OnFormClosing_YAYMainForm $YAYMainForm $EventArgs} )
$YAYMainForm.Add_Shown({$YAYMainForm.Activate()})
$YAYMainForm.ShowDialog()

#Free ressources
$YAYMainForm.Dispose()

Hi Dalebert,

I hope you’re having a great Monday!
Could you edit your post and format your code? Please see: How to format code on PowerShell.org This will ensure the code is more readable for folks to help and can help with other formatting issues as well.

I see a couple issues. First be sure to share with us more details as we won’t always be able to know what you’re expecting to happen. When you say ‘nothing seems to fire’ what is your expected result? Have you ran you code in lines/sections at a time?

The first issue:

$CBDrive.add.TextChanged({

Notice above in your code you use CBDrives (plural) but here you switch to singular.

The second issue (maybe?):

I’d not use [void]. If you use [void] that typically nulls the output. I don’t think you want that.

The third issue:

I’m not seeing an ‘add’ method on that type of object when looking at it with Get-Member -Force. I did see a number of add_ properties though A couple that looked interesting was the ‘selectedindexchanged’ and ‘selectedvaluechanged’ (though textchanged was there too), but sense this is a combo box, you’re definitely changing the index when you have a new selection so I gave that a shot and it worked for me:

$action = {
    $selectedItem = $CBDrives.SelectedItem
    [System.Windows.Forms.MessageBox]::Show("CBDrive text Changed: $selecteditem")
}

$CBDrives.add_SelectedIndexChanged($action)

I have a feeling the selectedvaluechanged may work as well. Basically you need to ‘define’ an action and then take said action when the selected index changes. You can do that all inline like you were doing, but its more readable if you separate it out.

also please note that this part is erroring out as well:

OnFormClosing_YAYMainForm

Though you might have a function defined elsewhere that you didn’t share.

Thanks for looking at this with me. The typo of $CBDrive vs. $CBDrives was a copy/paste error.

I thought I had noted what I wanted to happen in the code. Sorry for not being more clear to begin with. But I will say that changing “$CBDrives.add.TextChanged” to “$CBDrives.add_SelectedIndexChanged” seems to be what the problem was. The action on the ComboBox needed changing. Now when I select the different value in the ComboBox the message appears.

Anyway, thanks for the guidance. It is much appreciated!

All good, glad that worked!