Help with PowerShell Form MinimumSize Issue

0
1
Asked By CreativeTurtle42 On

Hey everyone! I'm having a bit of trouble with my PowerShell form script. When I set `$form.MinimumSize = "700,900"`, it automatically resets to "700,814". For example:
```powershell
$formRenameUser.MinimumSize = "700,900"
```
And the output shows `{Width=700, Height=814}`. However, if I set it to "700,300", it keeps that value:
```powershell
$formRenameUser.MinimumSize = "700,300"
```
And the output is `{Width=700, Height=300}`. The same thing happens if I try "1000,1000"; it changes to "1000,814". It's like there's a minimum height limit for the MinimumSize parameter. Can anyone help me solve this? Thanks a lot! Here's the complete function for context:

```powershell
# Main Form
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$w = 700
$padding = 20
$formRenameUser = New-Object Windows.Forms.Form
$formRenameUser.Width = $w
$formRenameUser.StartPosition = "CenterScreen"
$formRenameUser.Font = "Consolas,12"
$formRenameUser.Topmost = $true
$formRenameUser.ShowIcon = $False
$formRenameUser.AutoSizeMode = "GrowAndShrink"
$formRenameUser.Text = "RENOMMER UN UTILISATEUR"

##############################
BUTTONS, LABEL, TEXTBOX, ....
##############################

# Display form
$lastFormY = $btnCancel
$spaceY = 60
$formRenameUser.Height = $(($btnCancel.Location.Y + $btnCancel.Height + $spaceY))
$formRenameUser.MinimumSize = "700,$($formRenameUser.Height)"

# DEBUG
write-host $formRenameUser.Size
write-host $formRenameUser.MinimumSize
# DEBUG

$formRenameUser.Controls.AddRange(@($lblTitle,$lblSearch,$tbxSearch,$dgvUsersList,$lblDetails,$lblGivenName,$tbxGivenName,$lblSurname,$lblDisplay,$tbxDisplayName,$lblMail,$tbxMail,$chkKeepAlias,$lblPass,$tbxPass,$btnTmpPass,$btnStrongPass,$chkPass,$lblInfos,$btnCancel,$btnOk))
$formRenameUser.ShowDialog() | Out-Null
```

2 Answers

Answered By DevNinja123 On

It looks like you're running into a type issue with how you're passing the size. You're using a string for **MinimumSize**, which is probably causing it to be treated incorrectly. Instead of:
```powershell
$form.MinimumSize = "700,900"
```
Try using:
```powershell
$form.MinimumSize = 700,900
```
This way, it will treat it as numbers, not a string. Another solution could be to instantiate a new Size object:
```powershell
$form.MinimumSize = New-Object System.Drawing.Size (700,900)
```
That should fix your issue!

Answered By CodeGuru88 On

When you set **MinimumSize = 700x900**, that's the entire window size, but Windows automatically accounts for the title bar and borders, leaving you with a client area height of only 814. This means if you try to set any height below or equal to 814, it won't go lower since that's the enforced minimum size. So, setting it to 300 keeps it at 814 because it's already below that threshold.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.