Search code examples
user-interfacepowershellhashtablerunspace

How to add label from a different runspace in a Powershell GUI?


In my Powershell GUI I have two mode : connection and deconnection. For each there is a list of step. This list is read from a xml and then load into a flowlayout panel, as a label for each step. So If I have 8 steps for connection, I will have 8 labels created in the flowlayout panel.

I wish to change labels dynamically when my mode change. If I'm in connection mode and pass to the deconnection mode, I need to load the related steps in the flowlayout panel, from the function runspace

In my script I have three runspace (one for GUI, one for function and one for a timer).

In the Gui runspace, PanelLabelInner (the flowlayoutpanel) is wrapped into a PanelLabelOuter. This is for vertical centering. I need to add the labels into PanelLabelInner.

I need to finalize BUiltXML function. At first, delete all's the labels when a mode change, and then update the ui to show the new labels : how I can do that ?

With this code, nothing is added, I suppose I must use something like update, refresh...

Gui runspace :

$CommonHashTable.PanelLabelOuter=New-Object System.Windows.Forms.Panel
$CommonHashTable.PanelLabelOuter.BackColor = [string]$PanelLabelOuterCfg.BackColor
$CommonHashTable.PanelLabelOuter.Name ="PanelLabelOuter"
$CommonHashTable.PanelLabelOuter.BorderStyle =[string]$PanelLabelOuterCfg.BorderStyle
$CommonHashTable.PanelLabelOuter.Dock = "Fill"
$CommonHashTable.PanelLabelOuter.AutoSize = $false
$CommonHashTable.MiddleLayout.Controls.Add($CommonHashTable.PanelLabelOuter,2,0)

$CommonHashTable.PanelLabelInner=New-Object System.Windows.Forms.FlowLayoutPanel
$CommonHashTable.PanelLabelInner.AutoSize = $false
$CommonHashTable.PanelLabelInner.Height = $CommonHashTable.c*20
$CommonHashTable.PanelLabelInner.Left = [Int32]$PanelLabelInnerCfg.Left
$CommonHashTable.PanelLabelInner.Width= $CommonHashTable.PanelLabelOuter.Width
$top=[int32](($CommonHashTable.PanelLabelOuter.Size.Height -  $CommonHashTable.PanelLabelInner.Size.Height) / 2)
$CommonHashTable.PanelLabelInner.Top=$top
$CommonHashTable.PanelLabelInner.Padding= 0
$CommonHashTable.PanelLabelInner.Anchor = 'None'
$CommonHashTable.PanelLabelInner.FlowDirection = "TopDown"
$CommonHashTable.PanelLabelInner.WrapContents = $false
$CommonHashTable.PanelLabelInner.BackColor = [string]$PanelLabelInnerCfg.BackColor
$CommonHashTable.PanelLabelInner.Name ="PanelLabelInner"
$CommonHashTable.PanelLabelOuter.Controls.Add($CommonHashTable.PanelLabelInner)

Function runspace

function BuiltXML{
        Switch ($CommonHashTable.Phase) {
                    {$CommonHashTable.Phase -eq "Connect"}
                        {
                                $ConnectLabelText = "Connection"
                                $CommonHashTable.sourceXML = [xml](Get-Content $ProductPath\Xml\ConnectionLabels.xml)

                        }

                    {$CommonHashTable.Phase -eq "Disconnect"}
                        {
                                $ConnectLabelText = "Logout"
                                $CommonHashTable.sourceXML = [xml](Get-Content $ProductPath\Xml\DeconnectionLabels.xml)
                        }
        }
        $CommonHashTable.steps= $CommonHashTable.sourceXML.Dialer.Steps.Stp
        $CommonHashTable.c = $CommonHashTable.steps.count
        $CommonHashTable.PanelLabelInner.Invoke([Action[string]] {
            $i =1
                #$CommonHashTable.PanelLabelInner.Controls.Remove($CommonHashTable.Lbl)
                $CommonHashTable.Lbl.Controls.Clear()
                foreach ($e in $CommonHashTable.steps) 
                {
                    $CommonHashTable.Lbl = New-Object System.Windows.Forms.Label
                    $CommonHashTable.Lbl.Size=New-Object System.Drawing.Size($CommonHashTable.PanelLabelInner.Size.Width,20) 
                    $CommonHashTable.Lbl.AutoSize = $false
                    $CommonHashTable.Lbl.Name = "Label"+$i
                    $CommonHashTable.Lbl.TextAlign = "MiddleLeft"
                    $CommonHashTable.Lbl.Text = $e.Label
                    $CommonHashTable.PanelLabelInner.Controls.Add($CommonHashTable.Lbl)
                    $i++
                }
            },
        'normal')
}

Solution

  • I've found a solution... the function also built the flowlayout panel in relation to the number of labels. So I have a flowlayout panel with the good height. I only need to clear the outer panel when I need to switch, add theses labels and then do an update.

    function BuiltXML{
        Switch ($CommonHashTable.Phase) {
                    {$CommonHashTable.Phase -eq "Connect"}
                        {
                                $CommonHashTable.ConnectLabel.Text = "Connexion"
                                $sourceXML = [xml](Get-Content $ProductPath\Xml\ConnectionLabels.xml)
    
                        }
    
                    {$CommonHashTable.Phase -eq "Disconnect"}
                        {
                                $CommonHashTable.ConnectLabel.Text = "Deconnexion"
                                $sourceXML = [xml](Get-Content $ProductPath\Xml\DeconnectionLabels.xml)
                        }
        }
    
         $etapes = $sourceXML.Dialer.Etapes.Etape 
         $c = $etapes.count
    
         $CommonHashTable.PanelLabelOuter.Invoke(
    
         [Action] {
            $CommonHashTable.PanelLabelOuter.Controls.Clear()
                $CommonHashTable.PanelLabelInner=New-Object System.Windows.Forms.FlowLayoutPanel
                $CommonHashTable.PanelLabelInner.AutoSize = $false
                $CommonHashTable.PanelLabelInner.Height = $c*20
                $CommonHashTable.PanelLabelInner.Left = [Int32]$PanelLabelInnerCfg.Left
                $CommonHashTable.PanelLabelInner.Width= $CommonHashTable.PanelLabelOuter.Width
                $top=[int32](($CommonHashTable.PanelLabelOuter.Size.Height -  $CommonHashTable.PanelLabelInner.Size.Height) / 2)
                $CommonHashTable.PanelLabelInner.Top=$top
                $CommonHashTable.PanelLabelInner.Padding= 0
                $CommonHashTable.PanelLabelInner.Anchor = 'None'
                $CommonHashTable.PanelLabelInner.FlowDirection = "TopDown"
                $CommonHashTable.PanelLabelInner.WrapContents = $false
                $CommonHashTable.PanelLabelInner.BackColor = [string]$PanelLabelInnerCfg.BackColor
                $CommonHashTable.PanelLabelInner.Name ="PanelLabelInner"
                $CommonHashTable.PanelLabelOuter.Controls.Add($CommonHashTable.PanelLabelInner)
    
                $i =1
                foreach ($e in $etapes) 
                {
                    $CommonHashTable.Lbl = New-Object System.Windows.Forms.Label
                    $CommonHashTable.Lbl.Size=New-Object System.Drawing.Size($CommonHashTable.PanelLabelInner.Size.Width,20) 
                    $CommonHashTable.Lbl.AutoSize = $false
                    $CommonHashTable.Lbl.Name = "Label"+$i
                    $CommonHashTable.Lbl.TextAlign = "MiddleLeft"
                    #$CommonHashTable.Lbl.Font = New-Object System.Drawing.Font([string]$lblCfg.Font,[int32]$lblCfg.Size,[System.Drawing.FontStyle]::[string]$lblCfg.Style)
                    #$CommonHashTable.Lbl.ForeColor = [string]$lblCfg.Color
                    $CommonHashTable.Lbl.Text = $e.Label
                    $CommonHashTable.PanelLabelInner.Controls.Add($CommonHashTable.Lbl)
                    $i++
                }
                    $CommonHashTable.PanelLabelInner.update()
            } 
        )
    

    }