首页 > 解决方案 > 将选项卡部分添加到 powershell gui

问题描述

我刚刚做了一个简单的powershell gui。您可以在下面找到代码。它运行良好。我如何向现有的 gui 添加一个选项卡,以便我有第二部分可以添加一些操作,例如另一个按钮后面有一个操作。

   $objForm                 = New-Object System.Windows.Forms.Form
$objForm.Text            = "testgui"
$objForm.Size            = New-Object System.Drawing.Size(350,680)
$objForm.BackColor       = "Black"
$objForm.StartPosition   = "CenterScreen"
$objForm.FormBorderStyle = 'Fixed3D'
$objForm.MaximizeBox     = $false
$objForm.KeyPreview      = $True
$objForm.Topmost         = $false

$installed_button           = New-Object system.windows.Forms.Button
$installed_button.Text      = "button 1"
$installed_button.ForeColor = "White"
$installed_button.backColor = "blue"
$installed_button.Cursor    = [System.Windows.Forms.Cursors]::Hand
$installed_button.Width     = 150
$installed_button.Height    = 40
$installed_button.Add_Click({
start-process 
})
$installed_button.location  = new-object system.drawing.point(10,50)
$obJForm.controls.Add($installed_button)

$installed_button           = New-Object system.windows.Forms.Button
$installed_button.Text      = "button 2"
#$installed_button.AutoSize  = $true
$installed_button.ForeColor = "White"
$installed_button.backColor = "red"
$installed_button.Cursor    = [System.Windows.Forms.Cursors]::Hand
$installed_button.Width     = 150
$installed_button.Height    = 40
$installed_button.Add_Click({
start-process 
})
$installed_button.location  = new-object system.drawing.point(180,50)
$obJForm.controls.Add($installed_button) 




$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

标签: powershell

解决方案


首先,不要对两个对象(按钮)使用相同的变量名!

有很多文章可以证明这一点,但至少您需要添加如下内容:

$objForm                 = New-Object System.Windows.Forms.Form
$objForm.Text            = "testgui"
$objForm.Size            = New-Object System.Drawing.Size(350,680)
$objForm.StartPosition   = "CenterScreen"
$objForm.FormBorderStyle = 'Fixed3D'
$objForm.MaximizeBox     = $false
$objForm.KeyPreview      = $True
$objForm.Topmost         = $false



# Tab master control
    $MainTab = New-Object System.Windows.Forms.TabControl
    $MainTab.Size = '300,620'
    $MainTab.Location = '10,15'
    $MainTab.Multiline = $true
    $MainTab.AutoSize = $true
    $MainTab.Anchor = 'Top,Left,Bottom,Right'

# Tab pages

    $TabPage1 = New-Object System.Windows.Forms.TabPage
    $Tabpage1.TabIndex = 1
    $Tabpage1.Text = 'My Tab 1'
    $TabPage1.Name = 'Tab1'

    $TabPage2 = New-Object System.Windows.Forms.TabPage
    $Tabpage2.TabIndex = 2
    $Tabpage2.Text = 'My Tab 2'
    $TabPage2.Name = 'Tab2'

# Add tabs to tab control
    $MainTab.Controls.AddRange(@($TabPage1,$TabPage2))


# Create Objects for Tab 1 and add to tab  


$installed_button           = New-Object system.windows.Forms.Button
$installed_button.Text      = "button 1"
$installed_button.ForeColor = "White"
$installed_button.backColor = "blue"
$installed_button.Cursor    = [System.Windows.Forms.Cursors]::Hand
$installed_button.Width     = 150
$installed_button.Height    = 40
$installed_button.Add_Click({
start-process 
})
$installed_button.location  = new-object system.drawing.point(10,50)


$installed_button2           = New-Object system.windows.Forms.Button
$installed_button2.Text      = "button 2"
#$installed_button.AutoSize  = $true
$installed_button2.ForeColor = "White"
$installed_button2.backColor = "red"
$installed_button2.Cursor    = [System.Windows.Forms.Cursors]::Hand
$installed_button2.Width     = 150
$installed_button2.Height    = 40
$installed_button2.Add_Click({
start-process 
})
$installed_button2.location  = new-object system.drawing.point(160,50)

$TabPage1.Controls.AddRange(@($installed_button,$installed_button2))

# Create Objects for Tab 2 and add to tab  


$installed_button3           = New-Object system.windows.Forms.Button
$installed_button3.Text      = "button 1"
$installed_button3.ForeColor = "White"
$installed_button3.backColor = "blue"
$installed_button3.Cursor    = [System.Windows.Forms.Cursors]::Hand
$installed_button3.Width     = 150
$installed_button3.Height    = 40
$installed_button3.Add_Click({
start-process 
})
$installed_button3.location  = new-object system.drawing.point(10,100)


$installed_button4           = New-Object system.windows.Forms.Button
$installed_button4.Text      = "button 2"
#$installed_button.AutoSize  = $true
$installed_button4.ForeColor = "White"
$installed_button4.backColor = "red"
$installed_button4.Cursor    = [System.Windows.Forms.Cursors]::Hand
$installed_button4.Width     = 150
$installed_button4.Height    = 40
$installed_button4.Add_Click({
start-process 
})
$installed_button4.location  = new-object system.drawing.point(160,100)

$TabPage2.Controls.AddRange(@($installed_button3,$installed_button4))



$obJForm.controls.Add($MainTab) 




$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

推荐阅读