GUI: Scrollable selection of many checkboxes

This code:

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

# set form size
$Form = New-Object System.Windows.Forms.Form
$Form.width = 500
$Form.height = 500
$Form.Text = ”Install Software”

# set font
$Font = New-Object System.Drawing.Font("Verdana",10)
$Form.Font = $Font

# checkbox 7-zip
$checkbox1 = new-object System.Windows.Forms.checkbox
$checkbox1.Location = new-object System.Drawing.Size(30,30)
$checkbox1.Size = new-object System.Drawing.Size(120,20)
$checkbox1.Text = " 7-Zip"
$checkbox1.Checked = $false
$Form.Controls.Add($checkbox1)

# checkbox notepad ++
$checkbox2 = new-object System.Windows.Forms.checkbox
$checkbox2.Location = new-object System.Drawing.Size(30,50)
$checkbox2.Size = new-object System.Drawing.Size(120,20)
$checkbox2.Text = " Notepad ++"
$checkbox2.Checked = $false
$Form.Controls.Add($checkbox2)

# ok button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(130,400)
$OKButton.Size = new-object System.Drawing.Size(100,40)
$OKButton.Text = "OK"
$OKButton.Add_Click({$Form.Close()})
$form.Controls.Add($OKButton)

# cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = new-object System.Drawing.Size(255,400)
$CancelButton.Size = new-object System.Drawing.Size(100,40)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$Form.Close()})
$form.Controls.Add($CancelButton)


$OKButton.Add_Click{

if($checkbox1.Checked) {Start-Process -FilePath C:\software\7z1900-x64.exe /S}
if($checkbox2.Checked) {Start-Process -FilePath C:\software\npp.7.8.1.Installer.x64.exe /S}

}

# activate form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

(This is just an example of script, don’t try to fix it)

I would like to add many checkboxes to the GUI, but I don’t want to make the window larger, I want to make it so it stays the same size, but I will have the option to scroll if I have too many entries, it will look something like this (used paint to edit)::
image

So in short summery:
A GUI that has many checkboxes which the user can scroll from

it looks like maybe a solution is to add a Panel to the form, and the panel will automatically show a scroll bar if the content doesn’t fit. Instead of adding your checkboxes to the form,create a panel, and add them to that:

# Create panel
$Panel = New-Object System.Windows.Forms.Panel
$Panel.Location = New-Object System.Drawing.Size(0,0)
$Panel.Size = New-Object System.Drawing.Size(480,380)
$Panel.AutoScroll = $true
$Form.Controls.Add($Panel)

#example
# Checkbox 7-Zip
$checkbox1 = New-Object System.Windows.Forms.Checkbox
$checkbox1.Location = New-Object System.Drawing.Size(30,30)
$checkbox1.Size = New-Object System.Drawing.Size(120,20)
$checkbox1.Text = " 7-Zip"
$checkbox1.Checked = $false
$Panel.Controls.Add($checkbox1)
2 Likes

Thank you! That’s exactly what I wanted!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.