简体   繁体   English

如何在Powershell GUI中从其他运行空间添加标签?

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

In my Powershell GUI I have two mode : connection and deconnection. 在我的Powershell GUI中,我有两种模式:连接和断开连接。 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. 该列表是从xml中读取的,然后作为每个步骤的标签加载到flowlayout面板中。 So If I have 8 steps for connection, I will have 8 labels created in the flowlayout panel. 因此,如果我有8个连接步骤,则将在flowlayout面板中创建8个标签。

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 如果我处于连接模式并传递到断开连接模式,则需要从功能运行区在flowlayout面板中加载相关步骤。

In my script I have three runspace (one for GUI, one for function and one for a timer). 在我的脚本中,我有三个运行空间(一个用于GUI,一个用于功能,一个用于计时器)。

In the Gui runspace, PanelLabelInner (the flowlayoutpanel) is wrapped into a PanelLabelOuter. 在Gui运行空间中,PanelLabelInner(flowlayoutpanel)被包装到PanelLabelOuter中。 This is for vertical centering. 这是用于垂直居中。 I need to add the labels into PanelLabelInner. 我需要将标签添加到PanelLabelInner中。

I need to finalize BUiltXML function. 我需要完成BUiltXML函数。 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 ? 首先,在模式更改时删除所有标签,然后更新ui以显示新标签:我该怎么做?

With this code, nothing is added, I suppose I must use something like update, refresh... 有了这段代码,什么也没添加,我想我必须使用诸如update,refresh之类的东西。

Gui runspace : gui运行空间:

$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')
}

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()
        } 
    )

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM