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.
Locked
StupidSexyFlanders
Posts: 107
Joined: Sun Nov 27, 2011 8:22 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
Joined: Thu Aug 18, 2011 4:56 am

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
Joined: Sun Nov 27, 2011 8:22 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: 14696
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 5 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
Joined: Thu Aug 18, 2011 4:56 am

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: 14696
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 5 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
Joined: Sun Nov 27, 2011 8:22 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

Locked