Question:
I am writing some PowerShell code to use for later projects. This is a list where the user selects an item from the list, and it assigns the selection to a variable. I am unsure of how to control the font size, specifically for the list box text.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# Creates a window that prompts a user to select an item from a list #Enables .NET Framework Classes Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # Creates the window prompt $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Select an Item" $objForm.Size = New-Object System.Drawing.Size(600,500) $objForm.StartPosition = "CenterScreen" # Defines keystrokes as inputs # # Sets Enter to set highlighted item to a variable # Sets Esc to close windowed prompt # $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$objListBox.SelectedItem;$objForm.Close()}}) $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) # Creates the OK button for the window $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(75,300) $OKButton.Size = New-Object System.Drawing.Size(100,35) $OKButton.Text = "OK" $OKButton.Add_Click({$x=$objListBox.SelectedItem;$ObjForm.Close()}) $objForm.Controls.Add($OKButton) # Creates the cancel button for the window $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(200,300) $CancelButton.Size = New-Object System.Drawing.Size(100,35) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) # Adds the label text $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,40) $objLabel.Size = New-Object System.Drawing.Size(400,50) $objLabel.Text = "Please Select an Item" $objForm.Controls.Add($ObjLabel) # Creates the empty List box $objListBox = New-Object System.Windows.Forms.ListBox $objListBox.Location = New-Object System.Drawing.Size(10,100) $objListBox.Size = New-Object System.Drawing.Size(500,300) $objListBox.Height = 200 # Adds items to the list box # Can call items from file # # Example : Get-Content C:\Scripts\Test.txt | ForEach-Object {[void] $objListBox.Items.Add($_)} # [void] $objListBox.Items.Add("one") [void] $objListBox.Items.Add("two") [void] $objListBox.Items.Add("three") [void] $objListBox.Items.Add("four") [void] $objListBox.Items.Add("five") $objForm.Controls.Add($objListBox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() $x |
Answer:
Here is the MSDN for the ListBox Class. There is a property called Font. On the MSDN page for font can see all of the constructors or ways to make a Font Object. In this example, this is the one I used.
1 2 3 4 5 6 7 |
#Creates the empty List box $objListBox = New-Object System.Windows.Forms.ListBox $objListBox.Location = New-Object System.Drawing.Size(10,100) $objListBox.Size = New-Object System.Drawing.Size(500,300) $objListBox.Height = 200 $objListBox.Font = New-Object System.Drawing.Font("Lucida Console",12,[System.Drawing.FontStyle]::Regular) |