From the looks of your screenshot, the font property is not properly being 
applied. You can see that font is not fixed width since the letters do not line 
up between the 2 rows. Try using “Courier New” or “Consolas”.

PowerShell Studio (http://www.sapien.com/software/powershell_studio) is great 
if you are working with forms.

Gavin

From: [email protected] [mailto:[email protected]] On 
Behalf Of Michael Leone
Sent: Tuesday, September 30, 2014 1:33 PM
To: [email protected]
Cc: [email protected]
Subject: [powershell] Formatting a ListBox

I am not a .Net programmer. But I came across a script online that I find 
useful, and now I am trying to modify it a bit. What the script does is put a 
listbox on a form; the listbox is populated with an entry I am trying to 
construct (by which I mean - I am concatenating 3 string variables together, 
and adding that as the list entry.

Here's the problem: The onscreen formatting needs to be a fixed width, and what 
I am ending up with looks like this:
[Inline image 1]

See how it's wandering? I want a fixed font onscreen, and I can't seem to 
figure out how to get it to do that. I am padding the individual components to 
the widths I want, but it still shows onscreen like that.

Here's the code snippets:

$FormWidth    = 300         # 300
$FormHeight   = 500         # 320

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select user(s)"
$objForm.Size = New-Object System.Drawing.Size($FormWidth,$FormHeight)
$objForm.StartPosition = "CenterScreen"

## ----- Make the ListBox -----
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Name = "List of RDS Users"
$ListBoxLocationWidth   = $FormWidth * 0.03
$ListBoxLocationHeight  = $FormHeight * 0.09
$objListBox.Location    = New-Object 
System.Drawing.Size($ListBoxLocationWidth,$ListBoxLocationHeight)
$ListBoxSizeWidth  = $FormWidth * 0.93
$ListBoxSizeHeight = $FormHeight * 0.05
$objListBox.Size   = New-Object 
System.Drawing.Size($ListBoxSizeWidth,$ListBoxSizeHeight)
$objListBox.Height = $FormHeight * 0.41
$objListBox.ScrollAlwaysVisible   = $true
$objListBox.Font   = "Courier Regular"
$objListBox.SelectionMode = "MultiExtended"


Function FillListBox
{
$LoggedOnUsers = Get-RDUserSession -ConnectionBroker "$connectionBroker" 
-CollectionName "$SessionHostCollection" | Sort-Object -Property UserName
ForEach ($user in $LoggedOnUsers)
{
  $RDS_User = "{0,-10}" -f $user.UserName.PadRight(10)
  SWITCH ($user.SessionState)
  {
    "STATE_CONNECTED"   { $RDS_Status = "ACTIVE      "}
    "STATE_DISCONNECTED"     { $RDS_Status = "DISCONNECTED"}
   }
 $RDS_Server  = [string] $user.ServerName
 $TheListEntry = $RDS_User + "| " + $RDS_Status + "; " +  
$RDS_Server.SubString(0,10)
 [void] $objListBox.Items.Add($TheListEntry)
}

I'm sure it's simple, but it's eluding my fried brain today.
Clues, anyone?

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to