June 19, 2018 at 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
June 19, 2018 at 12:06 pm
schleep - Tuesday, June 19, 2018 11:55 AMHi 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.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
June 19, 2018 at 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?
June 19, 2018 at 12:56 pm
schleep - Tuesday, June 19, 2018 12:31 PMI 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)
June 19, 2018 at 12:56 pm
schleep - Tuesday, June 19, 2018 12:31 PMI 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()
?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
June 20, 2018 at 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
June 20, 2018 at 5:15 am
schleep - Wednesday, June 20, 2018 4:36 AMI 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!
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
June 20, 2018 at 7:48 am
schleep - Wednesday, June 20, 2018 4:36 AMI 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?
June 20, 2018 at 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.
June 20, 2018 at 8:02 am
ZZartin - Wednesday, June 20, 2018 7:48 AMschleep - Wednesday, June 20, 2018 4:36 AMI 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?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
June 20, 2018 at 8:18 am
schleep - Wednesday, June 20, 2018 8:01 AMHi 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)
June 20, 2018 at 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
June 20, 2018 at 8:36 am
schleep - Wednesday, June 20, 2018 8:32 AMFrom 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.
June 20, 2018 at 8:54 am
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