DogManDave
DogManDave

Reputation: 17

Power Shell progress bar colour

I am trying to change the colour of the progress bar value but I can't seem to get it to work. The progress bar itself works but it is still in the default green. Any help would be greatly appreciated.

function  Add_ProgressBar ($name, $parent, $x, $y, $l, $h, $text){
    $object = New-Object System.Windows.Forms.progressbar
    $object.Location = New-Object System.Drawing.Point($x, $y)
    $object.Size = New-Object System.Drawing.size($l, $h)
    $object.Text = $text
    $object.Style  = 'Marquee'
    $object.ForeColor = 'Aqua'
    New-Variable $name -Value $object -Scope global
    (Get-Variable $parent).Value.Controls.Add((Get-Variable $name).Value)

}

Upvotes: 1

Views: 8740

Answers (1)

boeprox
boeprox

Reputation: 1868

If you are running this from the ISE, odds are that the VisualStyles is enabled...

[System.Windows.Forms.Application]::EnableVisualStyles()

...which is preventing your progress bar from being the color that you want it to be. I don't know of a good way to disable (or prevent this from being enabled in the ISE assuming it didn't break something needed). If you run your code from the console, it should look like what you are hoping it should be.

I ran this example from my console and it showed a black background with a red foreground when the button was clicked.

[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

$WinForm = new-object Windows.Forms.Form   
$WinForm.text = "ListBox Control"   
$WinForm.Size = new-object Drawing.Size(350,200)
$Button = new-object Windows.Forms.Button


$Script:Progressbar = New-Object System.Windows.Forms.progressbar
$Script:Progressbar.Location = New-Object System.Drawing.Point(0, 120)
$Script:Progressbar.Width = 350
$Script:Progressbar.Height = 15
$Script:Progressbar.Style  = 'Marquee'
$Script:Progressbar.ForeColor = 'Red'
$Script:Progressbar.BackColor='Black'
$Script:Progressbar.Style = 'Continuous'
$winform.controls.add($Script:Progressbar)
$winform.controls.add($button)

$WinForm.Add_Shown($WinForm.Activate()) 
$Button.Add_Click({
    1..$Script:Progressbar.Maximum | ForEach {
        $Script:Progressbar.Increment(1)
    }
}) 
$WinForm.showdialog() | out-null  

Upvotes: 2

Related Questions