none
Trying to limit clicks in Checkbox form RRS feed

  • Question

  • Hi Everybody

    i have a PS script that creates a GUI for internal use the follow is the creation of Destination server group creation:

           

        # Create a group for destination servers list
        $destServerGroupBox = New-Object System.Windows.Forms.GroupBox
        $destServerGroupBox.Location = '12,200'
        $destServerGroupBox.size = '600,100'
        $destServerGroupBox.text = "Destination Server: Please select 2 servers from the list below (Multiple choice enabled)"
        $destServerGroupBox.BackColor = "White"

        # Create the collection of destination servers
        $destServer1 = New-Object System.Windows.Forms.CheckBox
        $destServer1.Location = '10,25'
        $destServer1.size = '100,40'
        $destServer1.Checked = $true 
        $destServer1.Text = "VMWEBCMSPP"

        $destServer2 = New-Object System.Windows.Forms.CheckBox
        $destServer2.Location = '115,25'
        $destServer2.size = '100,40'
        $destServer2.Checked = $true
        $destServer2.Text = "VMWEBCMSPP2"

        $destServer3 = New-Object System.Windows.Forms.CheckBox
        $destServer3.Location = '220,25'
        $destServer3.size = '100,40'
        $destServer3.Checked = $false
        $destServer3.Text = "VMWEBCMSUAT"

        $destServer4 = New-Object System.Windows.Forms.CheckBox
        $destServer4.Location = '325,25'
        $destServer4.size = '100,40'
        $destServer4.Checked = $false 
        $destServer4.Text = "VMWEBCMSUAT2"

    today users can choose every server on the dest group-box i would like to limit them to select only 2.

    do you have any ides?

    Thursday, January 18, 2018 7:19 AM

Answers

  • Hi,

    Quick & dirty:

    # Load Assemblies
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    function myCheckStateChangedFunction(){
    
        $counter = 0
        if($script:destServer1.Checked -eq $true) { $counter++ } 
        if($script:destServer2.Checked -eq $true) { $counter++ } 
        if($script:destServer3.Checked -eq $true) { $counter++ } 
        if($script:destServer4.Checked -eq $true) { $counter++ } 
    
        if($counter -ge 2){
    
            if($script:destServer1.Checked -eq $false) { $script:destServer1.Enabled = $false }
            if($script:destServer2.Checked -eq $false) { $script:destServer2.Enabled = $false }
            if($script:destServer3.Checked -eq $false) { $script:destServer3.Enabled = $false }
            if($script:destServer4.Checked -eq $false) { $script:destServer4.Enabled = $false }
        }
        else{
    
            $script:destServer1.Enabled = $true 
            $script:destServer2.Enabled = $true 
            $script:destServer3.Enabled = $true 
            $script:destServer4.Enabled = $true 
        }
    }
    
    # Init Form
    $form = New-Object System.Windows.Forms.Form
    $form.width = 500
    $form.height = 150
    
    # Create a group for destination servers list
    $destServerGroupBox = New-Object System.Windows.Forms.GroupBox
    $destServerGroupBox.Location = '12,200'
    $destServerGroupBox.size = '600,100'
    $destServerGroupBox.text = "Destination Server: Please select 2 servers from the list below (Multiple choice enabled)"
    $destServerGroupBox.BackColor = "White"
    
    # Create the collection of destination servers
    $destServer1 = New-Object System.Windows.Forms.CheckBox
    $destServer1.Location = '10,25'
    $destServer1.size = '100,40'
    $destServer1.Checked = $false 
    $destServer1.Text = "VMWEBCMSPP"
    $destServer1.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    $destServer2 = New-Object System.Windows.Forms.CheckBox
    $destServer2.Location = '115,25'
    $destServer2.size = '100,40'
    $destServer2.Checked = $false
    $destServer2.Text = "VMWEBCMSPP2"
    $destServer2.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    $destServer3 = New-Object System.Windows.Forms.CheckBox
    $destServer3.Location = '220,25'
    $destServer3.size = '100,40'
    $destServer3.Checked = $false
    $destServer3.Text = "VMWEBCMSUAT"
    $destServer3.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    $destServer4 = New-Object System.Windows.Forms.CheckBox
    $destServer4.Location = '325,25'
    $destServer4.size = '100,40'
    $destServer4.Checked = $false 
    $destServer4.Text = "VMWEBCMSUAT2"
    $destServer4.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    
    $form.Controls.Add($destServer1)
    $form.Controls.Add($destServer2)
    $form.Controls.Add($destServer3)
    $form.Controls.Add($destServer4)
    
    
    # Create Form
    $form.Add_Shown({$form.Activate()})
    $form.ShowDialog()
    Best,
    Toby

    • Marked as answer by EDolev Thursday, January 18, 2018 12:16 PM
    Thursday, January 18, 2018 8:47 AM

All replies

  • Hi,

    Quick & dirty:

    # Load Assemblies
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    function myCheckStateChangedFunction(){
    
        $counter = 0
        if($script:destServer1.Checked -eq $true) { $counter++ } 
        if($script:destServer2.Checked -eq $true) { $counter++ } 
        if($script:destServer3.Checked -eq $true) { $counter++ } 
        if($script:destServer4.Checked -eq $true) { $counter++ } 
    
        if($counter -ge 2){
    
            if($script:destServer1.Checked -eq $false) { $script:destServer1.Enabled = $false }
            if($script:destServer2.Checked -eq $false) { $script:destServer2.Enabled = $false }
            if($script:destServer3.Checked -eq $false) { $script:destServer3.Enabled = $false }
            if($script:destServer4.Checked -eq $false) { $script:destServer4.Enabled = $false }
        }
        else{
    
            $script:destServer1.Enabled = $true 
            $script:destServer2.Enabled = $true 
            $script:destServer3.Enabled = $true 
            $script:destServer4.Enabled = $true 
        }
    }
    
    # Init Form
    $form = New-Object System.Windows.Forms.Form
    $form.width = 500
    $form.height = 150
    
    # Create a group for destination servers list
    $destServerGroupBox = New-Object System.Windows.Forms.GroupBox
    $destServerGroupBox.Location = '12,200'
    $destServerGroupBox.size = '600,100'
    $destServerGroupBox.text = "Destination Server: Please select 2 servers from the list below (Multiple choice enabled)"
    $destServerGroupBox.BackColor = "White"
    
    # Create the collection of destination servers
    $destServer1 = New-Object System.Windows.Forms.CheckBox
    $destServer1.Location = '10,25'
    $destServer1.size = '100,40'
    $destServer1.Checked = $false 
    $destServer1.Text = "VMWEBCMSPP"
    $destServer1.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    $destServer2 = New-Object System.Windows.Forms.CheckBox
    $destServer2.Location = '115,25'
    $destServer2.size = '100,40'
    $destServer2.Checked = $false
    $destServer2.Text = "VMWEBCMSPP2"
    $destServer2.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    $destServer3 = New-Object System.Windows.Forms.CheckBox
    $destServer3.Location = '220,25'
    $destServer3.size = '100,40'
    $destServer3.Checked = $false
    $destServer3.Text = "VMWEBCMSUAT"
    $destServer3.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    $destServer4 = New-Object System.Windows.Forms.CheckBox
    $destServer4.Location = '325,25'
    $destServer4.size = '100,40'
    $destServer4.Checked = $false 
    $destServer4.Text = "VMWEBCMSUAT2"
    $destServer4.Add_CheckStateChanged({ myCheckStateChangedFunction })
    
    
    $form.Controls.Add($destServer1)
    $form.Controls.Add($destServer2)
    $form.Controls.Add($destServer3)
    $form.Controls.Add($destServer4)
    
    
    # Create Form
    $form.Add_Shown({$form.Activate()})
    $form.ShowDialog()
    Best,
    Toby

    • Marked as answer by EDolev Thursday, January 18, 2018 12:16 PM
    Thursday, January 18, 2018 8:47 AM
  • Hi Toby

    will give it a try and let you know.

    thanks

    Eyal

    Thursday, January 18, 2018 11:08 AM
  • Thank you Toby

    works great

    Thursday, January 18, 2018 12:17 PM
  • Happy to help :-)
    Thursday, January 18, 2018 12:36 PM