none
Running ProgressBar on Powershell-GUI (System.Windows.Forms) when Form loaded

    Question

  • Hey guys

    So I created a GUI using Windows.Forms and added a ProgressBar and a Button. Actually I want the ProgressBar to start running just after loading the Form, but it didn't work (I tried it using the Form.Shown Event, see '# DOESN'T WORK' Section below). So I added a Button and added the Code to run the Progressbar to the Button.Click Event.

    How can I make the ProgressBar start running just after the Form finished loading?

    My code:

    # Load Assemblies
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    # Init Form
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 400
    $Form.height = 600
    $Form.Text = "Add Resource"
    
    # Init ProgressBar
    $pbrTest = New-Object System.Windows.Forms.ProgressBar
    $pbrTest.Maximum = 100
    $pbrTest.Minimum = 0
    $pbrTest.Location = new-object System.Drawing.Size(10,10)
    $pbrTest.size = new-object System.Drawing.Size(100,50)
    $i = 0
    $Form.Controls.Add($pbrTest)
    
    # Button
    $btnConfirm = new-object System.Windows.Forms.Button
    $btnConfirm.Location = new-object System.Drawing.Size(120,10)
    $btnConfirm.Size = new-object System.Drawing.Size(100,30)
    $btnConfirm.Text = "Start Progress"
    $Form.Controls.Add($btnConfirm)
    
    # Button Click Event to Run ProgressBar
    $btnConfirm.Add_Click({
        
        While ($i -le 100) {
            $pbrTest.Value = $i
            Start-Sleep -m 1
            "VALLUE EQ"
            $i
            $i += 1
        }
    })
    
    # Show Form
    $Form.Add_Shown({$Form.Activate()})
    $Form.ShowDialog()
    
    # DOESN'T WORK: Run ProgressBar after Form loaded
    $Form.Add_Shown({
        
        While ($i -le 5) {
            $pbrTest.Value = $i
            Start-Sleep -s 1
            "VALLUE EQ"
            $i
            $i += 1
        }
    })
    

    Any Help'd b appreciated.

    Thx so far.

    Thursday, April 19, 2012 1:31 PM

Answers

  • Function StartProgressBar
    {
    	if($i -le 5){
    	    $pbrTest.Value = $i
    	    $script:i += 1
    	}
    	else {
    		$timer.enabled = $false
    	}
    	
    }
    
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 400
    $Form.height = 600
    $Form.Text = "Add Resource"
    
    # Init ProgressBar
    $pbrTest = New-Object System.Windows.Forms.ProgressBar
    $pbrTest.Maximum = 100
    $pbrTest.Minimum = 0
    $pbrTest.Location = new-object System.Drawing.Size(10,10)
    $pbrTest.size = new-object System.Drawing.Size(100,50)
    $i = 0
    $Form.Controls.Add($pbrTest)
    
    # Button
    $btnConfirm = new-object System.Windows.Forms.Button
    $btnConfirm.Location = new-object System.Drawing.Size(120,10)
    $btnConfirm.Size = new-object System.Drawing.Size(100,30)
    $btnConfirm.Text = "Start Progress"
    $Form.Controls.Add($btnConfirm)
    
    $timer = New-Object System.Windows.Forms.Timer 
    $timer.Interval = 1000
    
    $timer.add_Tick({
    StartProgressBar
    })
    
    $timer.Enabled = $true
    $timer.Start()
    
    # Button Click Event to Run ProgressBar
    $btnConfirm.Add_Click({
        
        While ($i -le 100) {
            $pbrTest.Value = $i
            Start-Sleep -m 1
            "VALLUE EQ"
            $i
            $i += 1
        }
    })
    
    # Show Form
    $Form.Add_Shown({$Form.Activate()})
    $Form.ShowDialog()

    Thursday, April 19, 2012 2:58 PM
  • Hi,

    The below link is about Timer class, it should be helpful to understand the script:

    Timer Class

    http://msdn.microsoft.com/en-us/library/xdh6857z.aspx

    The class has a method named add_Tick and a property named interval, used in the script.

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Friday, April 20, 2012 7:49 AM
    Moderator

All replies

  • Function StartProgressBar
    {
    	if($i -le 5){
    	    $pbrTest.Value = $i
    	    $script:i += 1
    	}
    	else {
    		$timer.enabled = $false
    	}
    	
    }
    
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 400
    $Form.height = 600
    $Form.Text = "Add Resource"
    
    # Init ProgressBar
    $pbrTest = New-Object System.Windows.Forms.ProgressBar
    $pbrTest.Maximum = 100
    $pbrTest.Minimum = 0
    $pbrTest.Location = new-object System.Drawing.Size(10,10)
    $pbrTest.size = new-object System.Drawing.Size(100,50)
    $i = 0
    $Form.Controls.Add($pbrTest)
    
    # Button
    $btnConfirm = new-object System.Windows.Forms.Button
    $btnConfirm.Location = new-object System.Drawing.Size(120,10)
    $btnConfirm.Size = new-object System.Drawing.Size(100,30)
    $btnConfirm.Text = "Start Progress"
    $Form.Controls.Add($btnConfirm)
    
    $timer = New-Object System.Windows.Forms.Timer 
    $timer.Interval = 1000
    
    $timer.add_Tick({
    StartProgressBar
    })
    
    $timer.Enabled = $true
    $timer.Start()
    
    # Button Click Event to Run ProgressBar
    $btnConfirm.Add_Click({
        
        While ($i -le 100) {
            $pbrTest.Value = $i
            Start-Sleep -m 1
            "VALLUE EQ"
            $i
            $i += 1
        }
    })
    
    # Show Form
    $Form.Add_Shown({$Form.Activate()})
    $Form.ShowDialog()

    Thursday, April 19, 2012 2:58 PM
  • Hi,

    The below link is about Timer class, it should be helpful to understand the script:

    Timer Class

    http://msdn.microsoft.com/en-us/library/xdh6857z.aspx

    The class has a method named add_Tick and a property named interval, used in the script.

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Friday, April 20, 2012 7:49 AM
    Moderator