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.
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
rmckay9969
Posts: 63
Last visit: Fri Feb 01, 2019 6:51 pm

Centering a window after loading

Post by rmckay9969 »

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.     $font = New-Object System.Drawing.Font("Verdana", 9, [System.Drawing.FontStyle]::Bold)
  3.     $Width = 0
  4.     $Height = 0
  5.     if ($foldername.ShowDialog() -eq "OK") {
  6.         $Count = (Get-ChildItem $foldername.SelectedPath -Filter *.txt | Measure-Object).Count     
  7.         $list = foreach ($_ in Get-ChildItem $foldername.SelectedPath -Filter *.txt) {
  8.             $Text = $_.FullName
  9.             # Get the length in pixels of the text string based off of font & Size
  10.             $SizeW = [System.Windows.Forms.TextRenderer]::MeasureText([string]$Text, $font).Width
  11.             # Get the Height in pixels of the text string based off of font & Size
  12.             $SizeH = [System.Windows.Forms.TextRenderer]::MeasureText([string]$Text, $font).Height
  13.             $Width = $Width + $SizeW
  14.             $Height = $Height + $SizeH + 2 # A little more padding if needed
  15.             New-Object PsObject -Property @{ HeaderName = $_.FullName }
  16.         }
  17.         # Average the text lengths and add some padding
  18.         $form1.Width = $Width / $Count + 200
  19.         # A little more padding if needed
  20.         $form1.Height = $Height + 150
  21.     }
  22.     # Center the form to the main form
  23.     $form1.Left = $mainform.Left + (0.5 * $mainform.Width) - (0.5 * $form1.Width)
  24.     $form1.Top = $mainform.Top + (0.5 * $mainform.Height) - (0.5 * $form1.Height)
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked