Powershell GUI 选项卡式布局脚本放置

问题描述

我写了六个小的 gui 脚本,我想将它们放在一个文件中,以便使用 ps2exe 将所述文件转换为 rxecutable。

我在堆栈上找到了一个脚本,here 非常适合我的需求。不幸的是,我在选项卡中找不到有关脚本放置的任何信息,而 MS 文档将我引导至 ISE 选项卡,这没有帮助。

说我想放置这个

Add-Type -AssemblyName PresentationFramework

    $button_click = {
        $folder = $textBox1.Text;
        
          $pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?','','4');
           If($pytanie -eq 'Yes')
           {Remove-Item –path $folder –recurse -Force};
           $test = Test-Path $folder;
           if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety','0')}}
    
    $label2 = New-Object System.Windows.Forms.Label
    $label2.AutoSize = $True
    $label2.Text = ("Scieżka")
    $label2.Location = New-Object System.Drawing.Point (10,30)
    $label2.Size = New-Object System.Drawing.Size (25,70)
    $label2.Font = [System.Drawing.Font]::new("Arial",10,[System.Drawing.FontStyle]::Bold)
    
    $textBox1 = New-Object System.Windows.Forms.TextBox
    $textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text Box
    $textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text Box
    $textBox1.Multiline = $false ### Allows multiple lines of data
    $textBox1.Font = New-Object System.Drawing.Font("Consolas",[System.Drawing.FontStyle]::Regular)
    $textBox1.ReadOnly=$false
    
    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Point(10,120)
    $button.Size = New-Object System.Drawing.Size (200,30)
    $button.Text = "Usun Folder"
    $button.Add_Click($button_click)
    
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Mapowanie' ### Text to be displayed in the title
    $form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
    $form.StartPosition = 'Manual'
    $form.Location      = '10,10'
    $form.Topmost = $true  ### Optional - Opens on top of other windows
    
    
    $form.Controls.AddRange(@($textBox1,$button,$label2))
    
    
    
    $form.ShowDialog()

标签中。怎么做?

解决方法

我认为是

$Tab1.Controls.Add($button)

您正在寻找。

例如

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false 
$ApplicationForm.Size = "800,600"

$FormTabControl = New-object System.Windows.Forms.TabControl 
$FormTabControl.Size = "755,475" 
$FormTabControl.Location = "25,75" 
$ApplicationForm.Controls.Add($FormTabControl)

$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0 
$Tab1.UseVisualStyleBackColor = $True 
$Tab1.Name = "Tab1" 
$Tab1.Text = "Tab1” 
$FormTabControl.Controls.Add($Tab1)

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)


$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0 
$Tab2.UseVisualStyleBackColor = $True 
$Tab2.Name = "Tab2" 
$Tab2.Text = "Tab2” 
$FormTabControl.Controls.Add($Tab2)

$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)

# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()