2

I'm trying to put a text on a progress bar (percentage, etc) but it's not working. Progress bar text is based on this. Below is a simplified version of the code.

#Form
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size (604,430)
$Form.Text = "Move User Files"
$Form.StartPosition = "CenterScreen"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"

#progres bar
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Name = 'ProgressBar'
$progressBar.Value = 0
$progressBar.Style = "Continuous"
$progressBar.Location = New-Object System.Drawing.Size (4,357)
$progressBar.Size = New-Object System.Drawing.Size (580,30)
$Form.Controls.Add($progressBar)

#This is the part that is not working
$gr = $progressBar.CreateGraphics()
$progressBarText = '0%'
$Font = new-object System.Drawing.Font("Bauhaus 93", 30, "Bold", "Pixel")
$Brush = New-Object Drawing.SolidBrush([System.Drawing.Color]::Black)
$PointF = [System.Drawing.PointF]::new($progressBar.Width /2 - ($gr.MeasureString($progressBarText,$Font).Width / 2),
    $progressBar.Height /2 - ($gr.MeasureString($progressBarText,$Font).Height / 2))
$gr.DrawString($progressBarText, $Font, $Brush, $PointF)

#Show The Form
$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()

I Don't receive any error, but it simply doesn't show the text. What am I missing? any thoughts?

ldg
  • 21
  • 1
  • 3

3 Answers3

3

Shouldn't you be setting the text property on the progressbar

$progressBarText should be $progressBar.Text

Sid
  • 2,586
  • 1
  • 11
  • 22
  • Thank you for your help. Unfortunately I have already tried that and didn't work. In fact the [Documentation](https://msdn.microsoft.com/en-us/library/6fetw5dd(v=vs.110).aspx) for System.Windows.Forms tells that "This API supports the product infrastructure and is not intended to be used directly from your code.". That's why I'm trying to translate into PowerShell the example from CodeProject.com that I mentioned in my post. – ldg Jul 04 '18 at 14:40
  • Have you tried Sapien PowerShell Studio? Easiest way to make GUIs in PowerShell. They have a Progressbar as well as a ProgressbarOverlay (I think thats what they called it). There is pretty good documentation on how to get this done on their community sites. – Sid Jul 04 '18 at 19:34
1

is there some reason the one in PowerShell won't work for you? Here's a snippet from a real script that I use multiple times per day. You might be able to tweak it to your needs. I realize it's not a GUI but it is 100% PowerShell.

       try {
           "Your Secret" | clip
            1..$Delay | % {
                if (-not ( [console]::KeyAvailable) ) {
                    write-host "$($_)`r" -NoNewline
                    Write-Progress -Status "Press Any Key to continue" `
                        -Activity "Paste password before it is removed from the clipboard" `
                        -PercentComplete ($_ * 100 / $Delay)
                    Start-Sleep -Seconds 1
                }
            }
        } finally {
            $null | clip

            if ([console]::KeyAvailable) {
                $x = [console]::ReadKey()
                Write-Information -MessageData $x -Tags "KeyStroke"
            }
        }

(How you really get a secure password into the clipboard is a separate task left to the reader.)

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43
  • You can use the `-Status` parameter to include some custom text such as the percent complete. – No Refunds No Returns Jul 04 '18 at 01:24
  • Thank you for answering, but I don't think this is related to my question. I do lots of powershell scripts and they work just fine. My only problem is being to make this C# code translated into PowerShell. – ldg Jul 04 '18 at 14:31
  • This is a powershell progress bar. Your question was how to make GUI progress bar work in powershell. I understand it's not GUI as stated in my response. But others may find this answer useful. – No Refunds No Returns Jul 04 '18 at 15:53
0

How I did it and it worked.

I put it at the beginning of my script like this:

$Font = New-Object System.Drawing.Font('Arial', 10, 'Bold', 'Pixel')
$brush1 = New-Object Drawing.SolidBrush([System.Drawing.Color]::Black)
$PBCG = $ProgressBar1.CreateGraphics()

I did a function that writes, like this:

function UpdateText([String] $text){
    $x = ($ProgressBar1.Width / 2) - ([int]$PBCG.MeasureString($text, $Font).Width / 2)
    $y = ($ProgressBar1.Height / 2) - ([int]$PBCG.MeasureString($text, $Font).Height / 2)
    $PointF = [System.Drawing.PointF]::new($x, $y)
    $PBCG.DrawString($text, $Font, $brush1, $PointF)
}

I hope it helped you

David Buck
  • 3,752
  • 35
  • 31
  • 35