This is a fairly simple thing, if you know what you’re doing or if you have done it before. But if, like me, you are at a complete lost and sick of searching only to find half complete or even completely contradictory answers, then this is the guide for you.
My goals for this GUI was to have the form resize IF a tick box was $true (ticked)
First I started off created a blank form with a single checkbox item, you can see this below:
$form = New-Object System.Windows.Forms.Form $form.Text = "New User Form" $form.Size = New-Object System.Drawing.Size(500,540) $form.StartPosition = "CenterScreen" $form.FormBorderStyle = "Fixed3D" $form.MaximizeBox = $false $form.MinimizeBox = $false $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Point(165,470) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $OKButton $form.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Point(245,470) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $CancelButton $form.Controls.Add($CancelButton) $checkbox = New-Object System.Windows.Forms.CheckBox $checkbox.Location = New-Object System.Drawing.Point(100,100) $checkbox.Size = New-Object System.Drawing.Size(100,25) $checkbox.Text = "Test Checkbox" $form.controls.Add($checkbox) $result = $form.ShowDialog()
That will create an empty form like the one below:
Now I will add the following code after all the objects but before the end $result variable which displays the form:
$checkbox.add_checkstatechanged({ if ($checkbox.Checked -eq $True){ $form.size = new-object system.drawing.size(800,540) }else{ $form.size = new-object system.drawing.size(500,540) } })
this is the part that will make the form larger if the checkbox is checked and return it to the original size if it isn’t checked. You can do SO many other things as well. For example, having a textbox enabled or disabled, changing the forms title, changing the colour of the form and to be honest just about anything you can think of.
This is the complete script you will need:
$form = New-Object System.Windows.Forms.Form $form.Text = "New User Form" $form.Size = New-Object System.Drawing.Size(500,540) $form.StartPosition = "CenterScreen" $form.FormBorderStyle = "Fixed3D" $form.MaximizeBox = $false $form.MinimizeBox = $false $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Point(165,470) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $OKButton $form.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Point(245,470) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $CancelButton $form.Controls.Add($CancelButton) $checkbox = New-Object System.Windows.Forms.CheckBox $checkbox.Location = New-Object System.Drawing.Point(100,100) $checkbox.Size = New-Object System.Drawing.Size(100,25) $checkbox.Text = "Test Checkbox" $form.controls.Add($checkbox) $checkbox.add_checkstatechanged({ if ($checkbox.Checked -eq $true){ $form.Size = New-Object System.Drawing.Size(800,540) }else{ $form.Size = New-Object System.Drawing.Size(500,540) } }) $result = $form.ShowDialog()
This script will create a form like in the screenshots below. In this screenshot the checkbox is not ticked:
And in this screenshot the checkbox has been ticked, making the form size noticeably wider:
Hope this helps someone struggling with getting started with some of the more complex WinForm tricks. Enjoy and comment if you need any help!
Hey I know this is off topic but I was wondering if you knew of any widgets I could
add to my blog that automatically tweet mmy newest
twitter updates. I’ve been looking for a plug-in like ths
ffor quite some time and was hoping maybe you would have some experience with something like this.
Plerase let me know if you run into anything. I truly enjoy reading youhr blog and I lokk forfward to your new updates