# PowerShell GUI working with font size

**URL:** <https://forums.powershell.org/t/powershell-gui-working-with-font-size/11227>\
**Category:** PowerShell Help\
**Created:** [August 29, 2018, 2:46pm UTC](https://forums.powershell.org/t/powershell-gui-working-with-font-size/11227 "2018-08-29T14:46:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlogan3o13](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@jlogan3o13](https://forums.powershell.org/u/jlogan3o13)\
**Post date:** [August 29, 2018, 2:46pm UTC](https://forums.powershell.org/t/powershell-gui-working-with-font-size/11227/1 "2018-08-29T14:46:19Z")

</div>

I have a GUI wrapper I am working on for a larger script. On one input field, I have to accommodate text of varying lengths, and I am trying to think through how to do this. Rather than putting a great big input box on a line by itself in the GUI, when 90% of the time the text will be no more than 10 characters, I was playing around with doing something like this:

```
#region Storage Cluster=====
    $StorageLabel = New-Object System.Windows.Forms.Label
        $StorageLabel.Location = New-Object System.Drawing.Point(680, 190)
        $StorageLabel.AutoSize = $true
        $StorageLabel.Text = "Storage Cluster"
        $groupBoxVM.Controls.Add($StorageLabel)

    $StorageInput = New-Object System.Windows.Forms.TextBox
        $StorageInput.Location = New-Object System.Drawing.Point(665, 220)
        $StorageInput.Size = New-Object System.Drawing.Size(270, 20)
        $StorageInput.TextAlign = 'Center'
        $StorageInput.add_TextChanged({
            if($StorageInput.Text.Length -gt 21) {
                $StorageInput.Font = New-Object System.Drawing.Font("Arial", 12)
            } 
            if ($StorageInput.Text.Length -ge 31) {
                $StorageInput.Font = New-Object System.Drawing.Font("Arial", 8)
            }
            if ($StorageInput.Text.Length -le 21) {
                $StorageInput.Font = New-Object System.Drawing.Font("Arial", 14)
            }
        })
        $groupBoxVM.Controls.Add($StorageInput)
#endregion Storage Cluster=====
```

So if the text length goes above 21, the input field font goes from Arial 14 to Arial 12. If the text length gets really long, the font goes from 12 to 8. If the user changes their mind and removes characters down to 21, the font goes back to 14. This is all working just fine.

The problem is the input field itself. Whenever the font changes, the GUI redraws the field smaller. So from a height of 20 it goes down to 15 after 21 characters, and down to about 10 after 31. If I erase characters down to 21, the height value returns to 20.

I have tried specifying the Size again after each change in font, and have tried setting AutoSize both to $true and to $false, but no joy. Just curious if anyone has run into this before, and knows of a way around it.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [August 29, 2018, 3:36pm UTC](https://forums.powershell.org/t/powershell-gui-working-with-font-size/11227/2 "2018-08-29T15:36:16Z")

</div>

This isn’t really a Powershell question, this is .NET forms behavior. It’s probably some auto-scaling that is causing the behavior that you are seeing. See this:

> **[ContainerControl.AutoScaleMode Property (System.Windows.Forms)](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.containercontrol.autoscalemode?redirectedfrom=MSDN&amp%3Bview=netframework-4.7.2&view=windowsdesktop-6.0)**
>
> Gets or sets the automatic scaling mode of the control.

If you set it to DPI, then it’s not supposed to change the control size. You can also see if dock or anchor makes the control work as expected:

> **[Manage WinForm controls using the Anchor and Dock properties](https://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/)**
>
> Many beginning WinForms developers have trouble keeping the controls on their forms uniform and organized. Often the controls look fine at design time, but when the form is resized at runtime the controls either lose their positions or don't...

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/powershell-gui-working-with-font-size/11227/3 "2024-05-16T20:34:32Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


