Programmatically trigger an event on a WinForms object in Powershell

  • Hi all,

    Let's say I've got a label ($ObjLabel) and a checkbox ($ObjCheckBox), that is created not checked, and added to my form.

    I've got code in the CheckedChanged event that changes the text in the label.

    How can I programmatically run the CheckedChanged event?

    Thanks!

    P

  • schleep - Tuesday, June 19, 2018 11:55 AM

    Hi all,

    Let's say I've got a label ($ObjLabel) and a checkbox ($ObjCheckBox), that is created not checked, and added to my form.

    I've got code in the CheckedChanged event that changes the text in the label.

    How can I programmatically run the CheckedChanged event?

    Thanks!

    P

    Just checking - you want your external PoSh process to somehow 'reach into' your WinForms app and fire an event, is that right? I don't think you can do that, though I'm not saying it's impossible.

    Instead, you'd need to code your WinForms app to react to something that the PoSh process does.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I guess I didn't explain myself very well.

    This is all in a single powershell script. In pseudocode:

    1. create label $objLabel1 with text "Black"
    2. create checkbox $ObjCB1, with Checked = $false
    3. $ObjCB1.Add_CheckChanged( {if $objCB1.Checked {$ObjLabel1.Text = "White"} else {$ObjLabel1.Text = "Black"}})

    Now I want to see trigger this event, which should change the label's text property.

    4. programmatically trigger the CheckChanged event on $ObjCB1

    Then run this code.

    Does this make more sense?

  • schleep - Tuesday, June 19, 2018 12:31 PM

    I guess I didn't explain myself very well.

    This is all in a single powershell script. In pseudocode:

    1. create label $objLabel1 with text "Black"
    2. create checkbox $ObjCB1, with Checked = $false
    3. $ObjCB1.Add_CheckChanged( {if $objCB1.Checked {$ObjLabel1.Text = "White"} else {$ObjLabel1.Text = "Black"}})

    Now I want to see trigger this event, which should change the label's text property.

    4. programmatically trigger the CheckChanged event on $ObjCB1

    Then run this code.

    Does this make more sense?

    I was able to do something like this if this is what you're looking for,

    $check_box_clicked=
    {
      If($checkBox3.Checked)
      {
       $textBox.Text = "Clicked"
      }
      Else
      {
       $textBox.Text = "Not Clicked"
      }
    }

    $checkBox3.add_Click($check_box_clicked)

  • schleep - Tuesday, June 19, 2018 12:31 PM

    I guess I didn't explain myself very well.

    This is all in a single powershell script. In pseudocode:

    1. create label $objLabel1 with text "Black"
    2. create checkbox $ObjCB1, with Checked = $false
    3. $ObjCB1.Add_CheckChanged( {if $objCB1.Checked {$ObjLabel1.Text = "White"} else {$ObjLabel1.Text = "Black"}})

    Now I want to see trigger this event, which should change the label's text property.

    4. programmatically trigger the CheckChanged event on $ObjCB1

    Then run this code.

    Does this make more sense?

    Ah, right, that makes more sense. I've never built a WinForms PoSh script, so I am out of my depth here.
    Presumably you've tried just running
    $ObjCB1.Add_CheckChanged()
    ?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I had not tried that, but it gives an error
    Cannot find an overload for "add_CheckedChanged" and the argument count: "0".

    All the examples I can find are in C#, and it appears to be possible that way, I just can't find an example, nor the corresponding syntax, in PoSh

  • schleep - Wednesday, June 20, 2018 4:36 AM

    I had not tried that, but it gives an error
    Cannot find an overload for "add_CheckedChanged" and the argument count: "0".

    All the examples I can find are in C#, and it appears to be possible that way, I just can't find an example, nor the corresponding syntax, in PoSh

    Looks like it found add_CheckedChanged(), but expected > 0 arguments. Which, in C#, would make me think that it should be declared as a 'void'. But PoSh is a different animal & I won't waste your time by continuing to guess!

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • schleep - Wednesday, June 20, 2018 4:36 AM

    I had not tried that, but it gives an error
    Cannot find an overload for "add_CheckedChanged" and the argument count: "0".

    All the examples I can find are in C#, and it appears to be possible that way, I just can't find an example, nor the corresponding syntax, in PoSh

    Does the example I posted for you not work?

  • Hi ZZartin,
    Thanks for taking the time to respond - and check back.

    That's what I had already. What I'm trying to do it to fire the Clicked/Checked event in code.

  • ZZartin - Wednesday, June 20, 2018 7:48 AM

    schleep - Wednesday, June 20, 2018 4:36 AM

    I had not tried that, but it gives an error
    Cannot find an overload for "add_CheckedChanged" and the argument count: "0".

    All the examples I can find are in C#, and it appears to be possible that way, I just can't find an example, nor the corresponding syntax, in PoSh

    Does the example I posted for you not work?

    Once the handler has been declared (which you have shown), how do you (using code) run it?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • schleep - Wednesday, June 20, 2018 8:01 AM

    Hi ZZartin,
    Thanks for taking the time to respond - and check back.

    That's what I had already. What I'm trying to do it to fire the Clicked/Checked event in code.

    Ah... so you're trying to call the on click command from somewhere else, can you just use something like Invoke-Expression "$check_box_clicked"

    Something like this,

    $testy_button = New-Object System.Windows.Forms.Button
    $testy_button.Location = New-Object System.Drawing.Point(150,150)
    $testy_button.Size = New-Object System.Drawing.Size(75,23)
    $testy_button.Text = 'Testy'
    $testy_button.Add_Click(
      {
       Invoke-Command -ScriptBlock $check_box_clicked
      }
    )
    $form.Controls.Add($testy_button)

  • From your example above,
    $check_box_clicked= {
    If($checkBox3.Checked)
    {
      $textBox.Text = "Clicked"
    }
    Else
    {
      $textBox.Text = "Not Clicked"
    }
    }
    $checkBox3.add_Click($check_box_clicked)

    In the next line of code, I want to "Click" $checkBox3

  • schleep - Wednesday, June 20, 2018 8:32 AM

    From your example above,
    $check_box_clicked= {
    If($checkBox3.Checked)
    {
      $textBox.Text = "Clicked"
    }
    Else
    {
      $textBox.Text = "Not Clicked"
    }
    }
    $checkBox3.add_Click($check_box_clicked)

    In the next line of code, I want to "Click" $checkBox3

    Yep like I said, Invoke-Command -ScriptBlock $check_box_clicked

    Another option would be to put that code into a function and just call that function in the check box on button as well as wherever else you want.

  • Thank you Sir!!!

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply