0

I am attempting to create a form in Powershell. It contains a ComboBox dropdown option that I am using as a required field. Until an option is selected, the continue button will be disabled. This is the code for the ComboBox and the button enabling:

$TSTypeBox.Name = "TSType"
$TSTypeBox.Location = New-Object System.Drawing.Point(116,100)
$TSTypeBox.Size = New-Object System.Drawing.Size(145,20)
$TSTypeBox.add_MouseHover($ShowHelp)
$TSTypeBox.DropDownStyle = "DropDownList"
Foreach ($item in ("1","2","3","4","5")) {
    $TSTypeBox.Items.Add($item) | Out-Null
    }
$TSTypeBox.SelectedItem = $TSLocation
$handler_TSTypeBox_SelectedIndexChanged= {
        If (($TSTypeBox.Text) -and ($ComputerNameBox.Text)) 
        {
            $OKButton.Enabled = 1
        }
        Else 
        {
            $OKButton.Enabled = 0
        }
    }
$TSTypeBox.add_SelectedIndexChanged($handler_TSTypeBox_SelectedIndexChanged)

This code in particular works as intended so I'm not worried about that. I am here about the $TSTypeBox.SelectedItem = $TSLocation line that I included. I have code elsewhere that pulls the IP address of the computer the program is being run on, which is then matched against an if/elseif/else statement to determine if the computer belongs to 1 or to 2, which are options that you can see were added to the ComboBox in the code above.

That if/else statement updated the $TSLocation variable which I then use to force the selection of one of the dropdown options in the ComboBox. This works as well, but unfortunately it does not enable the continue button as I would like. I had a hard time looking up issues about this because its super particular and I am probably doing this incorrectly (I have very little experience with Powershell scripting). If you have any additional questions about this please let me know. Thanks!

ccj9874
  • 35
  • 3
  • Have you tried setting 'Enabled' to boolean values? $OKButton.Enabled = $false etc? – Captain_Planet Dec 09 '21 at 19:18
  • You could perhaps also run: [System.Windows.Forms.Application]::DoEvents() after setting Enabled, but I'm not sure that should be necessary.... – Captain_Planet Dec 09 '21 at 19:19
  • How are you forcing selection when you change $TSLocation? Would be easier if you pasted a minimal working example script? – Scepticalist Dec 09 '21 at 19:25
  • @Scepticalist Sorry, I suppose I didn't make it clear earlier in the post. I "force" the selection using the line of code from the first paragraph, $TSTypeBox.SelectedItem = $TSLocation The $TSLocation variable only changes once, when the program is first run. – ccj9874 Dec 09 '21 at 19:34
  • I suspect this has something to do with scoping or how you're choosing the selected item, but without a working example of the script its hard to tell. – Scepticalist Dec 09 '21 at 20:01
  • I am attempting to modify this script for my purposes: https://github.com/sccmtst/SCCM-Management-Scripts/blob/master/TSGUI/TSGUI.ps1 In the original file, the area that I'm looking at is lines 146 to 166. The continue button code works fine if I select any option manually after running the script, but it must have some issue with me deciding what option is selected in the ComboBox at a code level. – ccj9874 Dec 09 '21 at 20:05
  • See answer below – Scepticalist Dec 10 '21 at 07:01

1 Answers1

1

Ok, this might illustrate your problem.

Just because you set the SelectedItem value to something, doesn't mean the SelectedIndex changes

#
Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop
#
$TSLocation = '2'
#
$form = New-Object System.Windows.Forms.Form
$form.Text = "Test"
$form.MinimumSize = '430,495'
$form.MaximumSize = '430,545'
$form.StartPosition = 'CenterScreen'
#
# Add form objects
#
$TSTypeBox = New-Object System.Windows.Forms.ComboBox
$TSTypeBox.Name = "TSType"
$TSTypeBox.Location = '116,100'
$TSTypeBox.Size = '145,20'
$TSTypeBox.add_MouseHover($ShowHelp)
$TSTypeBox.DropDownStyle = "DropDownList"
Foreach ($item in ("1","2","3","4","5")) {
    $TSTypeBox.Items.Add($item) | Out-Null
    }

$ComputerNameBox = New-Object System.Windows.Forms.TextBox
$ComputerNameBox.Location = '120,20'
$ComputerNameBox.Size = '120,17'
$ComputerNameBox.Text = 'test'

$OutputBox = New-Object System.Windows.Forms.TextBox
$OutputBox.Location = '120,240'
$OutputBox.Size = '120,17'

$OkButton = New-Object System.Windows.Forms.Button
$OkButton.Location = '120,200'
$OkButton.Size = '54,24'
$OkButton.Text = 'OK'

$form.controls.AddRange(@($TSTypeBox,$OkButton,$ComputerNameBox,$OutputBox))
#
# Main Script goes here
#
$handler_TSTypeBox_SelectedIndexChanged= {
    $OutputBox.Text = "SelectedIndex is " + $TSTypeBox.SelectedIndex
        If (($TSTypeBox.Text) -and ($ComputerNameBox.Text)) 
        {
            $OKButton.Enabled = 1
        }
        Else 
        {
            $OKButton.Enabled = 0
        }
    }
$TSTypeBox.add_SelectedIndexChanged($handler_TSTypeBox_SelectedIndexChanged)

#
$TSTypeBox.SelectedIndex = $TSTypeBox.FindStringExact($TSLocation)
#
# Show form
$form.ShowDialog() | Out-Null
$form.Dispose()
# End

If in doubt, always best to simplify your script and add debug ,logging or output that shows what values are changing

Now that the problem is clear - this article points you in the right direction:

How do I set the selected item in a comboBox to match my string using C#?

Scepticalist
  • 3,737
  • 1
  • 13
  • 30
  • Added a link above to a previous SO answer that should fix your problem. – Scepticalist Dec 10 '21 at 06:59
  • It seems like that still doesn't work... I see what you're saying and its what I assumed but I can't get it to work for whatever reason. Copying your code over, the button doesn't get disabled when the fields are empty and I honestly don't know how to fix that lol. I'm going to try to link this function to a check box instead, one that is just binary and not automatic. Its possible that the particular function I'm looking for doesn't work. Thank you!! – ccj9874 Dec 10 '21 at 15:05
  • See the answer on the link I posted - you need to set the `SelectedIndex` property, not `SelectedItem` I've amended the script above for an example. Remember that indexes start at ZERO. the button needs disabling when empty you code for ALL the eventualities e.g. Write an event handler for the ComputerName text box. – Scepticalist Dec 10 '21 at 15:29
  • Yep, I've got a handler on the ComputerName box too. I did update my code to use SelectedIndex but it ends up with the same result, button does not get enabled when first run. It must be something to do with ComboBoxes that make it incompatible. I ended up switching to a Checkbox confirmation and that worked for me. Thank you for your help – ccj9874 Dec 10 '21 at 17:11
  • The script above enables the button when run. Not sure you;ve supplied all the information here. – Scepticalist Dec 11 '21 at 06:58