combobox trigger

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of TJ Anon TJ Anon 2 years, 1 month 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.

You must be logged in to reply to this topic.