locked
How to write the click event of the dynamically generated radio button in windows form using powershell . RRS feed

  • Question

  • I have a below piece of code to modify the host file on the basis of configuration file , generating  a dynamic windows form which contains multiple radio button. But i am not sure how to write the click event of each radio button which is checked.Please let me know in case any further details are required from my end. 


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help


    Wednesday, March 27, 2019 8:54 AM

Answers

  • All control events are passed with to arguments; the sender and the EventArgs object.

    <control event> (object sender,EventArgs e)

    Power shell passes these as "$this" for "sender" and "$_" for "EventArgs"

    The eventargs is a custom object for the event type.  To discover how the event is called you need to look up the event.

    For the RadioButtonCheckedChanged event the following is true:

    PS D:\scripts> $rb = [System.Windows.Forms.RadioButton]::New()
    PS D:\scripts> $rb|gm|where{$_.Name -eq 'CheckedChanged'}
    
    
       TypeName: System.Windows.Forms.RadioButton
    
    Name           MemberType Definition
    ----           ---------- ----------
    CheckedChanged Event      System.EventHandler CheckedChanged(System.Object, System.EventArgs)
    
    
    PS D:\scripts>
    

    With PowerShell all events are passed like this.  Notice "(System.Object, System.EventArgs)" which is the generic pass.  Now look up the control event: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.radiobutton?view=netframework-4.7.2And scroll to the even and click the link: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.radiobutton.checkedchanged?view=netframework-4.7.2THis will tell you if the eventargs has a special definition.  In the case of this even there is none so the EventArgs are what is passed:

    The generic "EvenArgs" object is just an empty object with no usable properties.

    For many events there are special args objects.  Here is the "Paint" event arguments description: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.painteventargs?view=netframework-4.7.2

    This event defines the "PaintEventArgs" object and shows the properties that are made available by the Net Framework for use by the paint event.\ such as the "Graphics" object for the control sending the event.

    To get a readout of the objects we would just cast the argument received and intellesense will tell us what is available.

    For the rdiobutton the sender can be exposed like this"

    $sender = [System.Windows.Forms.RadioButton]$this

    Now $sender will show us all of the properties and methods of the sending control.

    Since we know the properties we can skip this and just reference the properties that we know:

    "$this.Text"  would be the text displayed by the radio button.

    This is what I posted for this control:

        $radiobuttons_CheckedChanged = {
            if ($this.Checked) {
                [System.Windows.Forms.MessageBox]::Show($this.Text)
            }
        }

    This code block can run for every control in the group and will display the sending controls "Text" property after testing the controls "Checked" property.  There is no variable used in the code for the object sending the event.  We only need the "$this" object to know which control is eventing.

    Once you learn enough about basic programming methods and learn the basics of forms you will find that all of these things are well thought out and make programming very easy.  Without the programming and forms skills this will be massively confusing.


    \_(ツ)_/

    Thursday, March 28, 2019 9:33 AM

All replies

  • Once again I need to remind you to post code correctly in this forum.  You must post code using the code posting tool provided on the edit bar.  Code posted as you have cannot be correctly copied and is hard to read.

    Please edit your original post an fix the code.

    To add a "click" event to any code use the "add_Click()" method.  If you searh you will find hundreds of blogs showing how to do this and ho to write forms code.


    \_(ツ)_/


    • Edited by jrv Wednesday, March 27, 2019 9:07 AM
    Wednesday, March 27, 2019 9:05 AM
  • which object i have to use for radiobutton 

    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Wednesday, March 27, 2019 10:00 AM
  • which object i have to use for radiobutton 

    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Please fix you code post.  It is unreadable and cannot be copied so there is no way to answer your quesiotn.


    \_(ツ)_/

    Wednesday, March 27, 2019 10:02 AM
  • ForEach ($File in $Files) 
    {
       Write-Host "File: " $File "IsReadOnly: " $File.IsReadOnly 
       if ($File.Attributes -ne "Directory" -and $File.Attributes -ne"Directory, Archive")
       {
          if ($File.IsReadOnly -eq $true )
          {
              try  
              {
                   Set-ItemProperty -path $File.FullName -nameIsReadOnly -value $false 
                   #write-host "file:" $File.FullName "is now false read only"  -foregroundcolor "magenta"
              }
              catch [Exception] 
              { 
                   
                   Continue
              }
          }
        } 

    Wednesday, March 27, 2019 10:08 AM
  • I have mentioned the code properly in the page using the tool.

    Thanks for your input, I am explaining my problem once again .

    i have created a windows form in which i am created radiobutton dynamically ,However i have written the click event of the object which is working fine but it is working for the last radiobutton. code is not working for other radio element.

    On which object of a radiobutton i have to write the click event

    or i have to work on $form control.

    Please help.


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Wednesday, March 27, 2019 10:23 AM
  • Why can't you just edit your original post?  I didn't ask for a complete new post.

    As I noted above. YOU must add the click event to each button that you want to execute a click.

    Your code is also incomplete.  There is no form and the controls will not work without a form.

    Your code also makes little sense. Why are you using so many variables. Just assign the values to the properties.

    $rb = New-Object System.Windows.Forms.RadioButton
    $rb.Location = '295,70'
    $rb.Size = '104,20'
    $rb.Text = $j
    $rb.Font = 'Microsoft Sans Serif,10'
    $rb.add_Click(<your code>)$Form.Controls.Add($rb)


    \_(ツ)_/

    Wednesday, March 27, 2019 10:31 AM
  • I have mentioned the code properly in the page using the tool.

    Thanks for your input, I am explaining my problem once again .

    i have created a windows form in which i am created radiobutton dynamically ,However i have written the click event of the object which is working fine but it is working for the last radiobutton. code is not working for other radio element.

    On which object of a radiobutton i have to write the click event

    or i have to work on $form control.

    Please help.


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    You cannot set the same code for all radio buttons by just adding it to the last button. 

    It appears that you have copied some of this code and tried to change it without understanding the code or how code works.  Without understanding you will not likely understand what I am pointing at.


    \_(ツ)_/

    Wednesday, March 27, 2019 10:36 AM
  • $rb = New-Object System.Windows.Forms.RadioButton
    $rb.Location = '295,70'
    $rb.Size = '104,20'
    $rb.Text = $j
    $rb.Font = 'Microsoft Sans Serif,10'
    $rb.add_Click(<your code>)$Form.Controls.Add($rb)

    in a windows form which radio button is checked how to get the object of that radio button which is checked=True if i ll et the object then i ll right the code for it in click event

    Not sure whether we can achieve it or not


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Wednesday, March 27, 2019 12:06 PM
  • $rb.Checked is true if the button is checked.


    \_(ツ)_/

    Wednesday, March 27, 2019 12:20 PM
  • $rb.Checked is working but  it gives the value which is available in the last iteration in the loop but not working for initial value.

    whatever action i  am trying to perform it is working for the last radio button in the windows form.same thing it should work for the initial iteration.


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Wednesday, March 27, 2019 4:54 PM
  • Only one button can be checked at a time.  You must loop through all buttons to find the one that is checked.

    You can also use the bottom checkChanged event to set a variable to the name of the button or to perform any operations that you want that setting to perform.


    \_(ツ)_/

    Wednesday, March 27, 2019 4:57 PM
  • $Form.Controls | Where {    $_ -is [System.Windows.Forms.RadioButton]} | ForEach {    $_.Add_Click({      My Action    })}

    I am using this piece of code but in that also it is taking the value of last radio button only . my be i am doing small mistake in my loop that i am not able to find out.Please have a look on foreach loop mentioned above.


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    I have added the entire piece of code
    Wednesday, March 27, 2019 5:36 PM
  • 
    ForEach ($File in $Files) 
    {
       Write-Host "File: " $File "IsReadOnly: " $File.IsReadOnly 
       if ($File.Attributes -ne "Directory" -and $File.Attributes -ne"Directory, Archive")
       {
          if ($File.IsReadOnly -eq $true )
          {
              try  
              {
                   Set-ItemProperty -path $File.FullName -nameIsReadOnly -value $false 
                   #write-host "file:" $File.FullName "is now false read only"  -foregroundcolor "magenta"
              }
              catch [Exception] 
              { 
                   
                   Continue
              }
          }
        } 

    Wednesday, March 27, 2019 5:46 PM
  • $Form.Controls | Where {    $_ -is [System.Windows.Forms.RadioButton]} | ForEach {    $_.Add_Click({      My Action    })}

    I am using this piece of code but in that also it is taking the value of last radio button only . my be i am doing small mistake in my loop that i am not able to find out.Please have a look on foreach loop mentioned above.


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    I have added the entire piece of code

    This code will add the same function to all radiobuttons.  It is not the correct event.  You need to use the "CheckChanged" event.

    $form.Controls | 
        ForEach-Object{ 
            $_.add_CheckedChanged({
                Write-Host $_.Name $_.Text
            })
        }


    \_(ツ)_/


    • Edited by jrv Wednesday, March 27, 2019 6:49 PM
    Wednesday, March 27, 2019 6:36 PM
  • Here is an example that is tested.

    $radiobuttons_CheckedChanged={
        if($this.Checked){
        	[System.Windows.Forms.MessageBox]::Show($this.Text)
        }
    }
    
    $form1_Load={
        $groupbox1.Controls |
            ForEach-Object{
                Write-Host $_.Name
                $_.add_CheckedChanged($radiobuttons_CheckedChanged)
            }
    }
    


    \_(ツ)_/

    Wednesday, March 27, 2019 6:49 PM
  • Here is a complete working example of adding  events to a form at runtime.

    function Show-Test-RadioButton {
        Add-Type -AssemblyName System.Windows.Forms
    
        
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $form1 = New-Object 'System.Windows.Forms.Form'
        $groupbox1 = New-Object 'System.Windows.Forms.GroupBox'
        $radiobuttonPlums = New-Object 'System.Windows.Forms.RadioButton'
        $radiobuttonCarrots = New-Object 'System.Windows.Forms.RadioButton'
        $radiobuttonOranges = New-Object 'System.Windows.Forms.RadioButton'
        $radiobuttonApples = New-Object 'System.Windows.Forms.RadioButton'
        $buttonOK = New-Object 'System.Windows.Forms.Button'
        $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    
        $radiobuttons_CheckedChanged = {
            if ($this.Checked) {
                [System.Windows.Forms.MessageBox]::Show($this.Text)
            }
        }
        
        $form1_Load = {
            $groupbox1.Controls |
            ForEach-Object{
                Write-Host $_.Name
                $_.add_CheckedChanged($radiobuttons_CheckedChanged)
            }
        }
        
        $form1.Controls.Add($groupbox1)
        $form1.Controls.Add($buttonOK)
        $form1.AcceptButton = $buttonOK
        $form1.AutoScaleDimensions = '8, 17'
        $form1.AutoScaleMode = 'Font'
        $form1.ClientSize = '379, 239'
        $form1.FormBorderStyle = 'FixedDialog'
        $form1.Margin = '5, 5, 5, 5'
        $form1.MaximizeBox = $False
        $form1.MinimizeBox = $False
        $form1.Name = 'form1'
        $form1.StartPosition = 'CenterScreen'
        $form1.Text = 'Form'
        $form1.add_Load($form1_Load)
        
        #
        # groupbox1
        $groupbox1.Controls.Add($radiobuttonPlums)
        $groupbox1.Controls.Add($radiobuttonCarrots)
        $groupbox1.Controls.Add($radiobuttonOranges)
        $groupbox1.Controls.Add($radiobuttonApples)
        $groupbox1.BackColor = 'PeachPuff'
        $groupbox1.Font = 'Microsoft Sans Serif, 9pt, style=Bold'
        $groupbox1.Location = '67, 31'
        $groupbox1.Margin = '4, 4, 4, 4'
        $groupbox1.Name = 'groupbox1'
        $groupbox1.Padding = '4, 4, 4, 4'
        $groupbox1.Size = '187, 159'
        $groupbox1.TabIndex = 2
        $groupbox1.TabStop = $False
        $groupbox1.Text = 'Which is not a fruit?'
        $groupbox1.UseCompatibleTextRendering = $True
        
        #
        # radiobuttonPlums
        $radiobuttonPlums.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold, Italic'
        $radiobuttonPlums.ForeColor = 'HotTrack'
        $radiobuttonPlums.Location = '40, 123'
        $radiobuttonPlums.Margin = '4, 4, 4, 4'
        $radiobuttonPlums.Name = 'radiobuttonPlums'
        $radiobuttonPlums.Size = '139, 31'
        $radiobuttonPlums.TabIndex = 4
        $radiobuttonPlums.TabStop = $True
        $radiobuttonPlums.Text = 'Plums'
        $radiobuttonPlums.UseCompatibleTextRendering = $True
        $radiobuttonPlums.UseVisualStyleBackColor = $True
        
        #
        # radiobuttonCarrots
        $radiobuttonCarrots.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold, Italic'
        $radiobuttonCarrots.ForeColor = 'HotTrack'
        $radiobuttonCarrots.Location = '40, 91'
        $radiobuttonCarrots.Margin = '4, 4, 4, 4'
        $radiobuttonCarrots.Name = 'radiobuttonCarrots'
        $radiobuttonCarrots.Size = '139, 31'
        $radiobuttonCarrots.TabIndex = 3
        $radiobuttonCarrots.TabStop = $True
        $radiobuttonCarrots.Text = 'Carrots'
        $radiobuttonCarrots.UseCompatibleTextRendering = $True
        $radiobuttonCarrots.UseVisualStyleBackColor = $True
        
        #
        # radiobuttonOranges
        $radiobuttonOranges.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold, Italic'
        $radiobuttonOranges.ForeColor = 'HotTrack'
        $radiobuttonOranges.Location = '40, 59'
        $radiobuttonOranges.Margin = '4, 4, 4, 4'
        $radiobuttonOranges.Name = 'radiobuttonOranges'
        $radiobuttonOranges.Size = '139, 31'
        $radiobuttonOranges.TabIndex = 2
        $radiobuttonOranges.TabStop = $True
        $radiobuttonOranges.Text = 'Oranges'
        $radiobuttonOranges.UseCompatibleTextRendering = $True
        $radiobuttonOranges.UseVisualStyleBackColor = $True
        
        #
        # radiobuttonApples
    
        $radiobuttonApples.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold, Italic'
        $radiobuttonApples.ForeColor = 'MediumBlue'
        $radiobuttonApples.Location = '40, 27'
        $radiobuttonApples.Margin = '4, 4, 4, 4'
        $radiobuttonApples.Name = 'radiobuttonApples'
        $radiobuttonApples.Size = '139, 31'
        $radiobuttonApples.TabIndex = 1
        $radiobuttonApples.TabStop = $True
        $radiobuttonApples.Text = 'Apples'
        $radiobuttonApples.UseCompatibleTextRendering = $True
        $radiobuttonApples.UseVisualStyleBackColor = $True
        
        #
        # buttonOK
        $buttonOK.Anchor = 'Bottom, Right'
        $buttonOK.DialogResult = 'OK'
        $buttonOK.Location = '263, 193'
        $buttonOK.Margin = '4, 4, 4, 4'
        $buttonOK.Name = 'buttonOK'
        $buttonOK.Size = '100, 30'
        $buttonOK.TabIndex = 0
        $buttonOK.Text = '&OK'
        $buttonOK.UseCompatibleTextRendering = $True
        $buttonOK.UseVisualStyleBackColor = $True
        $groupbox1.ResumeLayout()
        $form1.ResumeLayout()
        return $form1.ShowDialog()
        
    } #End Function
    
    Show-Test-RadioButton
    


    \_(ツ)_/

    Wednesday, March 27, 2019 6:55 PM
  • Here is a complete example of how to generate a collection of radio buttons and manage them as a whole with one event code.

    function Show-Test-RadioButton {
        Add-Type -AssemblyName System.Windows.Forms
    
        $form1 = New-Object 'System.Windows.Forms.Form'
        $groupbox1 = New-Object 'System.Windows.Forms.GroupBox'
        $buttonOK = New-Object 'System.Windows.Forms.Button'
    
        $form1_Load = {
        }
        
        $form1.Controls.Add($groupbox1)
        $form1.Controls.Add($buttonOK)
        $form1.AcceptButton = $buttonOK
        $form1.AutoScaleDimensions = '8, 17'
        $form1.AutoScaleMode = 'Font'
        $form1.ClientSize = '379, 239'
        $form1.FormBorderStyle = 'FixedDialog'
        $form1.Margin = '5, 5, 5, 5'
        $form1.MaximizeBox = $False
        $form1.MinimizeBox = $False
        $form1.Name = 'form1'
        $form1.StartPosition = 'CenterScreen'
        $form1.Text = 'Form'
        $form1.add_Load($form1_Load)
        
        #
        # groupbox1
        $groupbox1.BackColor = 'PeachPuff'
        $groupbox1.Font = 'Microsoft Sans Serif, 9pt, style=Bold'
        $groupbox1.Location = '67, 31'
        $groupbox1.Margin = '4, 4, 4, 4'
        $groupbox1.Name = 'groupbox1'
        $groupbox1.Padding = '4, 4, 4, 4'
        $groupbox1.Size = '187, 159'
        $groupbox1.TabIndex = 2
        $groupbox1.TabStop = $False
        $groupbox1.Text = 'Which is not a fruit?'
        
        #
        # radiobuttons
        $fruitsMaybe ='Apples','Oranges','Carrots','Plums'
        $radiobuttons_CheckedChanged = {
            if ($this.Checked) {
                [System.Windows.Forms.MessageBox]::Show($this.Text)
            }
        }
        $yloc = 20
        foreach($fruit in $fruitsMaybe){
            $rb = New-Object System.Windows.Forms.RadioButton
            $rb.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold, Italic'
            $rb.ForeColor = 'HotTrack'
            $yLoc = $yLoc + 20
            $rb.Location = "40, $yLoc"
            $rb.Margin = '4, 4, 4, 4'
            $rb.Name = "radioButton$fruit"
            $rb.Size = '139, 31'
            $rb.TabIndex = 4
            $rb.TabStop = $True
            $rb.Text = $fruit
            $rb.UseCompatibleTextRendering = $True
            $rb.UseVisualStyleBackColor = $True
            $rb.add_CheckedChanged($radiobuttons_CheckedChanged)
            $groupbox1.Controls.Add($rb)
        }
        
        
        #
        # buttonOK
        $buttonOK.Anchor = 'Bottom, Right'
        $buttonOK.DialogResult = 'OK'
        $buttonOK.Location = '263, 193'
        $buttonOK.Margin = '4, 4, 4, 4'
        $buttonOK.Name = 'buttonOK'
        $buttonOK.Size = '100, 30'
        $buttonOK.TabIndex = 0
        $buttonOK.Text = '&OK'
        return $form1.ShowDialog()
        
    } #End Function
    
    Show-Test-RadioButton
    


    \_(ツ)_/

    Wednesday, March 27, 2019 7:07 PM
  • And here is an example of how to use auto layout to create a sizable list with AutoScroll.

    function Show-Test-RadioButton {
        Add-Type -AssemblyName System.Windows.Forms
    
        $form1 = New-Object System.Windows.Forms.Form
        $flowLayout = New-Object System.Windows.Forms.FlowLayoutPanel
        $buttonOK = New-Object System.Windows.Forms.Button
    
        
        $form1.Controls.Add($flowLayout)
        $form1.Controls.Add($buttonOK)
        $form1.AcceptButton = $buttonOK
        $form1.ClientSize = '379, 239'
        $form1.FormBorderStyle = 'Sizable'
        $form1.Margin = '5, 5, 5, 5'
        $form1.MaximizeBox = $False
        $form1.MinimizeBox = $False
        $form1.Name = 'form1'
        $form1.StartPosition = 'CenterScreen'
        $form1.Text = 'Radio Button Tester'
        
        #
        # buttonOK
        $buttonOK.Anchor = 'Bottom, Right'
        $buttonOK.DialogResult = 'OK'
        $buttonOK.Location = '263, 193'
        $buttonOK.Margin = '4, 4, 4, 4'
        $buttonOK.Name = 'buttonOK'
        $buttonOK.Size = '100, 30'
        $buttonOK.TabIndex = 0
        $buttonOK.Text = '&OK'
        
        #
        # groupbox1
        $flowLayout.BackColor = 'PeachPuff'
        $flowLayout.Font = 'Microsoft Sans Serif, 9pt, style=Bold'
        $flowLayout.Location = '67, 31'
        $flowLayout.Padding = '4, 4, 4, 4'
        $flowLayout.Size = '187, 159'
        $flowLayout.FlowDirection = 'TopDown'
        $flowLayout.WrapContents = $false
        $flowLayout.AutoScroll = $true
        
        #
        # radiobuttons
        $fruitsMaybe ='Apples','Oranges','Carrots','Plums','Grapes','Wristwatches','Cherries'
        $radiobuttons_CheckedChanged = {
            if ($this.Checked) {
                [System.Windows.Forms.MessageBox]::Show($this.Text)
            }
        }
        foreach($fruit in $fruitsMaybe){
            $rb = New-Object System.Windows.Forms.RadioButton
            $rb.Font = 'Microsoft Sans Serif, 7.8pt, style=Bold, Italic'
            $rb.ForeColor = 'HotTrack'
            $rb.AutoSize = $true
            $rb.Name = "radioButton$fruit"
            $rb.Text = $fruit
            $rb.add_CheckedChanged($radiobuttons_CheckedChanged)
            $flowLayout.Controls.Add($rb)
        }
        
        return $form1.ShowDialog()
        
    } #End Show-Test-RadioButton
    
    Show-Test-RadioButton
    


    \_(ツ)_/

    Wednesday, March 27, 2019 7:35 PM
  • foreach ($j in $Buttons_BNKB)
    {
    $object_BNKB=("$object_BNKB"+"$j")
    $object_BNKB                  = New-Object System.Windows.Forms.RadioButton
    $object_BNKB.Location         = New-Object System.Drawing.Size($RadiobuttonXStart_BNKB,$RadiobuttonYStart_BNKB) 
    $object_BNKB.Size             = New-Object System.Drawing.Size($RadiobuttonX_BNKB,$RadiobuttonY_BNKB) 
    $object_BNKB.Text             = $j
    $object_BNKB.Name             = $j
    $object_BNKB.AutoSize         = $true
    $object_BNKB.Font             = 'Microsoft Sans Serif,10'
    
    $Form.Controls.Add($object_BNKB) 
    $RadiobuttonYStart_BNKB=$RadiobuttonYStart_BNKB+35
    $handler =
        {
            param($object_BNKB)
            if ($object_BNKB.Checked){
                remove-host $filename $HostfileServer_Details  
    	        add-host $file $hashtable_IP_BNKB[$hashtable_BNKB[$j]] $hashtable_BNKB[$j]
                
            }}
    $RadiobuttonYStart_BNKB=$RadiobuttonYStart_BNKB+
    $object_BNKB.add_CheckedChanged(([System.EventHandler]$handler))
        
    	 
    }
    i have tried this option but no luck .again actin is accouring for the last iteration

    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Thursday, March 28, 2019 8:20 AM
  • Sorry but you are overcomplicating this.  Please try to understand my code examples.  You also need to not reate such bad variable names as they can distract you from understanding how the code works.

    Apparently you are trying to sue some code you have found without understanding PowerShell well enough to understand the code. 

    The code I posted shows how to design with radio buttons.  The method posted shows that you do not need to create variables.  The buttons manage themselves and produce usable events.


    \_(ツ)_/


    • Edited by jrv Thursday, March 28, 2019 8:28 AM
    Thursday, March 28, 2019 8:25 AM
  • Thanks for all your inputs but end of the day i am at the same position where i have started this forum.

    i will not raise my problem if i am having a expertise in powershell


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Thursday, March 28, 2019 8:46 AM
  • You cannot specify a param statement on event code.

    Please carefully look at my code and try to understand "why" it works and why yours does not work.


    \_(ツ)_/

    Thursday, March 28, 2019 8:58 AM
  • thanks i am i ll try and confirm

    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Thursday, March 28, 2019 9:02 AM
  • All control events are passed with to arguments; the sender and the EventArgs object.

    <control event> (object sender,EventArgs e)

    Power shell passes these as "$this" for "sender" and "$_" for "EventArgs"

    The eventargs is a custom object for the event type.  To discover how the event is called you need to look up the event.

    For the RadioButtonCheckedChanged event the following is true:

    PS D:\scripts> $rb = [System.Windows.Forms.RadioButton]::New()
    PS D:\scripts> $rb|gm|where{$_.Name -eq 'CheckedChanged'}
    
    
       TypeName: System.Windows.Forms.RadioButton
    
    Name           MemberType Definition
    ----           ---------- ----------
    CheckedChanged Event      System.EventHandler CheckedChanged(System.Object, System.EventArgs)
    
    
    PS D:\scripts>
    

    With PowerShell all events are passed like this.  Notice "(System.Object, System.EventArgs)" which is the generic pass.  Now look up the control event: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.radiobutton?view=netframework-4.7.2And scroll to the even and click the link: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.radiobutton.checkedchanged?view=netframework-4.7.2THis will tell you if the eventargs has a special definition.  In the case of this even there is none so the EventArgs are what is passed:

    The generic "EvenArgs" object is just an empty object with no usable properties.

    For many events there are special args objects.  Here is the "Paint" event arguments description: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.painteventargs?view=netframework-4.7.2

    This event defines the "PaintEventArgs" object and shows the properties that are made available by the Net Framework for use by the paint event.\ such as the "Graphics" object for the control sending the event.

    To get a readout of the objects we would just cast the argument received and intellesense will tell us what is available.

    For the rdiobutton the sender can be exposed like this"

    $sender = [System.Windows.Forms.RadioButton]$this

    Now $sender will show us all of the properties and methods of the sending control.

    Since we know the properties we can skip this and just reference the properties that we know:

    "$this.Text"  would be the text displayed by the radio button.

    This is what I posted for this control:

        $radiobuttons_CheckedChanged = {
            if ($this.Checked) {
                [System.Windows.Forms.MessageBox]::Show($this.Text)
            }
        }

    This code block can run for every control in the group and will display the sending controls "Text" property after testing the controls "Checked" property.  There is no variable used in the code for the object sending the event.  We only need the "$this" object to know which control is eventing.

    Once you learn enough about basic programming methods and learn the basics of forms you will find that all of these things are well thought out and make programming very easy.  Without the programming and forms skills this will be massively confusing.


    \_(ツ)_/

    Thursday, March 28, 2019 9:33 AM
  • It worked for me. Thank you for your time and efforts put in to resolve my query.

    And yes.. thank you for your patience too.


    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Thursday, March 28, 2019 10:27 AM