locked
Powershell form help RRS feed

  • Question

  • I have a script that I want to start by asking for input and the input values to be assigned to variable which is then used throughout remaining script. I think this bit is working now. The issue is the radio buttons, that need to be assigned to variables.

    The form displays okay, but I cant get the radio button selection to do something after clicking OK. I cant even get it to display some text, let alone assign to variable. Sorry I am not an expert at this.

    Here is code:

    # Function to create form for input & assign to variables

    Function button ($title,$Name, $Age, $Title) {

     

    # Load Assembly for creating form & button

    #Add-Type -AssemblyName System.Windows.Forms

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

     

    # Define the form size & placement

    $form = New-Object "System.Windows.Forms.Form";

    $form.Width = 360;

    $form.Height = 350;

    $form.Text = $title;

    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

     

    # Define text label1

    $textLabel1 = New-Object "System.Windows.Forms.Label";

    $textLabel1.Left = 25;

    $textLabel1.Top = 15;

    #$textLabel.Width = 80

    $textLabel1.Text = $Name;

     

    # Define text label2

    $textLabel2 = New-Object "System.Windows.Forms.Label";

    $textLabel2.Left = 25;

    $textLabel2.Top = 50;

    $textLabel2.Width = 125

    $textLabel2.Text = $Age;

     

    # Define text label3

    $textLabel3 = New-Object "System.Windows.Forms.Label";

    $textLabel3.Left = 25;

    $textLabel3.Top = 90;

    $textLabel3.Width = 125

    $textLabel3.Text = $Title;

     

    # Define text box1 for input

    $textBox1 = New-Object "System.Windows.Forms.TextBox";

    $textBox1.Left = 170;

    $textBox1.Top = 10;

    $textBox1.width = 100;

     

    #Define text box2 for input

    $textBox2 = New-Object "System.Windows.Forms.TextBox";

    $textBox2.Left = 170;

    $textBox2.Top = 50;

    $textBox2.width = 100;

     

    # Define text box3 for input

    $textBox3 = New-Object "System.Windows.Forms.TextBox";

    $textBox3.Left = 170;

    $textBox3.Top = 90;

    $textBox3.width = 100;

     

    # Define default values for the input boxes

    $defaultValue = ""

    $textBox1.Text = $defaultValue;

    $textBox2.Text = 20;

    $textBox3.Text = Mr;

     

    # Define OK button

    $OKbutton = New-Object "System.Windows.Forms.Button";

    $OKbutton.Left = 60;

    $OKbutton.Top = 280;

    $OKbutton.Width = 100;

    $OKbutton.Text = "Ok";

    $OKbutton.Add_Click($eventHandler) ;

    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

    $form.AcceptButton = $OKButton

     

    # Define Cancel button

    $closeButton = New-Object "System.Windows.Forms.Button";

    $closeButton.Left = 180

    $closeButton.Top = 280

    $closeButton.Width = 100

    $closeButton.Text = "Cancel"

    $closeButton.DialogResult = 'Cancel'

    $closeButton.Add_Click({$Form.Close()})

    $Form.CancelButton = $closeButton

    $closeButton.Add_Click({$form.Close();$cancel=$true})

    if($cancel) {return}

     

    # Group that contains radio buttons

    $MyGroupBox = New-Object System.Windows.Forms.GroupBox

    $MyGroupBox.Location = '90,130'

    $MyGroupBox.size = '175,120'

    $MyGroupBox.text = "Select your sex"

     

    # Define radio buttons

    $RadioButton1 = New-Object System.Windows.Forms.RadioButton

    $RadioButton1.Location = '20,40'

    $RadioButton1.size = '350,20'

    $RadioButton1.Checked = $true

    $RadioButton1.Text = "Male"

     

    $RadioButton2 = New-Object System.Windows.Forms.RadioButton

    $RadioButton2.Location = '20,70'

    $RadioButton2.size = '350,20'

    $RadioButton2.Checked = $false

    $RadioButton2.Text = "Female"

     

    # Add controls to all the above objects defined

    $form.Controls.Add($OKbutton);

    $form.Controls.Add($closeButton);

    $form.Controls.Add($textLabel1);

    $form.Controls.Add($textLabel2);

    $form.Controls.Add($textLabel3);

    $form.Controls.Add($textBox1);

    $form.Controls.Add($textBox2);

    $form.Controls.Add($textBox3);

    $form.Controls.Add($MyGroupBox);

    $MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2));

    [void]$form.ShowDialog()

     

    # Assign Accept & Cancel to corresponding buttons

    $form.AcceptButton = $OKButton

    $form.CancelButton = $closeButton

     

    # Activate form

    $form.Add_Shown({$form.Activate()})

     

    # Return values

    return $textBox1.Text, $textBox2.Text, $textBox3.Text

    }

     

    $return= button "Enter your details" "Name :" "Age :" "Title :"

     

    # Output variables

    $VMName = $return[0]

    $CPU = $return[1]

    $Memory = $return[2]

     

    # If the OK button is selected

    if ($DialogResult -eq "OK"){

           

            # Check current state of each radio button & respond accordingly

            if ($RadioButton1.Checked){

               [System.Windows.Forms.MessageBox]::Show("RadioButton1 was selected" , "Male")}

            elseif ($RadioButton2.Checked){

                  [System.Windows.Forms.MessageBox]::Show("RadioButton2 was selected" , "Female")}

            }

     

    # Close the form after getting values

    $eventHandler = [System.EventHandler]{

    $textBox1.Text;

    $textBox2.Text;

    $textBox3.Text;

    $Form.Close();};

    Tuesday, July 24, 2018 5:26 PM

Answers

  • Like I posted.  I did a quick fix but didn't try to solve all of your issues.  It is your baby now.  First you have to understand PowerShell then forms.  Without the basics you will be completely lost.  Use this as a chance to learn.  Try to figure out what the code is doing.


    \_(ツ)_/

    • Marked as answer by Wotsits Friday, July 27, 2018 6:37 PM
    Tuesday, July 24, 2018 7:18 PM

All replies

  • before we continue on this magical journey; please use the code paste tool. between HTML and the image insert (<>).
    Tuesday, July 24, 2018 5:47 PM
  • We have to say this dozens of times in a day.  Please post code correctly.  Colorized code cannot be copied correctly and it is impossible to read on many browsers.  That is why technical forums provide tools for posting code.


    \_(ツ)_/

    Tuesday, July 24, 2018 5:51 PM
  • Sorry didn't see that. I tried edit but didn't let me says post is SPAM. I will add here though:

    # Function to create form for input & assign to variables
    Function button ($title,$Name, $Age, $Title) {
     
    # Load Assembly for creating form & button
    #Add-Type -AssemblyName System.Windows.Forms
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
     
    # Define the form size & placement
    $form = New-Object "System.Windows.Forms.Form";
    $form.Width = 360;
    $form.Height = 350;
    $form.Text = $title;
    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
     
    # Define text label1
    $textLabel1 = New-Object "System.Windows.Forms.Label";
    $textLabel1.Left = 25;
    $textLabel1.Top = 15;
    #$textLabel.Width = 80
    $textLabel1.Text = $Name;
     
    # Define text label2
    $textLabel2 = New-Object "System.Windows.Forms.Label";
    $textLabel2.Left = 25;
    $textLabel2.Top = 50;
    $textLabel2.Width = 125
    $textLabel2.Text = $Age;
     
    # Define text label3
    $textLabel3 = New-Object "System.Windows.Forms.Label";
    $textLabel3.Left = 25;
    $textLabel3.Top = 90;
    $textLabel3.Width = 125
    $textLabel3.Text = $Title;
     
    # Define text box1 for input
    $textBox1 = New-Object "System.Windows.Forms.TextBox";
    $textBox1.Left = 170;
    $textBox1.Top = 10;
    $textBox1.width = 100;
     
    #Define text box2 for input
    $textBox2 = New-Object "System.Windows.Forms.TextBox";
    $textBox2.Left = 170;
    $textBox2.Top = 50;
    $textBox2.width = 100;
     
    # Define text box3 for input
    $textBox3 = New-Object "System.Windows.Forms.TextBox";
    $textBox3.Left = 170;
    $textBox3.Top = 90;
    $textBox3.width = 100;
     
    # Define default values for the input boxes
    $defaultValue = ""
    $textBox1.Text = $defaultValue;
    $textBox2.Text = 20;
    $textBox3.Text = Mr;
     
    # Define OK button
    $OKbutton = New-Object "System.Windows.Forms.Button";
    $OKbutton.Left = 60;
    $OKbutton.Top = 280;
    $OKbutton.Width = 100;
    $OKbutton.Text = "Ok";
    $OKbutton.Add_Click($eventHandler) ;
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
     
    # Define Cancel button
    $closeButton = New-Object "System.Windows.Forms.Button";
    $closeButton.Left = 180
    $closeButton.Top = 280
    $closeButton.Width = 100
    $closeButton.Text = "Cancel"
    $closeButton.DialogResult = 'Cancel'
    $closeButton.Add_Click({$Form.Close()})
    $Form.CancelButton = $closeButton
    $closeButton.Add_Click({$form.Close();$cancel=$true})
    if($cancel) {return}
     
    # Group that contains radio buttons
    $MyGroupBox = New-Object System.Windows.Forms.GroupBox
    $MyGroupBox.Location = '90,130'
    $MyGroupBox.size = '175,120'
    $MyGroupBox.text = "Select your sex"
     
    # Define radio buttons
    $RadioButton1 = New-Object System.Windows.Forms.RadioButton
    $RadioButton1.Location = '20,40'
    $RadioButton1.size = '350,20'
    $RadioButton1.Checked = $true 
    $RadioButton1.Text = "Male"
     
    $RadioButton2 = New-Object System.Windows.Forms.RadioButton
    $RadioButton2.Location = '20,70'
    $RadioButton2.size = '350,20'
    $RadioButton2.Checked = $false
    $RadioButton2.Text = "Female"
     
    # Add controls to all the above objects defined
    $form.Controls.Add($OKbutton);
    $form.Controls.Add($closeButton);
    $form.Controls.Add($textLabel1);
    $form.Controls.Add($textLabel2);
    $form.Controls.Add($textLabel3);
    $form.Controls.Add($textBox1);
    $form.Controls.Add($textBox2);
    $form.Controls.Add($textBox3);
    $form.Controls.Add($MyGroupBox);
    $MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2));
    [void]$form.ShowDialog()
     
    # Assign Accept & Cancel to corresponding buttons
    $form.AcceptButton = $OKButton
    $form.CancelButton = $closeButton
     
    # Activate form
    $form.Add_Shown({$form.Activate()})
     
    # Return values
    return $textBox1.Text, $textBox2.Text, $textBox3.Text
    }
     
    $return= button "Enter your details" "Name :" "Age :" "Title :"
     
    # Output variables
    $VMName = $return[0]
    $CPU = $return[1]
    $Memory = $return[2]
     
    # If the OK button is selected
    if ($DialogResult -eq "OK"){
            
            # Check current state of each radio button & respond accordingly
            if ($RadioButton1.Checked){
               [System.Windows.Forms.MessageBox]::Show("RadioButton1 was selected" , "Male")}
            elseif ($RadioButton2.Checked){
                  [System.Windows.Forms.MessageBox]::Show("RadioButton2 was selected" , "Female")}
            }
     
    # Close the form after getting values
    $eventHandler = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    $textBox3.Text;
    $Form.Close();}; 

    Tuesday, July 24, 2018 5:53 PM
  • Give all controls a name.  After the form closes you can retrieve any controls value like this>

    $form.Controls['control name'].Text (or whatever ypu want like "Checked".

    You cannot return values from a form while the form is active.


    \_(ツ)_/

    Tuesday, July 24, 2018 5:54 PM
  • Your code has dozens of logic and syntax errors.  You have tried to modify code copied from somewhere and your edits are just wild guesses.

    You cannot have aby form code after the "ShowDialog"


    \_(ツ)_/

    Tuesday, July 24, 2018 6:22 PM
  • Here is a quick partial fix.  Study it and try to understand how a form works and how to use forms with PowerShell.

    Function button{
        Param(
            $Title,
            $Name,
            $Age,
            $FormTitle
        )
        
        Add-Type -AssemblyName System.Windows.Forms
    
        $form =  New-Object System.Windows.Forms.Form
        $form.Width =  360
        $form.Height =  350
        $form.Text =  $FormTitle
        $form.StartPosition =  'CenterScreen'
        $form.Add_Shown({$form.Activate()})
        
        # Define text label1
        $textLabel1 =  New-Object System.Windows.Forms.Label
        $form.Controls.Add($textLabel1)
        $textLabel1.Left =  25
        $textLabel1.Top =  15
        $textLabel1.Text =  $Name
    
        # Define text label2
        $textLabel2 =  New-Object System.Windows.Forms.Label
        $form.Controls.Add($textLabel2)
        $textLabel2.Left =  25
        $textLabel2.Top =  50
        $textLabel2.Width =  125
        $textLabel2.Text =  $Age
    
        # Define text label3
        $textLabel3 =  New-Object System.Windows.Forms.Label
        $form.Controls.Add($textLabel3)
        $textLabel3.Left =  25
        $textLabel3.Top =  90
        $textLabel3.Width =  125
        $textLabel3.Text =  $Title
    
        # Define text box1 for input
        $textBox1 =  New-Object System.Windows.Forms.TextBox
        $form.Controls.Add($textBox1)
        $textBox1.Left =  170
        $textBox1.Top =  10
        $textBox1.width =  100
    
        #Define text box2 for input
    
        $textBox2 =  New-Object System.Windows.Forms.TextBox
        $form.Controls.Add($textBox2)
        $textBox2.Left =  170
        $textBox2.Top =  50
        $textBox2.width =  100
        $textBox2.Text = 20
    
        # Define text box3 for input
        $textBox3 =  New-Object System.Windows.Forms.TextBox
        $form.Controls.Add($textBox3)
        $textBox3.Left =  170
        $textBox3.Top =  90
        $textBox3.width =  100
        $textBox3.Text = 'Mr'
    
        # Define OK button
        $OKbutton =  New-Object System.Windows.Forms.Button
        $form.Controls.Add($OKbutton)
        $form.AcceptButton =  $OKButton
        $OKbutton.Left =  60
        $OKbutton.Top =  280
        $OKbutton.Width =  100
        $OKbutton.Text =  'Ok'
        $OKbutton.Add_Click($eventHandler) 
        $OKButton.DialogResult = 'OK'
        $form.AcceptButton =  $OKButton
    
        # Define Cancel button
        $closeButton =  New-Object System.Windows.Forms.Button
        $form.Controls.Add($closeButton)
        $form.CancelButton =  $closeButton
        $closeButton.Left =  180
        $closeButton.Top =  280
        $closeButton.Width =  100
        $closeButton.Text =  'Cancel'
        $closeButton.DialogResult =  'Cancel'
        $closeButton.Add_Click({$Form.Close()})
    
        # Group that contains radio buttons
        $MyGroupBox =  New-Object System.Windows.Forms.GroupBox
        $form.Controls.Add($MyGroupBox)
        $MyGroupBox.Location =  '90,130'
        $MyGroupBox.size =  '175,120'
        $MyGroupBox.text =  'Select your sex'
    
        # Define radio buttons
        $RadioButton1 =  New-Object System.Windows.Forms.RadioButton
        $MyGroupBox.Controls.Add($RadioButton1)
        $RadioButton1.Location =  '20,40'
        $RadioButton1.size =  '350,20'
        $RadioButton1.Checked =  $true 
        $RadioButton1.Text =  'Male'
    
        $RadioButton2 =  New-Object System.Windows.Forms.RadioButton
        $MyGroupBox.Controls.Add($RadioButton2)
        $RadioButton2.Location =  '20,70'
        $RadioButton2.size =  '350,20'
        $RadioButton2.Checked =  $false
        $RadioButton2.Text =  'Female'
    
        if('Ok' -eq $form.ShowDialog()){
            $gender = if ($RadioButton1.Checked){
                'Male'
            }elseif ($RadioButton2.Checked){
                'Female'
            }
            $textBox1.Text, $textBox2.Text, $textBox3.Text,$gender
        }
    }
    
    $VMName,$CPU,$Memory,$gender = button  'Enter your details' 'Name :' 'Age :'  'Title :'
            
    Write-Host $VMName,$CPU,$Memory,$gender
    


    \_(ツ)_/

    Tuesday, July 24, 2018 6:26 PM
  • Thanks I will look at that 
    Tuesday, July 24, 2018 6:33 PM
  • I got it nearly there, but for some reason the variables assigned are outputting the wrong variable. So $VMName is showing what $CPU is, for example.

    Function button{
        Param(
            $FormTitle,
            $VMName,
            $CPU,
            $Memory       
        )
        
    Add-Type -AssemblyName System.Windows.Forms
    
    $form =  New-Object System.Windows.Forms.Form
    $form.Width =  360
    $form.Height =  350
    $form.Text =  $FormTitle
    $form.StartPosition =  'CenterScreen'
    $form.Add_Shown({$form.Activate()})
    
    # Define text label1
    $textLabel1 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel1)
    $textLabel1.Left =  25
    $textLabel1.Top =  15
    $textLabel1.Text =  $VMName
    
    # Define text label2
    $textLabel2 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel2)
    $textLabel2.Left =  25
    $textLabel2.Top =  50
    $textLabel2.Width =  125
    $textLabel2.Text =  $CPU
    
    # Define text label3
    $textLabel3 =  New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel3)
    $textLabel3.Left =  25
    $textLabel3.Top =  90
    $textLabel3.Width =  125
    $textLabel3.Text =  $Memory
    
    # Define text box1 for input
    $textBox1 =  New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox1)
    $textBox1.Left =  170
    $textBox1.Top =  15
    $textBox1.width =  100
    
    # Define text box2 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 text box3 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 OK button
    $OKbutton =  New-Object System.Windows.Forms.Button
    $form.Controls.Add($OKbutton)
    $form.AcceptButton =  $OKButton
    $OKbutton.Left =  60
    $OKbutton.Top =  280
    $OKbutton.Width =  100
    $OKbutton.Text =  'Ok'
    $OKbutton.Add_Click($eventHandler) 
    $OKButton.DialogResult = 'OK'
    $form.AcceptButton =  $OKButton
    
    # Define Cancel button
    $closeButton =  New-Object System.Windows.Forms.Button
    $form.Controls.Add($closeButton)
    $form.CancelButton =  $closeButton
    $closeButton.Left =  180
    $closeButton.Top =  280
    $closeButton.Width =  100
    $closeButton.Text =  'Cancel'
    $closeButton.DialogResult =  'Cancel'
    $closeButton.Add_Click({$Form.Close()})
    
    # Group that contains radio buttons
    $MyGroupBox =  New-Object System.Windows.Forms.GroupBox
    $form.Controls.Add($MyGroupBox)
    $MyGroupBox.Location =  '90,130'
    $MyGroupBox.size =  '175,120'
    $MyGroupBox.text =  'Select 2016 version'
    
    # Define radio buttons
    $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'
    
    if('Ok' -eq $form.ShowDialog()){
            $Version = if ($RadioButton1.Checked){
                'Standard'
            }elseif ($RadioButton2.Checked){
                'Core'
            }
            $textBox1.Text, $textBox2.Text, $textBox3.Text, $Version
        }
    }
    
    $FormTitle,$VMName,$CPU,$Memory = button  'Enter new Virtual Server details' 'Server Name :' 'How many vCPUs :'  'How much RAM :'
            
    Write-Host $VMName,$CPU,$Memory,$Version 
    

    Tuesday, July 24, 2018 7:12 PM
  • Like I posted.  I did a quick fix but didn't try to solve all of your issues.  It is your baby now.  First you have to understand PowerShell then forms.  Without the basics you will be completely lost.  Use this as a chance to learn.  Try to figure out what the code is doing.


    \_(ツ)_/

    • Marked as answer by Wotsits Friday, July 27, 2018 6:37 PM
    Tuesday, July 24, 2018 7:18 PM