none
Powershell form RadioButton checked issue RRS feed

  • Question

  • Hello

    I have 5 different Radio buttons (3 in one group) and (2 in another group) and want to assign a variable to the checked option in each group. I struggling to get the variable to assign correctly. This is code:

    $result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
        {    
         $Version = if ($RadioButton1.Checked){'Standard'}
                    elseif ($RadioButton2.Checked){'Core'}
         $Domain =  if ($RadioButtonDomain1.Checked){'Test'}
                    elseif ($RadioButtonDomain2.Checked){'Test1'}
                    elseif ($RadioButtonDomain3.Checked){'Test2'}
       
    }

    • Moved by Bill_Stewart Wednesday, December 12, 2018 6:35 PM This is not "debug/fix/rewrite my script for me" forum
    Thursday, August 16, 2018 8:53 AM

All replies

  • Why check for each one? design the radio buttons so that only one of the options for each section can be clocked at a time. That's what radio button are used for.

    Try looking at switch statements as well, might help you here :)

    if ($radiobutton.checked){
        $variable = "some value"
    }

    Thursday, August 16, 2018 9:45 AM
  • Ok I tried:

    if ($RadioButton1.Checked) {$Version = "Standard"}
    if ($RadioButton2.Checked) {$Version = "Core"}
    if ($RadioButtonDomain1.Checked) {$Domain = "Test"}
    if ($RadioButtonDomain2.Checked) {$Domain = "Test1"}
    if ($RadioButtonDomain3.Checked) {$Domain = "Test2"}

    But $Domain variable is getting both the values, $Version isn't getting anything.

    Thursday, August 16, 2018 11:02 AM
  • Isn't better this?:

    if ($RadioButton1.Checked) {$Version = "Standard"} elseif ($RadioButton2.Checked) {$Version = "Core"}

    if ($RadioButtonDomain1.Checked) {$Domain = "Test"} elseif ($RadioButtonDomain2.Checked) {$Domain = "Test1"} elseif ($RadioButtonDomain3.Checked) {$Domain = "Test2"}

    Thursday, August 16, 2018 11:30 AM
  • Tried that but still assigning both values to $Domain
    Thursday, August 16, 2018 12:24 PM
  • $result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK    {    
        if ($RadioButton1.Checked) {$Version = "Standard"}
        elseif ($RadioButton2.Checked) {$Version = "Core"} 
        if ($RadioButtonDomain1.Checked) {$Domain = "Test"}
        elseif ($RadioButtonDomain2.Checked) {$Domain = "Test1"}
        elseif ($RadioButtonDomain3.Checked) {$Domain = "Test2"}
        }
          
    $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text, $textBox5.Text, $Version, $Domain
    }
    
    if ($result -eq [System.Windows.Forms.DialogResult]::Cancel)
    {Exit}

    Thursday, August 16, 2018 1:47 PM
  • When a radio button is checked it creates and event.  THe event is used to execute required code.

    Add the same event to each radio button and use that to assign the variable.

    $radiobuttonDomain_CheckedChanged={
        if($this.Checked){
            $global:domain = $this.Text
            Write-Host $this.Text
        }
    }
    
    $radioButtonDomain1.add_CheckedChanged($radiobutton_CheckedChanged)
    $radioButtonDomain2.add_CheckedChanged($radiobutton_CheckedChanged)
    $radioButtonDomain3.add_CheckedChanged($radiobutton_CheckedChanged)

    Add this event to all radiobuttons in that group and the global will always reflect the item.


    \_(ツ)_/



    • Edited by jrv Thursday, August 16, 2018 2:15 PM
    Thursday, August 16, 2018 2:10 PM
  • Tried that but still assigning both values to $Domain

    Both methods proposed cannot assign multiple values.

    Do not use ISE for testing forms.  The ISE will retain old variables.  Start new PS to test the form each time.


    \_(ツ)_/

    Thursday, August 16, 2018 2:14 PM
  • How does that layout though.

    # Define radio buttons (Domain)
    $RadioButtonDomain1 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain1)
    $RadioButtonDomain1.Location =  '20,40'
    $RadioButtonDomain1.size =  '350,20'
    $RadioButtonDomain1.Checked =  $true 
    $RadioButtonDomain1.Text =  'Test1'
    $RadioButtonDomain1.add_CheckedChanged($Radiobutton_CheckedChanged)
    
    $RadioButtonDomain2 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain2)
    $RadioButtonDomain2.Location =  '20,70'
    $RadioButtonDomain2.size =  '350,20'
    $RadioButtonDomain2.Checked =  $false
    $RadioButtonDomain2.Text =  'Test2'
    $RadioButtonDomain2.add_CheckedChanged($Radiobutton_CheckedChanged)
    
    $RadioButtonDomain3 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain2)
    $RadioButtonDomain2.Location =  '20,70'
    $RadioButtonDomain2.size =  '350,20'
    $RadioButtonDomain2.Checked =  $false
    $RadioButtonDomain2.Text =  'Test3'
    $RadioButtonDomain3.add_CheckedChanged($Radiobutton_CheckedChanged)
    
    $RadiobuttonDomain_CheckedChanged={
        if($RadioButtonDomain2.Checked){
            $global:Domain = $RadioButtonDomain2.Text
            Write-Host $this.Text
        }
    }
    
    
    

    Thursday, August 16, 2018 2:42 PM
  • The script block must be defined before it is referenced.  Move it before the button creation.

    \_(ツ)_/

    Thursday, August 16, 2018 2:53 PM
  • Note that variables should be written in lower camelCase. 

    \_(ツ)_/

    Thursday, August 16, 2018 2:55 PM
  • So do the variables need to be associated after the OK button is pressed?

    if ($result -eq [System.Windows.Forms.DialogResult]::OK

    Thursday, August 16, 2018 5:11 PM
  • No.  All code has to be defined and assigned before the form is displayed.


    \_(ツ)_/

    Thursday, August 16, 2018 5:15 PM
  • Still not assigning correctly. Here all code:
    $RadiobuttonDomain1_CheckedChanged={
        if($RadioButtonDomain1.Checked){
            $global:Domain = $RadioButtonDomain1.Text
            Write-Host $RadioButtonDomain1.Text
        }
    }
    
    $RadiobuttonDomain2_CheckedChanged={
        if($RadioButtonDomain2.Checked){
            $global:Domain = $RadioButtonDomain2.Text
            Write-Host $RadioButtonDomain2.Text
        }
    }
    
    $RadiobuttonDomain3_CheckedChanged={
        if($RadioButtonDomain3.Checked){
            $global:Domain = $RadioButtonDomain3.Text
            Write-Host $RadioButtonDomain3.Text
        }
    }
    
    $MyGroupBox =  New-Object System.Windows.Forms.GroupBox
    $form.Controls.Add($MyGroupBox)
    $MyGroupBox.Location =  '25,130'
    $MyGroupBox.size =  '122,140'
    $MyGroupBox.text =  'Select version :'
    
    $RadioButton1 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBox.Controls.Add($RadioButton1)
    $RadioButton1.Location =  '20,40'
    $RadioButton1.size =  '350,20'
    $RadioButton1.Checked =  $true 
    $RadioButton1.Text =  'Standard'
    
    $RadioButton2 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBox.Controls.Add($RadioButton2)
    $RadioButton2.Location =  '20,70'
    $RadioButton2.size =  '350,20'
    $RadioButton2.Checked =  $false
    $RadioButton2.Text =  'Core'
    
    $MyGroupBoxDomain =  New-Object System.Windows.Forms.GroupBox
    $form.Controls.Add($MyGroupBoxDomain)
    $MyGroupBoxDomain.Location =  '175,130'
    $MyGroupBoxDomain.size =  '120,140'
    $MyGroupBoxDomain.text =  'Select Domain :'
    
    # Define radio buttons (Domain)
    $RadioButtonDomain1 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain1)
    $RadioButtonDomain1.Location =  '20,40'
    $RadioButtonDomain1.size =  '350,20'
    $RadioButtonDomain1.Checked =  $true 
    $RadioButtonDomain1.Text =  'test'
    $RadioButtonDomain1.add_CheckedChanged($Radiobutton1_CheckedChanged)
    
    $RadioButtonDomain2 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain2)
    $RadioButtonDomain2.Location =  '20,70'
    $RadioButtonDomain2.size =  '350,20'
    $RadioButtonDomain2.Checked =  $false
    $RadioButtonDomain2.Text =  'test1'
    $RadioButtonDomain2.add_CheckedChanged($Radiobutton2_CheckedChanged)
    
    $RadioButtonDomain3 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain3)
    $RadioButtonDomain3.Location =  '20,100'
    $RadioButtonDomain3.size =  '350,20'
    $RadioButtonDomain3.Checked =  $false
    $RadioButtonDomain3.Text =  'test2'
    $RadioButtonDomain3.add_CheckedChanged($Radiobutton3_CheckedChanged)
    
    $result = $form.ShowDialog()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {    
    
    if ($RadioButton1.Checked) {$Version = "Standard"}
        elseif ($RadioButton2.Checked) {$Version = "Core"} 
        if ($RadioButtonDomain1.Checked) {$Domain = "test"}
        elseif ($RadioButtonDomain2.Checked) {$Domain = "test1"}
        elseif ($RadioButtonDomain3.Checked) {$Domain = "test2"}
    
    }
      
    $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text, $textBox5.Text, $Version, $Domain
    }
    
    if ($result -eq [System.Windows.Forms.DialogResult]::Cancel)
    {Exit}
    Thursday, August 16, 2018 5:56 PM
  • Here is an example of how to manage a form.

    Run it and think about what you are seeing and why we would do it like this.

    function Get-DomainInfo{
        $results = [pscustomobject]@{
            Domain = $null
            Site = $null
            # etc other
        }
    
        Add-Type -AssemblyName System.Windows.Forms
        $form = New-Object System.Windows.Forms.Form
        $form.StartPosition = 'CenterScreen'
        $form.FormBorderStyle = 'FixedSingle'
    
        $groupBoxDomain = New-Object System.Windows.Forms.GroupBox
        $form.Controls.Add($groupBoxDomain)
        $groupBoxDomain.FlatStyle = 'System'
        $groupBoxDomain.Location = '5,5'
        $groupBoxDomain.BackColor = 'Bisque'
        $groupBoxDomain.ForeColor = 'DarkGreen'
        $groupBoxDomain.AutoSize = $true
        $groupBoxDomain.Text = 'Domain Group'
    
        $radioButtonDomain_CheckedChanged={
            if($this.Checked){
                $results.Domain = $this.Text
                Write-Host $this.Text
            }
        }
    
        $radioButtonDomain1 =  New-Object System.Windows.Forms.RadioButton
        $groupBoxDomain.Controls.Add($radioButtonDomain1)
        $radioButtonDomain1.Location =  '10,40'
        $radioButtonDomain1.size =  '150,30'
        $radioButtonDomain1.Checked =  $false 
        $radioButtonDomain1.Text =  'Test1'
        $radioButtonDomain1.add_CheckedChanged($radioButtonDomain_CheckedChanged)
    
        $radioButtonDomain2 =  New-Object System.Windows.Forms.RadioButton
        $groupBoxDomain.Controls.Add($radioButtonDomain2)
        $radioButtonDomain2.Location =  '10,65'
        $radioButtonDomain2.size =  '150,30'
        $radioButtonDomain2.Checked =  $false
        $radioButtonDomain2.Text =  'Test2'
        $radioButtonDomain2.add_CheckedChanged($radioButtonDomain_CheckedChanged)
    
        $radioButtonDomain3 =  New-Object System.Windows.Forms.RadioButton
        $groupBoxDomain.Controls.Add($radioButtonDomain3)
        $radioButtonDomain3.Location =  '10,90'
        $radioButtonDomain3.size =  '150,30'
        $radioButtonDomain3.Checked =  $false
        $radioButtonDomain3.Text =  'Test3'
        $radioButtonDomain3.add_CheckedChanged($radioButtonDomain_CheckedChanged)
        
        $but_InClose = New-Object System.Windows.Forms.Button
        $form.Controls.Add($but_InClose)
        $but_InClose.Location = '200,170'
        $but_InClose.Text = 'Ok'
        $but_InClose.DialogResult = 'Ok'
    
        $but_InCancel = New-Object System.Windows.Forms.Button
        $form.Controls.Add($but_InCancel)
        $form.CancelButton = $but_InCancel  # enables <escape>
        $but_InCancel.Location = '200,200'
        $but_InCancel.Text = 'Cancel'
        $but_InCancel.DialogResult = 'Cancel'
        
        $formClosing = {
            if(-not $results.Domain){
                [System.Windows.Forms.MessageBox]::Show('Must supply all values')
                $_.Cancel = $true
            }
            
        }
        $form.add_Closing($formClosing)
        
        if('Ok' -eq $form.ShowDialog()){
            $results
        }
            
    }
    Get-DomainInfo
    
    


    \_(ツ)_/

    • Marked as answer by Wotsits Friday, August 17, 2018 2:30 PM
    • Unmarked as answer by Wotsits Friday, August 17, 2018 4:20 PM
    Thursday, August 16, 2018 7:23 PM
  • Still the $Domain variable & $Version not available after form closed. I set as global but made no difference.

    I must be close. I need these variables to be used after form is OK so used in following code.

    Function button{
      Param(
          $Formtitle,
          $VMName,
          $CPU,
          $Memory,
          $IPAddress,
          $Version,
          $Domain
          #$DMZ
    )
    Function Get-DomainInfo{
      $results = [pscustomobject]@{
          Domain = $null
          Version = $null
      }
    
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $Tooltip1 = New-Object System.Windows.Forms.ToolTip
    
    $ShowHelp={
    
    Switch ($this.name) {
      "textLabel5tip"  {$tip = "Enter text here"}
      "testtip" {$tip = "Enter text here"}
    }
    
    $Tooltip1.SetToolTip($this,$tip)
    }
    
    $form =  New-Object System.Windows.Forms.Form
    $form.Width =  375
    $form.Height =  460
    $form.Text =  'Enter details'
    $form.StartPosition =  'CenterScreen'
    $form.Add_Shown({$form.Activate()})
    
    $textLabel1 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel1)
    $textLabel1.Left =  25
    $textLabel1.Top =  15
    $textLabel1.Text =  $VMName
    #$textLabel1.Name = 'Enter Server name'
    
    $textLabel2 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel2)
    $textLabel2.Left =  25
    $textLabel2.Top =  50
    $textLabel2.Width =  125
    $textLabel2.Text =  $CPU
    
    $textLabel3 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel3)
    $textLabel3.Left =  25
    $textLabel3.Top =  90
    $textLabel3.Width =  125
    $textLabel3.Text =  $Memory
    
    # Define textLabel4
    $textLabel4 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel4)
    $textLabel4.Left =  25
    $textLabel4.Top =  300
    $textLabel4.Width =  68
    $textLabel4.Text =  $IPAddress
    
    # Define textLabel5
    $textLabel5 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel5)
    $textLabel5.Left =  25
    $textLabel5.Top =  265
    $textLabel5.Width =  68
    #$textLabel5.Text =  $DMZ
    $textLabel5.Name = 'textLabel5tip'
    $textLabel5.add_MouseHover($ShowHelp)
    $form.Controls.Add($textLabel5)
    
    # Define textBox1 for input
    $textBox1 =  New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox1)
    $textBox1.Left =  170
    $textBox1.Top =  15
    $textBox1.width =  100
    
    # Define textBox2 for input
    $textBox2 =  New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox2)
    $textBox2.Left =  170
    $textBox2.Top =  50
    $textBox2.width =  100
    $textBox2.Text = 1
    
    # Define textBox3 for input
    $textBox3 =  New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox3)
    $textBox3.Left =  170
    $textBox3.Top =  90
    $textBox3.width =  100
    $textBox3.Text = 4
    
    # Define textBox4 for input
    $textBox4 =  New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox4)
    $textBox4.Left =  120
    $textBox4.Top =  300
    $textBox4.width =  100
    $textBox4.Text = '192.168.'
    
    $OKbutton =  New-Object System.Windows.Forms.Button
    $form.Controls.Add($OKbutton)
    $form.AcceptButton =  $OKButton
    $OKbutton.Left =  60
    $OKbutton.Top =  360
    $OKbutton.Width =  100
    $OKbutton.Text =  'Ok'
    $OKbutton.Add_Click($eventHandler) 
    $OKButton.DialogResult = 'OK'
    $form.AcceptButton =  $OKButton
    
    $closeButton =  New-Object System.Windows.Forms.Button
    $form.Controls.Add($closeButton)
    $closeButton.Left =  180
    $closeButton.Top =  360
    $closeButton.Width =  100
    $closeButton.Text =  'Cancel'
    $closeButton.DialogResult =  'Cancel'
    $closeButton.Add_Click({$Form.Close()})
    $form.CancelButton =  $closeButton  
    
    $MyGroupBox =  New-Object System.Windows.Forms.GroupBox
    $form.Controls.Add($MyGroupBox)
    $MyGroupBox.Location =  '25,130'
    $MyGroupBox.size =  '122,140'
    $MyGroupBox.text =  'Select version :'
    
    $Version
    $RadioButton_CheckedChanged={
          if($this.Checked){
              #$results.Version = $this.Text
              $global:Version = $this.Text
              Write-Host $this.Text
          }
      }
    
    $RadioButton1 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBox.Controls.Add($RadioButton1)
    $RadioButton1.Location =  '20,40'
    $RadioButton1.size =  '350,20'
    $RadioButton1.Checked =  $true 
    $RadioButton1.Text =  'Standard'
    $RadioButton1.add_CheckedChanged($Radiobutton_CheckedChanged)
    
    $RadioButton2 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBox.Controls.Add($RadioButton2)
    $RadioButton2.Location =  '20,70'
    $RadioButton2.size =  '350,20'
    $RadioButton2.Checked =  $false
    $RadioButton2.Text =  'Core'
    $RadioButton2.add_CheckedChanged($Radiobutton_CheckedChanged)
    
    $MyGroupBoxDomain =  New-Object System.Windows.Forms.GroupBox
    $form.Controls.Add($MyGroupBoxDomain)
    $MyGroupBoxDomain.Location =  '175,130'
    $MyGroupBoxDomain.size =  '120,140'
    $MyGroupBoxDomain.text =  'Select Domain :'
    
    $RadioButtonDomain_CheckedChanged={
          if($this.Checked){
              #$results.Domain = $this.Text
              $global:Domain = $this.Text
              Write-Host $Domain
          }
      }
    
    $RadioButtonDomain1 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain1)
    $RadioButtonDomain1.Location =  '20,40'
    $RadioButtonDomain1.size =  '350,20'
    $RadioButtonDomain1.Checked =  $true 
    $RadioButtonDomain1.Text =  'domain1'
    $RadioButtonDomain1.add_CheckedChanged($RadiobuttonDomain_CheckedChanged)
    
    $RadioButtonDomain2 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain2)
    $RadioButtonDomain2.Location =  '20,70'
    $RadioButtonDomain2.size =  '350,20'
    $RadioButtonDomain2.Checked =  $false
    $RadioButtonDomain2.Text =  'domain2'
    $RadioButtonDomain2.add_CheckedChanged($RadiobuttonDomain_CheckedChanged)
    
    $RadioButtonDomain3 =  New-Object System.Windows.Forms.RadioButton
    $MyGroupBoxDomain.Controls.Add($RadioButtonDomain3)
    $RadioButtonDomain3.Location =  '20,100'
    $RadioButtonDomain3.size =  '350,20'
    $RadioButtonDomain3.Checked =  $false
    $RadioButtonDomain3.Text =  'DMZ'
    $RadioButtonDomain3.Name = 'testtip'
    $RadioButtonDomain3.add_MouseHover($ShowHelp)
    $RadioButtonDomain3.add_CheckedChanged($RadiobuttonDomain_CheckedChanged)
    
    $form.Topmost = $true
    
    $result = $form.ShowDialog()
        
    $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text, $textBox5.Text, $Version, $Domain
    
    if ($result -eq [System.Windows.Forms.DialogResult]::Cancel)
    {Exit}
    
    }
    
    # Run function
    Get-DomainInfo
    
    }
    
    $VMName,$CPU,$Memory,$IPAddress,$Version,$Domain = button  'Enter details' 'Name :' 'CPUs :'  'RAM :' 'IP Address :'
    

    Friday, August 17, 2018 4:31 PM
  • I posted a complete script that works.  You have to learn more PowerShell and try to understand how forms are designed to work.  The code you just posted makes no sense and cannot possible run.  You are never calling the function and you have not used what I have shown you.

    Takes some time and try to understand why your code cannot work and why my code does work.

    Do NOT use ISE for testing.  It will fool you.


    \_(ツ)_/


    • Edited by jrv Friday, August 17, 2018 4:43 PM
    Friday, August 17, 2018 4:41 PM
  • Yes I do find ISE needs closing after run or terminated. The form closes correctly, but not when I add additional code under the function that runs. It seems to move onto that and exit from the first If Address statement. If Cancel button clicked, Exit and if incorrect IP address entered and OK button selected, Exit and Pop up message.

    $OKButton = New-Object System.Windows.Forms.Button
    $form.Controls.Add($OKButton)
    $OKButton.Location = '60,360'
    $OKButton.Text = 'Ok'
    $OKButton.DialogResult = 'Ok'
    
    $CloseButton = New-Object System.Windows.Forms.Button
    $form.Controls.Add($CloseButton)
    $form.CancelButton = $CloseButton  # enables <escape>
    $CloseButton.Location = '180,360'
    $CloseButton.Text = 'Cancel'
    $CloseButton.DialogResult = 'Cancel'
    
     $formClosing = {
           if(-not $results.Domain){
                [System.Windows.Forms.MessageBox]::Show('Enter all details')
                $_.Cancel = $false
                #$form.Add_Closing({[System.Windows.Forms.Application]::Exit()})        
            }       
        }        
    
    $form.Topmost = $true
    
    
        if('Ok' -eq $form.ShowDialog()){
            $global:Name = $textBox1.Text
            $global:CPU = $textBox2.Text
            $global:Memory = $textBox3.Text
            $global:IPAddress = $textBox4.Text
            $results
        }     
    }
    
    Get-DomainInfo
    
    If ($IPAddress -notlike "192.168.*") {
        (New-object -ComObject wscript.shell).Popup("Enter a valid IP Address",0,"Static IP",0x0)
        Exit 0}
        
        elseIf ($IPAddress -like "192.168.100.*") {
          Set-Variable -Name DefaultGateway -Value "192.168.100.254"
          Set-Variable -Name NetworkName -Value "LAN100"}

    Tuesday, August 21, 2018 8:27 AM
  • Note that variables should be written in lower camelCase. 

    \_(ツ)_/

    I believe this is a matter of taste. I tend to use camelCase (or actually lowercase) for local variables such as $null, $args, $true, $false, etc. But everything else I use PascalCase.

    From The Unofficial PowerShell Best Practices and Style Guide:

    "If you wish, you may use camelCase for variables within your functions (or modules) to distinguish private variables from parameters, but this is a matter of taste. Shared variables should be distinguished by using their scope name, such as $Script:PSBoundParameters or $Global:DebugPreference. If you are using camelCase for a variable that starts with a two-letter acronym (where both letters are capitalized), both letters should be set to lowercase (such as adComputer)."

    Even the PowerShell community is in doubt. There is an open discussion about this on GitHub.

    Thursday, August 23, 2018 5:39 AM
  • By convention for 2 decades lower camelCase has been the standard for variables.  Pascal for function, methods and properties.

    There is no law but the convention has its purposes and was debated hotly for almost 10 years.   Most programmers eventually settled on that standard.

    The overriding rule, though, is to be consistent.  Since nearly all variables in PowerShell are local then lowerCamel is the norm.

    Just a thought.


    \_(ツ)_/

    Thursday, August 23, 2018 5:48 AM
  • Fair enough, changed variables so starting with lower case.

    I still have issue with the Cancel button that pressing seems to go to the first If statement.

    Thursday, August 23, 2018 10:38 AM
  • You will have to learn PowerShell then learn how Forms work.   Until you understand why my code was written the way it was you will just be guessing badly.

    You have to stop guessing and know what you are doing and why.  We cannot do this thinking for you.


    \_(ツ)_/

    Thursday, August 23, 2018 10:44 AM
  • I still have issue with the Cancel button that pressing seems to go to the first If statement.

    What "Cancel" button. There is no cancel button in your code.

    What "if" statement?  There are multiple "if" statements.


    \_(ツ)_/

    Thursday, August 23, 2018 11:00 AM