Avatar of Richard Gardner
Richard Gardner
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Powershell form - Anchor a button to a dynamically sized groupbox

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"
$ButtonRestart.Add_Click({funcRestart})

$Form.Controls.Add($GroupBox1)
$GroupBox1.Controls.Add($Label1)
$GroupBox1.Controls.Add($Label2)
$Form.Controls.Add($ButtonRestart)

$Form.ShowDialog() | Out-Null

Open in new window

.NET ProgrammingPowershell

Avatar of undefined
Last Comment
Richard Gardner

8/22/2022 - Mon