Home > database >  powershell progressbar in groupbox GUI
powershell progressbar in groupbox GUI

Time:03-03

I'm working on a script but for some reason my ProgressBar won't appear in my groupbox. I tried a lot of different things (like making the groupbox trensparent) but nothing really work. Here is my code 'simplified' with just the problematic parts.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()


$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = ‘345,160’
$Form.Text = " bug progressbar "
$Form.StartPosition = "CenterScreen"


#Functions

Function BDDchoice(){

$BDD = $args[0]

$Label2.Text = "it takes a few seconds"


$ProgressBarcheck = New-Object System.Windows.Forms.ProgressBar
$ProgressBarcheck.Location = New-Object System.Drawing.Point(230,49)
$ProgressBarcheck.Size = New-Object System.Drawing.Size(98, 23)
$ProgressBarcheck.Style = "Marquee"
$ProgressBarcheck.MarqueeAnimationSpeed = 20
$ButtonNEXT1.Hide()

$Form.Controls.Add($ProgressBarcheck);


$job1 ={

sleep 10

$bdd_min = $args[0]       

echo $bdd_min

        }

Start-Job -ScriptBlock $job1 -ArgumentList $BDD -Name bdd
do { [System.Windows.Forms.Application]::DoEvents() } until ((Get-Job -Name bdd).State -eq "Completed")
Wait-Job -Name bdd
$resultat = Receive-Job -Name bdd -Keep


Get-Job | Remove-Job -Force

$TextBoxBDD.text = $resultat

$ProgressBarcheck.Hide()
$ButtonNEXT1.Visible = $true
$Label2.Text = "BDD:"
}

#variables 
    $BDDXS   = @('SCHEM1','SCHEM2','SCHEM3','SCHEM4')
    $BDD1 = 'BDD1'
    $BDD2 = 'BDD2'
    $BDD3 = 'BDD3'
    $BDD4 = 'BDD4'
    $BDD = $null

#groupbox

    $GroupBoxCREATE = New-Object System.Windows.Forms.GroupBox
    $GroupBoxCREATE.Location = New-Object System.Drawing.Point(10,5)
    $GroupBoxCREATE.Width = 325
    $GroupBoxCREATE.Height = 150
    $GroupBoxCREATE.Text = " Create " 


#combobox ZONE

    $comboBoxTRIG = New-Object System.Windows.Forms.ComboBox
    $comboBoxTRIG.Location = New-Object System.Drawing.Point(25, 50)
    $comboBoxTRIG.Size = New-Object System.Drawing.Size(200, 200)
        foreach($BDDX in $BDDXS)
                                    {
                $comboBoxTRIG.Items.add($BDDX)
                }
    $comboBoxTRIG.Text = "REGION"
    $comboBoxTRIG.AutoCompleteMode = "SuggestAppend"
    $comboBoxTRIG.AutoCompleteSource = "ListItems"
    $comboBoxTRIG.SelectedIndex ="0"


#butons
    $ButtonNEXT1 = New-Object System.Windows.Forms.Button
    $ButtonNEXT1.Location = New-Object System.Drawing.Point(230,49)
    $ButtonNEXT1.Size = New-Object System.Drawing.Size(98, 23)
    $ButtonNEXT1.Text = "OK"
    $ButtonNEXT1.add_Click({

    
    $zone = $comboBoxTRIG.SelectedItem.ToString()

        if ($zone -like "SCHEM1"){
            $BDD = $BDD1 
                                          }
        if ($zone -like "SCHEM2") {
            $BDD = $BDD2
                                                      }
        if ($zone -like "SCHEM3")  {
            $BDD = $BDD3 
                                         }
        if ($zone -like "SCHEM4") {
            $BDD = $BDD4 
                                         }



    
    BDDchoice($BDD)
                  })




#labels

    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Point(25,25)
    $Label1.Text = "REGION :"

    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.Point(25,80)
    $label2.Width = 200
    $Label2.Text = "BDD :"
    

#textbox    

    $TextBoxBDD = New-Object System.Windows.Forms.TextBox
    $TextBoxBDD.Location = New-Object System.Drawing.Point(25,100)
    $TextBoxBDD.Width = 200
    $TextBoxBDD.ReadOnly = $true
    $TextBoxBDD.Text = ""
    $TextboxBDD.BackColor = "white"

#list form's butons/lists/combobox

$Form.Controls.AddRange(@($TextBoxBDD))
$Form.controls.AddRange(@($Label1,$Label2))
$Form.controls.AddRange(@($ButtonNEXT1))
$Form.controls.AddRange(@($comboBoxTRIG))
$Form.controls.AddRange(@($GroupBoxCREATE))
$Form.ShowDialog() 

i'm interested in any idea. My coworkers could not find anything but I must admit we are far from expert (i'm still learning and so are they).

CodePudding user response:

$Form.Controls.Add($ProgressBarcheck);

It looks like you're adding Progress Bar to your form, NOT the groupbox. Add progress bar to groupbox then add groupbox to form.

$GroupBoxCREATE.Controls.Add($ProgressBarcheck);
$Form.Controls.Add($GroupBoxCREATE);
  • Related