combobox trigger

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Robert Gullick Robert Gullick 3 months, 1 week ago.

  • Author
    Posts
  • #19747
    Profile photo of TJ Anon
    TJ Anon
    Participant

    I'm stumped. Have a form with a ComboBox on it that holds two items. What I want is if I change the selected item in that ComboBox, some stuff happens. But there's no trigger built into the ComboBox control that I can find. I found one article and it looks like they created a trigger, but I don't understand how or why it works. Applicable lines of code:

    $cbx_Campus = New-Object System.Windows.Forms.ComboBox
    $cbx_Campus.OnTextUpdate(
    {$campus = $cbx_Campus.Text; write-host "campus = $campus" }
    ) 
    $frm_CampusVar.Controls.Add($cbx_Campus)

    Error: [System.Windows.Forms.ComboBox] doesn't contain a method named 'OnTextUpdate'.

    When I get-member on a ComboBox object, it lists "OnTextUpdate" as an event for ComboBox. So why is it not working?

    Thanks in advance for your help.

  • #19748
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    According to Get-Member, the event name is just TextUpdate, not OnTextUpdate. There's also a TextChanged event, though; I'd recommend checking the MSDN documentation to find out which event meets your needs.

  • #19749
    Profile photo of TJ Anon
    TJ Anon
    Participant

    I tried the following code. It didn't throw an error, but neither did it do anything.

       if ($cbx_Campus.TextUpdate) {
          write-host "campus = $cbx_campus.text"
          }
  • #19750
    Profile photo of TJ Anon
    TJ Anon
    Participant

    Somebody put this code up on the web. Looks like they manufactured an event trigger that works. But I'm not sure why it works or how to incorporate it in my script.

    $Form1 = New-Object System.Windows.Forms.Form
    $Form1.ClientSize = New-Object System.Drawing.Size(200, 200)
    $form1.topmost = $true
     
    $comboBox1 = New-Object System.Windows.Forms.ComboBox
    $comboBox1.Location = New-Object System.Drawing.Point(25, 55)
    $comboBox1.Size = New-Object System.Drawing.Size(150, 150)
    $comboBox1.Items.add("Test1")
    $comboBox1.Items.add("Test2")
    $Form1.Controls.Add($comboBox1)
     
    # -------------------------------------------------------------------------------
    #                             Change triggered function
    # -------------------------------------------------------------------------------
     
    $ComboBox1_SelectedIndexChanged=
    {
       If ($ComboBox1.text -eq "Test1") {Write-host "Test1 chosen"}
       Else {Write-host "Test2 chosen"}
    }
    ################ MUST CREATE BEFORE ASSIGN ################
    $ComboBox1.add_SelectedIndexChanged($ComboBox1_SelectedIndexChanged)
     
    [void]$form1.showdialog()

    Specifically, I don't know where the "add_SelectedIndexChanged" part comes from on this line: $ComboBox1.add_SelectedIndexChanged($ComboBox1_SelectedIndexChanged)

  • #19751
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Your original code was closer to what you'd need to do:

    $cbx_Campus = New-Object System.Windows.Forms.ComboBox
    $cbx_Campus.add_TextUpdate(
        {$script:campus = $cbx_Campus.Text; write-host "campus = $campus" }
    ) 
    $frm_CampusVar.Controls.Add($cbx_Campus)
    

    Notice that I added a $script: scope modifier to your variable assignment within the event handler. Otherwise, you'd be assigning a value to a local $campus variable within the handler block, and it wouldn't be accessible to the rest of your code later.

  • #19752
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Yep, you're on the right track there. They're using a different event (SelectedIndexChanged instead of TextUpdate), but the basic code is the same. You call the add_EventName() method and pass in a script block.

  • #19771
    Profile photo of TJ Anon
    TJ Anon
    Participant

    Got it. Figured sleep would help.

    $cbx_Campus_SelectedIndexChanged=
    {
    $script:campus = $cbx_Campus.Text
    If ($script:Campus -eq "RGC") {Write-host "RGC chosen"}
    Else {Write-host "HLC chosen"}
    }
    ################ MUST CREATE BEFORE ASSIGN ################
    $cbx_Campus.add_SelectedIndexChanged($cbx_Campus_SelectedIndexChanged)

    Thanks for the explanation about how add_EventName() method works.

  • #66949
    Profile photo of Robert Gullick
    Robert Gullick
    Participant

    Here is a modified example that seems to work. Two buttons are added to process the selected item and clear the form.

    #region Form
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $Form1 = New-Object System.Windows.Forms.Form
    $Form1.ClientSize = New-Object System.Drawing.Size(200, 200)
    $form1.topmost = $true
    #endregion Form

    #region Functions
    Function Clear-Form{
    $ComboBox1.SelectedIndex = -1
    $ComboBox1.SelectedItem = ""
    Clear-Host
    }

    Function Test-Something{
    If ($ComboBox1.SelectedItem -lt 0){
    $Selected = "No selection was made"
    Write-host $Selected -ForegroundColor Red

    }
    Else {
    $Selected = $ComboBox1.SelectedItem.ToString()
    Write-host "$Selected chosen" -ForegroundColor Green
    }
    Return $Selected
    }
    #endregion Functions

    #region Dropdown
    $comboBox1 = New-Object System.Windows.Forms.ComboBox
    $comboBox1.Location = New-Object System.Drawing.Point(20, 20)
    $comboBox1.Size = New-Object System.Drawing.Size(150, 150)
    $comboBox1.Items.add("Test1")
    $comboBox1.Items.add("Test2")
    $Form1.Controls.Add($comboBox1)
    #endregion Dropdown

    #region Buttons
    #region TEST Button1
    $TestButton1 = New-Object System.Windows.Forms.Button
    $TestButton1.Location = New-Object System.Drawing.Size(20,50)
    $TestButton1.Size = New-Object System.Drawing.Size(50,50)
    $TestButton1.ForeColor = "White"
    $TestButton1.BackColor = "Green"
    $TestButton1.Text = "Test"
    $TestButton1.Add_Click({Test-Something}) #Test-Something
    $Form1.controls.Add($TestButton1)
    #endregion TEST Button1

    #region TEST Button1
    $ClearButton = New-Object System.Windows.Forms.Button
    $ClearButton.Location = New-Object System.Drawing.Size(90,50)
    $ClearButton.Size = New-Object System.Drawing.Size(50,50)
    $ClearButton.ForeColor = "Black"
    $ClearButton.BackColor = "White"
    $ClearButton.Text = "Clear"
    $ClearButton.Add_Click({Clear-Form})
    $Form1.controls.Add($ClearButton)
    #endregion TEST Button1
    #endregion Buttons

    [void]$form1.showdialog()

You must be logged in to reply to this topic.