We help IT Professionals succeed at work.
Get Started

Powershell form - Anchor a button to a dynamically sized groupbox

Last Modified: 2017-04-11
I have a Powershell form with a dynamically sized groupbox (GrowOnly).

Can anyone tell me  how to anchor a button to the bottom of the group box?

Alternatively, how can I anchor the button so it is always at the bottom of the form? (the button is basically a close form button)

Here is a basic form I was using to practice on -

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

$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(900,700)
$Form.WindowState = "Normal"
$Form.StartPosition = "CenterScreen"
$Form.AutoSizeMode = "GrowAndShrink"

$GroupBox1x = 10
$GroupBox1 = New-Object System.Windows.Forms.GroupBox
$GroupBox1.Location = New-Object System.Drawing.Size($GroupBox1x,0)
$GroupBox1.Size = New-Object System.Drawing.Size @(700,30)
$GroupBox1.AutoSize = $true
$GroupBox1.AutoSizeMode = "GrowOnly"
$GroupBox1.Text = "Group Box 1"

$Labelx = 10
$labely = 30
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size($Labelx,$labely)
$Label1.Text = "Test1"
$Label1.Size = New-Object System.Drawing.Size(85,15)

$labely += 20
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size($Labelx,$labely)
$Label2.Text = "Test 2"
$Label2.Size = New-Object System.Drawing.Size(85,15)

$ButtonRestart = New-Object System.Windows.Forms.Button
$ButtonRestart.Size = New-Object System.Drawing.Size(70,30)
$ButtonRestart.Text = "Restart"


$Form.ShowDialog() | Out-Null

Open in new window

Watch Question
Senior Citrix Engineer
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE