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?

3 Answers 3

3

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

$progressBarText should be $progressBar.Text

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. Unfortunately I have already tried that and didn't work. In fact the Documentation 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.
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.
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.)

3 Comments

You can use the -Status parameter to include some custom text such as the percent complete.
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.
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.