Assistance with Radio Button Form

Hello,

I am in need of some assistance with the below form. I am attempting to write the response of the button selection to $LogonPrompt, however the variable does not exist after running the function.

Any help would be greatly appreciated!

# A function to create the form
function Prompt_Logon{
    [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
    [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') 
    
    # Set the size of the form
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 500
    $Form.height = 400
    $Form.Text = 'GUI Logon Type'
 
    # Set the font of the text to be used within the form
    $Font = New-Object System.Drawing.Font('Times New Roman',12)
    $Form.Font = $Font
 
    # Create a group that will contain the radio buttons
    $MyGroupBox = New-Object System.Windows.Forms.GroupBox
    $MyGroupBox.Location = '40,30'
    $MyGroupBox.size = '400,240'
    $MyGroupBox.text = 'Select Logon'
    
    # Create the collection of radio buttons
    $RadioButton1 = New-Object System.Windows.Forms.RadioButton
    $RadioButton1.Location = '20,40'
    $RadioButton1.size = '350,20'
    $RadioButton1.Checked = $true 
    $RadioButton1.Text = 'Project Accounting'
 
    $RadioButton2 = New-Object System.Windows.Forms.RadioButton
    $RadioButton2.Location = '20,70'
    $RadioButton2.size = '350,20'
    $RadioButton2.Checked = $false
    $RadioButton2.Text = 'Finance'
 
    $RadioButton3 = New-Object System.Windows.Forms.RadioButton
    $RadioButton3.Location = '20,100'
    $RadioButton3.size = '350,20'
    $RadioButton3.Checked = $false
    $RadioButton3.Text = 'Order Management'

    $RadioButton4 = New-Object System.Windows.Forms.RadioButton
    $RadioButton4.Location = '20,130'
    $RadioButton4.size = '350,20'
    $RadioButton4.Checked = $false
    $RadioButton4.Text = 'Purchasing - Receiving'
    
    $RadioButton5 = New-Object System.Windows.Forms.RadioButton
    $RadioButton5.Location = '20,160'
    $RadioButton5.size = '350,20'
    $RadioButton5.Checked = $false
    $RadioButton5.Text = 'Japan'
    
    $RadioButton6 = New-Object System.Windows.Forms.RadioButton
    $RadioButton6.Location = '20,190'
    $RadioButton6.size = '350,20'
    $RadioButton6.Checked = $false
    $RadioButton6.Text = 'None'
     
    # Add an OK button
    $OKButton = new-object System.Windows.Forms.Button
    $OKButton.Location = '130,280'
    $OKButton.Size = '100,40' 
    $OKButton.Text = 'OK'
    $OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
 
    #Add a Cancel button
    $CancelButton = new-object System.Windows.Forms.Button
    $CancelButton.Location = '255,280'
    $CancelButton.Size = '100,40'
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
 
    # Add all the Form controls on one line 
    $form.Controls.AddRange(@($MyGroupBox,$OKButton,$CancelButton))
 
    # Add all the GroupBox controls on one line
    $MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2,$RadioButton3,$RadioButton4,$RadioButton5,$RadioButton6))
    
    # Assign the Accept and Cancel options in the form to the corresponding buttons
    $form.AcceptButton = $OKButton
    $form.CancelButton = $CancelButton
 
    # Activate the form
    $form.Add_Shown({$form.Activate()})    
    
    # Get the results from the button click
    $dialogResult = $form.ShowDialog()
 
    # If the OK button is selected
    if ($dialogResult -eq 'OK'){
        
        # Check the current state of each radio button and respond accordingly
        if ($RadioButton1.Checked -eq $true){
            $LogonPrompt='1_Project Accounting'}
        elseif ($RadioButton2.Checked -eq $true){
            $LogonPrompt='2_Finance'}
        elseif ($RadioButton3.Checked -eq $true){
            $LogonPrompt='3_Order Management'}
        elseif ($RadioButton4.Checked -eq $true){
            $LogonPrompt='4_Purchasing_Receiving'}
        elseif ($RadioButton5.Checked -eq $true){
            $LogonPrompt='5_Japan'}
        elseif ($RadioButton6.Checked -eq $true){
            $LogonPrompt='6_None'}
    }
}
 
# Call the function
Prompt_Logon

Your issue seems to be one of scope. When you declare a variable inside a function that variable is only available inside that function by default. However if you declare it inside your function as a script level function i.e. $script:testvar it will be available for the whole script outside of the function.

See https://mohitgoyal.co/2016/12/26/understand-scopes-in-powershell-part-1/

I am sure some on here will give a better explanation but this should get you started

The best way is to return the value from function or you can scope the variable.

function Prompt_Logon {
#all your code
return $LogonPrompt
}

$PromptValue = Prompt_Logon


Using script scope
function Prompt_Logon {
#all your code
    if ($dialogResult -eq 'OK'){
        
        # Check the current state of each radio button and respond accordingly
        if ($RadioButton1.Checked -eq $true){
            $Script:LogonPrompt='1_Project Accounting'}
        elseif ($RadioButton2.Checked -eq $true){
            $LogonPrompt='2_Finance'}
        elseif ($RadioButton3.Checked -eq $true){
            $Script:LogonPrompt='3_Order Management'}
        elseif ($RadioButton4.Checked -eq $true){
            $Script:LogonPrompt='4_Purchasing_Receiving'}
        elseif ($RadioButton5.Checked -eq $true){
            $Script:LogonPrompt='5_Japan'}
        elseif ($RadioButton6.Checked -eq $true){
            $Script:LogonPrompt='6_None'}
    }
}
$Script:LogonPrompt

The first approach of @kvprasoon is the best way to handle the output

[pre]

function Prompt_Logon {
#all your code
return $LogonPrompt
}

$PromptValue = Prompt_Logon

[/pre]

[quote quote=135227]The first approach of @kvprasoon is the best way to handle the output

PowerShell
9 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
function Prompt_Logon {
#all your code
return $LogonPrompt
}
$PromptValue = Prompt_Logon
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

Thank you both for the prompt responses! This was a huge help!