How to connect a function to a radio button

Welcome Forums General PowerShell Q&A How to connect a function to a radio button

Viewing 2 reply threads
  • Author
    Posts
    • #163131
      Participant
      Topics: 2
      Replies: 3
      Points: 35
      Rank: Member

      Hello everyone

      I have started the proces of creating a GUI for a powershell script that I developed.

      I will be using some radio buttons to determine what the action will be performed.
      I however don’t know on how to connect a function to the selection of a specific radiobutton
      As an example

      #
      # radiobutton 1
      #
      $RadioButton1 = New-Object Windows.Forms.radiobutton
      $RadioButton1.text = "Vaste Tekst"
      $RadioButton1.height = 20
      $RadioButton1.width = 100
      $RadioButton1.top = 45
      $RadioButton1.left = 200
      #
      # radiobutton2
      #
      $radiobutton2 = New-Object Windows.Forms.radiobutton
      $RadioButton2.text = "Vrije Tekst"
      $RadioButton2.height = 20
      $RadioButton2.width = 100
      $RadioButton2.top = 45
      $RadioButton2.left = 300
      #
      
      
      #When user selects radiobutton1, a textlabel should appear where additional info can be added, which on its turn then calls another function
      
      #I thought following piece of code in the main body would do the trick, but apparently it doesn't
      
      $event = {if ($RadioButton1.Checked) {
              $Lbl_Tekst = New-Object System.Windows.Forms.Label
              $Lbl_Tekst.Location = New-Object System.Drawing.Size(10, 350)
              $Lbl_Tekst.AutoSize = $True
              $Lbl_Tekst.Text = "versturen?"
              $Form.Controls.Add($Lbl_Tekst)
          }
      }
      $RadioButton1.add_click($event)
      

       

      Can anyone help me out?

      Tks + b rgds

    • #163238
      Participant
      Topics: 2
      Replies: 1013
      Points: 2,093
      Helping Hand
      Rank: Community Hero

      Just write the event handler for it.

      This is not PowerShell thing, it is a general WinForms/WPF thing (depending on which you are doing). So, just pull up the WinForms/WPF GUI designer and make the change or view the docs on those two topics for the examples. The process is the same with WinForms/WPF, regardless of the language you are using.

      Search for ‘winforms radio button click event’

      https://www.bing.com/search?q=winforms%20radio%20button%20click%20event&qs=n&form=QBRE&sp=-1&pq=winforms%20radio%20button%20click%20event&sc=1-33&sk=&cvid=BD7F48201AD54845B34C8A31790E5D81

      Or ‘powershell winforms radio button click event’

      https://www.bing.com/search?q=powershell%20winforms%20radio%20button%20click%20event&qs=n&form=QBRE&sp=-1&pq=powershellwinforms%20radio%20button%20click%20event&sc=1-43&sk=&cvid=4DCCF8FEB9CE49429853EA0224B30786

      WF/WPF label object are not input boxes for user interactions. So, I am not real sure what you are after there, where as you should be using a text box instead if you want user input. This also seems convoluted, just show the input object and avoid the additional click altogether,  because you have to allocate an empty space on the from to populate the text box anyway.

       

       

    • #163274
      Participant
      Topics: 2
      Replies: 3
      Points: 35
      Rank: Member

      Thank you for your reply.

      In the meantime, I have created an event and it works

       

      B rgds

       

Viewing 2 reply threads
  • The topic ‘How to connect a function to a radio button’ is closed to new replies.