【问题标题】:How to build a single form powershell gui application如何构建单一形式的 powershell gui 应用程序
【发布时间】:2020-11-08 18:45:15
【问题描述】:

我想知道如何做一个 gui powershell 应用程序,它允许用户在不使用模式对话框而只使用父对话框的情况下浏览表单。 例如,我有一个包含按钮和文本框的表单 A。 当用户单击按钮但不打开子对话框时,我想显示另一个表单让我们说 B

其实我成功的从A的形式到B的形式。

我真正想要做的是转到第三种表单,让我们通过单击按钮从表单 BC

我似乎遗漏了一些东西,不幸的是我找不到能说明如何做到这一点的人。

这是代码示例。

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size( 300, 300 )
$button = New-Object System.Windows.Forms.Button
$button.Text = "gotoB"
$button.Location = New-Object System.Drawing.Point( 200, 200 )
$button.Size = New-Object System.Drawing.Size( 70, 25 )
$button.Add_Click( {
        $form.Controls.Remove( $button )
        $form.Update()            
        LoadScreen2 -Form $form
    })
$form.Controls.Add( $button )
$form.ShowDialog()

function LoadScreen2 () {
    [cmdletbinding()] param 
    (
        [Parameter(Mandatory = $True)]
        [System.Windows.Forms.Form]$Form
    )
    $bbutton = New-Object System.Windows.Forms.Button
    $bbutton.Text = "gotoC"
    $bbutton.Location = New-Object System.Drawing.Point( 200, 200 )
    $bbutton.Size = New-Object System.Drawing.Size( 70, 25 )        
    $bbutton.Add_Click({
            $Form.Controls.Remove($bbutton)                
            LoadScreen3 -Form $Form
    })       
    $Form.Controls.Add($bbutton)
    $Form.Update()
}

function LoadScreen3 () {
    [cmdletbinding()] param 
    (
        [Parameter(Mandatory = $True)]
        [System.Windows.Forms.Form]$Form
    )        
    $cbutton = New-Object System.Windows.Forms.Button
    $cbutton.Text = "goNowhere"
    $cbutton.Location = New-Object System.Drawing.Point( 200, 200 )
    $cbutton.Size = New-Object System.Drawing.Size( 70, 25 )
    $cbutton.Add_Click({})
    $Form.Controls.Add($cbutton)
    $Form.Update()              
}

【问题讨论】:

  • 我认为你想要的是标签。这... TabControl 控件- Windows 窗体.NET 框架| Microsoft Docs — docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/…
  • 感谢您回复李戴利。实际上,我不想要标签。为了便于理解,我特意简化了我的代码。
  • 这不是 PowerShell 代码问题。这是一个 UX/UI 设计问题。所以,真的是跑题了。我过去在 VBA/VB/VB.Net/C# 等中做过很多这样的事情。您可以只使用包含不同表单元素的容器(GroupBox),然后利用 SendToBack/BringToFront 或可见性(隐藏/显示)动作。然而,这又不是 PowerShell 问题,因为使用任何 UX/UI 编程语言作为设计目标都会遇到同样的问题。

标签: forms powershell user-interface dialog modal-dialog


【解决方案1】:

我终于找到了如何做到这一点。 在调用 LoadScreen3 函数之前,我必须添加以下行:

$Form.Controls.Clear()

【讨论】:

    【解决方案2】:

    从我的评论扩展。因此,只需使用分组框(而不是说您对选项卡式界面选项的反感),但最终还是一样的。

    您需要做的就是将您选择的任何元素添加到每个单独的组框中,它们都将包含在单独的组框中。

    同样,这都是 UX/UI 代码,无论出于其他原因涉及的编程语言如何,都可以正常工作。您的基线 UX/UI 应该可以正常工作。后面的代码是针对其他数据、字符串、互操作需求的。

    $SingleFormUXUI = New-Object -TypeName System.Windows.Forms.Form
    
    [System.Windows.Forms.GroupBox]$grpMainPage    = $null
    [System.Windows.Forms.GroupBox]$grpDataPage    = $null
    [System.Windows.Forms.GroupBox]$grpDetailsPage = $null
    
    [System.Windows.Forms.Label]$lblMainPage       = $null
    [System.Windows.Forms.Label]$lblFormPage       = $null
    
    [System.Windows.Forms.ComboBox]$cbxSelectPage  = $null
    
    
    function InitializeComponent
    {
        $grpMainPage    = (New-Object -TypeName System.Windows.Forms.GroupBox)
        $grpDataPage    = (New-Object -TypeName System.Windows.Forms.GroupBox)
        $grpDetailsPage = (New-Object -TypeName System.Windows.Forms.GroupBox)
        $cbxSelectPage  = (New-Object -TypeName System.Windows.Forms.ComboBox)
        $lblMainPage    = (New-Object -TypeName System.Windows.Forms.Label)
        $lblFormPage    = (New-Object -TypeName System.Windows.Forms.Label)
    
        $grpMainPage.SuspendLayout()
    
        $SingleFormUXUI.SuspendLayout()
        #
        #grpMainPage
        #
        $grpMainPage.Controls.Add($lblMainPage)
        $grpMainPage.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]11,[System.Int32]12))
        $grpMainPage.Name     = [System.String]'grpMainPage'
        $grpMainPage.Size     = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]587,[System.Int32]325))
        $grpMainPage.TabIndex = [System.Int32]1
        $grpMainPage.TabStop  = $false
        $grpMainPage.Text     = [System.String]'MainPage'
        $grpMainPage.UseCompatibleTextRendering = $true
        $grpMainPage.Visible  = $false
        #
        #grpDataPage
        #
        $grpDataPage.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]12))
        $grpDataPage.Name     = [System.String]'grpDataPage'
        $grpDataPage.Size     = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]586,[System.Int32]325))
        $grpDataPage.TabIndex = [System.Int32]2
        $grpDataPage.TabStop  = $false
        $grpDataPage.Text     = [System.String]'DataPage'
        $grpDataPage.UseCompatibleTextRendering = $true
        $grpDataPage.Visible  = $false
        #
        #grpDetailsPage
        #
        $grpDetailsPage.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]12))
        $grpDetailsPage.Name     = [System.String]'grpDetailsPage'
        $grpDetailsPage.Size     = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]586,[System.Int32]325))
        $grpDetailsPage.TabIndex = [System.Int32]3
        $grpDetailsPage.TabStop  = $false
        $grpDetailsPage.Text     = [System.String]'DetailsPage'
        $grpDetailsPage.UseCompatibleTextRendering = $true
        $grpDetailsPage.Visible  = $false
        #
        #cbxSelectPage
        #
        $cbxSelectPage.FormattingEnabled = $true
        $cbxSelectPage.Items.AddRange(
            [System.Object[]]@([System.String]'Main Page',
            [System.String]'Data Page',
            [System.String]'DetailsPage Page')
        )
    
        $cbxSelectPage.Location          = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]444,[System.Int32]338))
        $cbxSelectPage.Name              = [System.String]'cbxSelectPage'
        $cbxSelectPage.Size              = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]153,[System.Int32]21))
        $cbxSelectPage.TabIndex          = [System.Int32]4
        #
        #lblMainPage
        #
        $lblMainPage.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]7,[System.Int32]17))
        $lblMainPage.Name     = [System.String]'lblMainPage'
        $lblMainPage.Size     = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]295,[System.Int32]19))
        $lblMainPage.TabIndex = [System.Int32]0
        $lblMainPage.Text     = [System.String]'Use the dropdown box to select a page to work with.'
        $lblMainPage.UseCompatibleTextRendering = $true
    
        $cbxSelectPage.Add_TextChanged(
        {
            switch($cbxSelectPage.Text)
            {
                'Main Page'    
                {
                    $grpMainPage.Visible    = $true
                    $grpMainPage.BackColor  = [System.Drawing.ColorTranslator]::FromHtml("#4a90e2")
                    $grpDataPage.Visible    = $false
                    $grpDetailsPage.Visible = $false
                }
                'Data Page'    
                {
                    $grpDataPage.Visible    = $true
                    $grpDataPage.BackColor  = [System.Drawing.ColorTranslator]::FromHtml("#50e3c2")
                    $grpMainPage.Visible    = $false
                    $grpDetailsPage.Visible = $false
                }
                'DetailsPage Page' 
                {
                    $grpDetailsPage.Visible   = $true
                    $grpDetailsPage.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#9b9b9b")
                    $grpMainPage.Visible      = $false
                    $grpDataPage.Visible      = $false
                }
            }
        })
    
        #
        #SingleFormUXUI
        #
        $SingleFormUXUI.ClientSize = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]599,[System.Int32]375))
        #
        #lblFormPage
        #
        $lblFormPage.Location                   = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]10,[System.Int32]338))
        $lblFormPage.Name                       = [System.String]'lblFormPage'
        $lblFormPage.Size                       = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]333,[System.Int32]19))
        $lblFormPage.TabIndex                   = [System.Int32]0
        $lblFormPage.Text                       = [System.String]'Welcome to my UX/UI. Use the dropdown box to select a page.'
        $lblFormPage.UseCompatibleTextRendering = $true
        #
        $SingleFormUXUI.Controls.AddRange(
            @(
                $lblFormPage,
                $grpMainPage,
                $grpDataPage,
                $grpDetailsPage,
                $cbxSelectPage
            )
        )
        $SingleFormUXUI.Text = [System.String]'Single from MDI UX/UI'
    
        $grpMainPage.ResumeLayout($false)
        $SingleFormUXUI.ResumeLayout($false)
    
        Add-Member -InputObject $SingleFormUXUI -Name grpMainPage -Value $grpMainPage -MemberType NoteProperty
        Add-Member -InputObject $SingleFormUXUI -Name lblMainPage -Value $lblMainPage -MemberType NoteProperty
        Add-Member -InputObject $SingleFormUXUI -Name grpDataPage -Value $grpDataPage -MemberType NoteProperty
        Add-Member -InputObject $SingleFormUXUI -Name grpDetailsPage -Value $grpDetailsPage -MemberType NoteProperty
        Add-Member -InputObject $SingleFormUXUI -Name cbxSelectPage -Value $cbxSelectPage -MemberType NoteProperty
    
        $SingleFormUXUI.ShowDialog()
    }
    . InitializeComponent
    

    【讨论】:

      猜你喜欢
      • 2013-08-30
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      相关资源
      最近更新 更多