Trigger event on any change within a groupbox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Trigger event on any change within a groupbox

Post by StupidSexyFlanders »

If you have a groupbox with several controls can the state of those controls be queried whenever any control within the groupbox is changed?

For example, say I have a groupbox with 20 radiobuttons within it. How can I get a loop like this to start?:

Code: Select all

foreach($control in $groupbox.Controls)
{
   if($control.Checked -eq $true)
   {
      Write-Host ('You have selected ' + $control.Name)
   }
}
My goal is to avoid writing a elaborate switch to check each on in turn. :)
Mike
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Trigger event on any change within a groupbox

Post by davidc »

You should use the individual control's change events.

If you are using RadioButtons then you should use the control's CheckedChanged event. For more information please refer to the following blog post:

http://www.sapien.com/blog/2011/07/08/p ... n-control/


David
David
SAPIEN Technologies, Inc.
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Re: Trigger event on any change within a groupbox

Post by StupidSexyFlanders »

Yeah, I am familiar with using the checkchanged event to manage the checkstate of radio buttons.

I was just hoping that there was a way to avoid going through each button one at a time and defining essentially the same action for each one. I thought there was a general click event for groupboxes that could use to trigger a forloop, but I didn't see it listed among the events for that object.

Oh well, can't have everything I suppose.
Mike
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trigger event on any change within a groupbox

Post by jvierra »

Radio buttons behave as a group.

Checkboxes behave individually.

A custom control group can be designed to do what you ask.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Trigger event on any change within a groupbox

Post by davidc »

All the radio buttons can share the same single event. You do not need to create an event block for each of them.

David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trigger event on any change within a groupbox

Post by jvierra »

Yes - you can wire up the whole group in onload so you don't have to edit it manually every time things are changed.

You can also use a flow panel to dynamically add the radio buttons. Just set the flow characteristics and add a named and labeled button. It will position itself automatically. As you add the buttons set the event function or script block for each button.

With any of these methods you will need to manually set some aspect of the control group or a variable or just enumerate the children of the group if the info is used only when the from is submitted.
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Re: Trigger event on any change within a groupbox

Post by StupidSexyFlanders »

The blog post mentioned previously indicated that each event must also check for the 'Checked' state:
We set the same CheckChanged event for all the radio buttons and use the $this variable to access the radio button that triggered the event.
PowerShell Code
Double-click the code block to select all.
$radiobutton_CheckedChanged={
    #Use the $this variable to access the calling control
    #Only set the Label if this radio button is checked    
    if($this.Checked -eq $true)
    {
        #Use the Radio Button's Text to set the label
        $labelSelected.Text = $this.Text    
    }
}
However, that does not appear to be the case as this is working just fine:
PowerShell Code
Double-click the code block to select all.
$radiobutton1_CheckedChanged={
	$textbox1.Text = $this.Text
}
$radiobutton2_CheckedChanged={
	$textbox1.Text = $this.Text
}
$radiobutton3_CheckedChanged={
	$textbox1.Text = $this.Text
}
$radiobutton4_CheckedChanged={
	$textbox1.Text = $this.Text
}
$radiobutton5_CheckedChanged={
	$textbox1.Text = $this.Text
}
Mike
This topic is 10 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked