I guess the last item you could just use a static number (40 if the $cellsize 
is 50) since you know your length is 10 based on your substring parameter…

($RDS_Server.SubString(0,10)).PadRight(40," ")

Anyway, I think you get the general idea.

Matt

From: Kelley, Matthew
Sent: Tuesday, September 30, 2014 5:10 PM
To: powershell@lists.myitforum.com
Cc: ntsys...@lists.myitforum.com
Subject: RE: [powershell] Formatting a ListBox

When you fill your listbox, I would use the padright with spaces. Subtract the 
length of the item you are putting into each spot from a constant number you 
will use for the length of each “field”. Something like this:
$cellSize = 50
$TheListEntry      = $RDS_User.PadRight($cellSize - $RDS_User.Length," ") + "| 
" + $RDS_Status.PadRight($cellSize - $RDS_Status.Length," ") + "; " +  
$RDS_Server.SubString(0,10).PadRight($cellSize - 
$RDS_Server.SubString(0,10).Length," ")

That will get you a set width for each item, left justified essentially. Of 
course, you will have to determine the static cell size that works for you 
based on the size of the variables you get back. That should get you what you 
want. You may have to tweak the last item ($RDS_Server) by adding some 
parenthesis, I did not test the code.

Matt


From: listsad...@lists.myitforum.com<mailto:listsad...@lists.myitforum.com> 
[mailto:listsad...@lists.myitforum.com] On Behalf Of Michael Leone
Sent: Tuesday, September 30, 2014 4:33 PM
To: powershell@lists.myitforum.com<mailto:powershell@lists.myitforum.com>
Cc: ntsys...@lists.myitforum.com<mailto:ntsys...@lists.myitforum.com>
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
**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be 
used for urgent or sensitive issues 

================================================
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