Checkboxes in Forms
My last post was about populating a second dropdown list with data that was gathered from a choice made in the first dropdown list. I know, I jumped ahead of myself. Today’s post is about checkboxes in forms. The checkbox is a very common element in forms. Having a checkbox on your form always looks nice (don’t go over the top and put too many in your form).
The script below is a nice example of how to enable and disable the OK button by checking the checkbox.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
function checkbox_test{ [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") # Set the size of your form $Form = New-Object System.Windows.Forms.Form $Form.width = 500 $Form.height = 200 $Form.Text = ”My First Form with a working checkbox” # 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 your checkbox $checkbox1 = new-object System.Windows.Forms.checkbox $checkbox1.Location = new-object System.Drawing.Size(30,30) $checkbox1.Size = new-object System.Drawing.Size(250,50) $checkbox1.Text = "Enable/Disable OK button" $checkbox1.Checked = $true $Form.Controls.Add($checkbox1) # Add an OK button $OKButton = new-object System.Windows.Forms.Button $OKButton.Location = new-object System.Drawing.Size(130,100) $OKButton.Size = new-object System.Drawing.Size(100,40) $OKButton.Text = "OK" $OKButton.Add_Click({$Form.Close()}) $form.Controls.Add($OKButton) #Add a cancel button $CancelButton = new-object System.Windows.Forms.Button $CancelButton.Location = new-object System.Drawing.Size(255,100) $CancelButton.Size = new-object System.Drawing.Size(100,40) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$Form.Close()}) $form.Controls.Add($CancelButton) ########### This is the important piece ############## # # # Do something when the state of the checkbox changes # ####################################################### $checkbox1.Add_CheckStateChanged({ $OKButton.Enabled = $checkbox1.Checked }) # Activate the form $Form.Add_Shown({$Form.Activate()}) [void] $Form.ShowDialog() } #Call the function checkbox_test |
The most common usage I have seen for checkboxes is on a survey, or when you agree to a license agreement during a software install. Have fun playing with the controls and feel free to comment.
To add a logo to your form you can add this into your script
1 2 3 4 5 6 7 |
# add an image/logo $img = [System.Drawing.Image]::Fromfile('c:\temp4\ok_small.png') $pictureBox = new-object Windows.Forms.PictureBox $pictureBox.Location = new-object System.Drawing.Size(370,5) $pictureBox.Width = $img.Size.Width<br>$pictureBox.Height = $img.Size.Height $pictureBox.Image = $img $form.controls.add($pictureBox) |
Hi
this is the thing that i was looking, and appreciate your great job and wise. is there a way to add an logo.jpg?
any idea?
thanks in advance
Hi, I have added a section at the end of the page to include a logo.
Superb website you have here but I was curious if you knew of any community
forums that cover the same topics talked about here? I’d really love to be a part of community where I can get comments from other knowledgeable individuals that share the same interest.
If you have any recommendations, please let me know.
Kudos!
Ꮩery good website you have here but I was wanting to know if you knew of any community
foгums that cover the same topics talked about in tһis article?
I’d really like to be a part of online community
where I can get feedback from other experienced people that share the same interest.
If you have any recommendations, please lеt me know. Kudos!
Power Shell Windows forms forum”
http://www.scriptinganswers.com
Great, google took me stright here. thanks btw for this. Cheers!
i try to make a backuptool with folder copy. but the script starts to copy all folders before i can choose one of my 3 checkboxes (each for other folder). Pls help me, i read many in google… but not found anything for my problem
#######################################################
#$checkbox1.Add_CheckStateChanged({
#$OKButton.Enabled = $checkbox1.Checked })
If ($checkbox1.Checked = $true) { copy-item “C:\Users\Bernd PC\Links\” “D:\111\Links\” -recurse }
If ($checkbox2.Checked = $true) { copy-item “C:\Users\Bernd PC\Favoriten\” “D:\111\Favoriten\” -recurse }
Hi Bernhard,
Can you email me your full script – [email protected]
Thanks
you made my day.
Thanks a lot
Nice posts. Keep going.