Reputation: 64
I have a piece of PowerShell code that shows the progress. The progress bar is not updated correctly. It starts with 0% but it should start with for example 17%. Also, when it is finished, the progress bar is not finished. So the layout is not 100% accurate.
The progress bar has not the correct length.
What is wrong with the code?
# =============================================================================================================================================
# Add assemblies.
# =============================================================================================================================================
Add-Type -AssemblyName System.Windows.Forms
# =============================================================================================================================================
# Define objects
# =============================================================================================================================================
$frmTest = New-Object 'System.Windows.Forms.Form'
$pgbProgress = New-Object 'System.Windows.Forms.ProgressBar'
$lblPackage = New-Object 'System.Windows.Forms.Label'
# =============================================================================================================================================
# Form and show the progressbar. Close with the 'X' at the upper right corner.
# =============================================================================================================================================
$frmTest.Controls.Add($pgbProgress)
$frmTest.Controls.Add($lblPackage)
$frmTest.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$frmTest.AutoScaleMode = 'Font'
$frmTest.ClientSize = New-Object System.Drawing.Size(528, 220)
$frmTest.Name = 'frmTest'
$frmTest.Text = 'Form'
$frmTest.add_Shown({
$arrPackages = @("IrfanView","7Zip","WinZIP","Office","PhotoShop","PaintShop")
$numPackages = $arrPackages.Count
$numCounter = 1
ForEach($Package in $arrPackages)
{
[int]$Percentage = ($numCounter / $numPackages) * 100
$lblPackage.Text = "Processing: $Package ($numCounter of $numPackages packages - $Percentage%"
$pgbProgress.Value = $Percentage
$pgbProgress.PerformStep()
Sleep -Seconds 4
$numCounter++
}
Sleep -Seconds 5
[void]$frmTest.Close()
[void]$frmTest.Dispose()
})
$pgbProgress.Location = New-Object System.Drawing.Point(13, 165)
$pgbProgress.Name = 'pgbProgress'
$pgbProgress.Size = New-Object System.Drawing.Size(503, 43)
$pgbProgress.Style = 'Continuous'
$lblPackage.AutoSize = $True
$lblPackage.Location = New-Object System.Drawing.Point(13, 137)
$lblPackage.Name = 'lblPackage'
$lblPackage.Size = New-Object System.Drawing.Size(38, 13)
$lblPackage.Text = 'Processing all the packages...'
[void]$frmTest.ShowDialog()
Any help is appreciated.
With kind regards, Willem-Jan
Upvotes: 2
Views: 1124
Reputation: 3246
Okay, I think Windows Forms is a little flaky, but despite my comment above, it seems to be more reliable if you don't set the value, but set the Step instead, because you are moving the bar a set figure each time. This then uses PerformStep()
to increase the bar. Doing both i.e. setting the value and using PerformStep()
was confusing matters.
Like this:
Add-Type -AssemblyName System.Windows.Forms
$frmTest = New-Object 'System.Windows.Forms.Form'
$pgbProgress = New-Object 'System.Windows.Forms.ProgressBar'
$lblPackage = New-Object 'System.Windows.Forms.Label'
$frmTest.Controls.Add($pgbProgress)
$frmTest.Controls.Add($lblPackage)
$frmTest.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$frmTest.AutoScaleMode = 'Font'
$frmTest.ClientSize = New-Object System.Drawing.Size(528, 220)
$frmTest.Name = 'frmTest'
$frmTest.Text = 'Form'
$frmTest.add_Shown( {
$arrPackages = @("IrfanView", "7Zip", "WinZIP", "Office", "PhotoShop", "PaintShop")
$numPackages = $arrPackages.Count
$numCounter = 1
# Set the step value.
$pgbProgress.Step = ($numCounter / $numPackages) * 100
ForEach ($Package in $arrPackages) {
[int]$Percentage = ($numCounter / $numPackages) * 100
$lblPackage.Text = "Processing: $Package ($numCounter of $numPackages packages - $Percentage%"
$pgbProgress.PerformStep()
Sleep -Seconds 2
$numCounter++
}
Sleep -Seconds 2
[void]$frmTest.Close()
[void]$frmTest.Dispose()
})
$pgbProgress.Location = New-Object System.Drawing.Point(13, 165)
$pgbProgress.Name = 'pgbProgress'
$pgbProgress.Size = New-Object System.Drawing.Size(503, 43)
$pgbProgress.Style = 'Continuous'
$lblPackage.AutoSize = $True
$lblPackage.Location = New-Object System.Drawing.Point(13, 137)
$lblPackage.Name = 'lblPackage'
$lblPackage.Size = New-Object System.Drawing.Size(38, 13)
$lblPackage.Text = 'Processing all the packages...'
[void]$frmTest.ShowDialog()
Upvotes: 3