PowerShell GUI working with font size

Welcome Forums General PowerShell Q&A PowerShell GUI working with font size

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #110407

    Participant
    Points: 26
    Rank: Member

    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.

  • #110422

    Participant
    Points: 351
    Helping Hand
    Rank: Contributor

    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:

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.containercontrol.autoscalemode?redirectedfrom=MSDN&view=netframework-4.7.2#System_Windows_Forms_ContainerControl_AutoScaleMode

    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:

    https://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/

The topic ‘PowerShell GUI working with font size’ is closed to new replies.