none
Windows Form RRS feed

  • Question

  • Hello All,

    I'm trying to initiate myself to .Net Windows Forms through Powershell, to make simple GUI (I'm a beginner).

    The Scenario I'm trying to build in my form is this one:

    1. Ask the user what he wants to do , 2 possibilties:

    - report (it will in the end initiate GET-commands .. get-aduser, get-adcomputer etc..)

    - perform (it will initiate in the SET-commands.  set-aduser, set-adcomputer etc..)

    -> this a RadioButton

    2. Then ask the user if he wants to work on

    - users

    - computers

    -> this a ComboBox (dropdownlist, because in the future there will be many other stuff than users & computers)

    3. Then click on the button "Avalaible Actions" to create the list of available actions according to the previous choice made in step 1 & 2, example it could generate: get-disabled computers, get-lockout users account...

    -> this will be checkboxes in a Tab

    4. Click on a proceed button to get the Results of the checked checkboxes.

    In the code below I've suppress the step 4 (proceed button) because I'm already blocked ealier :-/

    My problem is in my function Radioselectchanged:

    A - it is always the same checbox which is appearing and I don't understand why ?

    B - i don't understand why if (($RadioButtonReport.Checked -eq $true) -and ($OnWhatEnvChoice = "USERS")), doesn't work with if

    if (($RadioButtonReport.Checked -eq $true) -and ($OnWhatEnvChoice -EQ "USERS")), or with

    if (($RadioButtonReport.Checked -eq $true) -and ($OnWhatEnvChoice -contains "USERS"))

    N.B: I've also tried to add $script before $OnWhatEnvChoice : $script:onWhatEnvChoice, as the variable is declared outside the function but it doesn't work as well.

    Thank you for making some holes in my Ignorance. I've tried by myself since a few days but without success.

    Here under is the code:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    
    
    ##################################################################################################
    # FORMS AREA                                
    ##################################################################################################
    
    
    
    ##################Main Form Definition
        
        $main_form = New-Object System.Windows.Forms.Form
        $main_form.Text = "ToolBox" #Form Title
        $main_form.Size = New-Object System.Drawing.Size(990,950)
        $main_form.StartPosition = "CenterScreen"
        $main_form.KeyPreview = $True
        $main_form.AutoScroll = $True
        $main_form.AutoSize =$false
        $main_form.FormBorderStyle="Fixed3D"
        $main_form.BackColor = "WhiteSmoke"
        $main_form.MaximizeBox = $false
        $main_form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
        {$main_form.Close()}})
    
    
    
     ##################GroupBox Definition
    
    
        $groupBoxLog = New-Object System.Windows.Forms.GroupBox
        $groupBoxLog.Location = New-Object System.Drawing.Size(10,575)
        $groupBoxLog.size = New-Object System.Drawing.Size(960,315)
        $groupBoxLog.text = "Output:"
        $main_form.Controls.Add($groupBoxLog)
    
        $groupBoxOnWhat = New-Object System.Windows.Forms.GroupBox
        $groupBoxOnWhat.Location = New-Object System.Drawing.Size(150,275)
        $groupBoxOnWhat.size = New-Object System.Drawing.Size(180,80)
        $groupBoxOnWhat.text = "On What :"
        $main_form.Controls.Add($groupBoxOnWhat)
    
        $groupBoxRadioButton = New-Object System.Windows.Forms.GroupBox
        $groupBoxRadioButton.Location = New-Object System.Drawing.Size(10,275)
        $groupBoxRadioButton.size = New-Object System.Drawing.Size(130,80)
        $groupBoxRadioButton.text = "Choose An Action:"
        $main_form.Controls.Add($GroupBoxRadioButton)
    
     
        $groupBoxAvailableActions = New-Object System.Windows.Forms.GroupBox
        $groupBoxAvailableActions.Location = New-Object System.Drawing.Size(350,275)
        $groupBoxAvailableActions.size = New-Object System.Drawing.Size(80,80)
        $groupBoxAvailableActions.text = ""
        $main_form.Controls.Add($groupBoxAvailableActions)
    
    
    
      ##################Report Tab Definition
    
    
                $TabControl = New-object System.Windows.Forms.TabControl
                $System_Drawing_Point = New-Object System.Drawing.Point
                $System_Drawing_Point.X = 10
                $System_Drawing_Point.Y = 370
                $tabControl.Location = $System_Drawing_Point
                $System_Drawing_Size = New-Object System.Drawing.Size
                $System_Drawing_Size.Height = 190
                $System_Drawing_Size.Width = 960
                $tabControl.Size = $System_Drawing_Size
                #$TabControl.add_SelectedIndexChanged({enableObjects})
                $main_form.Controls.Add($tabControl)
                $TabControl.Enabled = $true
        
                $Tab = New-Object System.Windows.Forms.TabPage
                $Tab.Text = "Report Management"
                $tabControl.Controls.Add($Tab)
    
    
       ########## Checkbox Definition
    
                $UsersDisableCheckBox = New-Object System.Windows.Forms.CheckBox
                $UsersDisableCheckBox.Location = new-object System.Drawing.Size(10,0)
                $UsersDisableCheckBox.Size = new-object System.Drawing.Size(250,50)
                $UsersDisableCheckBox.Text = "Disable Accounts"
                $UsersDisableCheckBox.Checked = $false
                $UsersDisableCheckBox.Enabled = $false
                $UsersDisableCheckBox.Visible= $false
                $Tab.Controls.Add($UsersDisableCheckBox)
    
    
                $blablacheckBox = New-Object System.Windows.Forms.CheckBox
                $blablacheckBox.Location = new-object System.Drawing.Size(10,35)
                $blablacheckBox.Size = new-object System.Drawing.Size(250,50)
                $blablacheckBox.Text = "BLABLA"
                $blablacheckBox.Checked = $false
                $blablacheckBox.Enabled=$false
                $blablacheckBox.Visible=$false
                $Tab.Controls.Add($blablacheckBox)
    
    
    
        ################## Available Actions Button Definition
    
    
        $buttonAvailableActions = New-Object System.Windows.Forms.Button
        $buttonAvailableActions.add_click({radioSelectChanged})
        $buttonAvailableActions.Text = "Available Actions"
        $buttonAvailableActions.Top=15
        $buttonAvailableActions.Left=7
        $buttonAvailableActions.Size= New-Object System.Drawing.Size(67,55)
        $buttonAvailableActions.Enabled = $true #Disabled by default
        $groupBoxAvailableActions.Controls.Add($buttonAvailableActions)
    
       ######## Radio Button Definition
     
        $RadioButtonReport = New-Object System.Windows.Forms.RadioButton
        $RadioButtonReport.Location = New-Object System.Drawing.Size (25,22)
        $RadioButtonReport.size = New-Object System.Drawing.Size(80,20)
        $RadioButtonReport.Checked = $false
        $RadioButtonReport.Text = "Report"
        $GroupBoxRadioButton.Controls.Add($RadioButtonReport)
       
    
        $RadioButtonPerform = New-Object System.Windows.Forms.RadioButton
        $RadioButtonPerform.Location = New-Object System.Drawing.Size (25,48)
        $RadioButtonPerform.size = New-Object System.Drawing.Size(80,20)
        $RadioButtonPerform.Checked = $false  
        $RadioButtonPerform.Enabled=$false                                  
        $RadioButtonPerform.Text = "Perform"
        $GroupBoxRadioButton.Controls.Add($RadioButtonPerform)
    
    
       ##################OnWhat DropDownBox Definition
    
        
    
        $OnWhatDropDownBox = New-Object System.Windows.Forms.ComboBox
        $OnWhatDropDownBox.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList #Disable user input in ComboBox
        $OnWhatDropDownBox.Location = New-Object System.Drawing.Size(10,20)
        $OnWhatDropDownBox.Size = New-Object System.Drawing.Size(150,20)
        $OnWhatDropDownBox.DropDownHeight = 200
        $OnWhatDropDownBox.Enabled=$true
        $OnWhatDropDownBox.add_SelectedIndexChanged({$OnWhatEnvChoice})
        $groupBoxOnWhat.Controls.Add($OnWhatDropDownBox)
    
    
    
    #################################################################################################
    ############ FUNCTIONS AREA
    #################################################################################################
    
        ##################Dropdown Array
    
        $OnWhatENVs = @("USERS","COMPUTERS")
    
    
        # OnWhatENV
    
        foreach ($OnWhatENV in $OnWhatENVs) {
                              $OnWhatDropDownBox.Items.Add($OnWhatENV)
                                      } #end foreach
    
        $OnWhatEnvChoice=$OnWhatDropDownBox.SelectedText.ToString()
    
     ###################### Functions Create CheckBoxes
    
    
    function radioSelectChanged
    {
    
    
        if (($RadioButtonReport.Checked -eq $true) -and ($OnWhatEnvChoice = "USERS"))    
            {
                $blablacheckBox.Enabled= $true
                $blablacheckBox.Visible= $true
            }
            
        elseif (($RadioButtonReport.Checked -eq $true) -and ($OnWhatEnvChoice = "COMPUTERS"))
            {
                $blablacheckBox.Visible=$false
                $blablacheckBox.Enabled= $false
                $UsersDisableCheckBox.Visible= $true
                $UsersDisableCheckBox.Enabled=$true
            }
    
          else
            {
                $blablacheckBox.Visible=$false
                $blablacheckBox.Enabled= $false
                $UsersDisableCheckBox.Visible= $false
                $UsersDisableCheckBox.Enabled=$false
            }
         
     }
     
    
    
     ##################Show Form
    
        $main_form.Add_Shown({$main_form.Activate()})
        [void] $main_form.ShowDialog()






    • Edited by doublevue Monday, October 9, 2017 2:19 PM
    Monday, October 9, 2017 1:10 PM

Answers

  • You are not using the combo value:

    if ($RadioButtonReport.Checked -and $OnWhatDropDownBox.SelectedItem -eq 'USERS')


    \_(ツ)_/

    • Marked as answer by doublevue Tuesday, October 10, 2017 7:41 AM
    Monday, October 9, 2017 8:52 PM

All replies

  • Use "-eq" and not "=".


    \_(ツ)_/

    Monday, October 9, 2017 2:32 PM
  • Thank you JRV

    As I was saying in point 'B' (not clearly i confess), it doesn't work either using "-eq".

    In fact it is worse, using "-eq", no checkbox at all is displayed.

    And if I try for example:

    if (($RadioButtonReport.Checked -eq $true) -and ($OnWhatEnvChoice -contains "USERS"))

    it doesn't work neither.

    Monday, October 9, 2017 2:44 PM
  • You are not using the combo value:

    if ($RadioButtonReport.Checked -and $OnWhatDropDownBox.SelectedItem -eq 'USERS')


    \_(ツ)_/

    • Marked as answer by doublevue Tuesday, October 10, 2017 7:41 AM
    Monday, October 9, 2017 8:52 PM
  • I thought I was using it through

    $OnWhatEnvChoice=$OnWhatDropDownBox.SelectedText.ToString()

    or

    $OnWhatEnvChoice=$OnWhatDropDownBox.SelectedItem

    but this doesn't work.

    The way you show me to use the combo value works perfectly.

    It does excatly what I want.

    Thank you very much,

    • Marked as answer by doublevue Tuesday, October 10, 2017 7:41 AM
    • Unmarked as answer by doublevue Tuesday, October 10, 2017 7:41 AM
    Tuesday, October 10, 2017 7:41 AM