Centering a window after loading

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
rmckay9969
Posts: 63
Joined: Fri Oct 30, 2015 4:24 pm

Centering a window after loading

Post by rmckay9969 » Mon Sep 26, 2016 10:02 pm

I realize that this is generally a Q & A based forum, however I thought it would be helpful to give a solution I came across. I have a program that loads all the filenames in a directory needing a place holder value replaced with real world data. For example a filename containing a milepost of 100.XX would need the fractional portion added once verified. This program loads the full path of the file into a datagrid and once the place holder of XX is replaced with actual data, it renames the file in the directory. Simple enough, but I have a major gripe about re-sizing a form manually to show the full length of text it contains. And a form that is not centered after programmatically resizing just does not look very nice. This solution although possibly crude provides both resizing to fit contents and centering to the main form. Hopefully useful to somebody as I was not able to find a good example and had to do some research to make a powershell/winforms result.
  1. $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
  2.  
  3.     $font = New-Object System.Drawing.Font("Verdana", 9, [System.Drawing.FontStyle]::Bold)
  4.  
  5.     $Width = 0
  6.  
  7.     $Height = 0
  8.  
  9.     if ($foldername.ShowDialog() -eq "OK") {
  10.  
  11.         $Count = (Get-ChildItem $foldername.SelectedPath -Filter *.txt | Measure-Object).Count     
  12.  
  13.         $list = foreach ($_ in Get-ChildItem $foldername.SelectedPath -Filter *.txt) {
  14.  
  15.             $Text = $_.FullName
  16.  
  17.             # Get the length in pixels of the text string based off of font & Size
  18.  
  19.             $SizeW = [System.Windows.Forms.TextRenderer]::MeasureText([string]$Text, $font).Width
  20.  
  21.             # Get the Height in pixels of the text string based off of font & Size
  22.  
  23.             $SizeH = [System.Windows.Forms.TextRenderer]::MeasureText([string]$Text, $font).Height
  24.  
  25.             $Width = $Width + $SizeW
  26.  
  27.             $Height = $Height + $SizeH + 2 # A little more padding if needed
  28.  
  29.             New-Object PsObject -Property @{ HeaderName = $_.FullName }
  30.  
  31.         }
  32.  
  33.         # Average the text lengths and add some padding
  34.  
  35.         $form1.Width = $Width / $Count + 200
  36.  
  37.         # A little more padding if needed
  38.  
  39.         $form1.Height = $Height + 150
  40.  
  41.     }
  42.  
  43.     # Center the form to the main form
  44.  
  45.     $form1.Left = $mainform.Left + (0.5 * $mainform.Width) - (0.5 * $form1.Width)
  46.  
  47.     $form1.Top = $mainform.Top + (0.5 * $mainform.Height) - (0.5 * $form1.Height)

Locked