Little King
Little King

Reputation: 1120

PowerShell Forms - Vertical Progress Bar

This is regarding Windows Forms in PowerShell and the System.Windows.Forms.ProgressBar.

I am looking all over and cannot find anything that allows the progress bar to fill vertically. I've considered alternatives (such as resizing a label with a background color), but I prefer to use something that already has a class if possible. I could have sworn I had seen something out there like this before, even if it wasn't a true progress bar. I am not using it to track progress, but more for CPU usage, RAM usage, and drive space for server statuses. This is for a useful GUI for a quick report of servers from a dropdown list, something where I don't have to open another complete shell session (I have enough shells open as it is between O365 eDiscovery, data analysis, and other needs). Thanks for any suggestions in advance.

Upvotes: 3

Views: 910

Answers (1)

Jan Chrbolka
Jan Chrbolka

Reputation: 4464

Here is a very good C# answer How do I make a winforms progress bar move vertically in C#?

It overrides the CreateParams method to set the PBS_VERTICAL flag in Style. To make it work in PowerShell you will unfortunately have to use a bit of C# code.

This works for me:

$type = @'
using System; 
using System.Windows.Forms; 

public class VerticalProgressBar : ProgressBar { 
  protected override CreateParams CreateParams { 
    get { 
      CreateParams cp = base.CreateParams; 
      cp.Style |= 0x04; 
      return cp; 
    } 
  } 
}
'@

Add-Type -TypeDefinition $type -ReferencedAssemblies System.Drawing,System.Data,System.Windows.Forms

$userForm = New-Object System.Windows.Forms.Form
$userForm.Text = "$title"
$userForm.Size = New-Object System.Drawing.Size(230,300)
$userForm.StartPosition = "CenterScreen"
$userForm.AutoSize = $False
$userForm.MinimizeBox = $False
$userForm.MaximizeBox = $False
$userForm.SizeGripStyle= "Hide"
$userForm.WindowState = "Normal"
$userForm.FormBorderStyle="Fixed3D"
$progressbar = New-Object 'VerticalProgressBar'
$progressbar.Location = New-Object System.Drawing.Point(180, 50); 
$progressbar.Width = 20
$progressbar.Height = 200
$userForm.Controls.Add($progressbar)

$TrackBar = New-Object 'System.Windows.Forms.TrackBar'
$TrackBar.Location = New-Object System.Drawing.Point(10, 10); 
$TrackBar.Width = 200
$TrackBar.add_ValueChanged({$progressbar.Value = $this.value*10})
$userForm.Controls.Add($TrackBar)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,220)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$userForm.Close()})
$userForm.Controls.Add($OKButton)


$userForm.ShowIcon = $False
$userForm.Add_Shown({$userForm.Activate()})
$userForm.AcceptButton = $OKButton
[void] $userForm.ShowDialog()

Upvotes: 2

Related Questions