如何使用 PowerShell 创建多个按钮? [英] How to create multiple button with PowerShell?

查看:56
本文介绍了如何使用 PowerShell 创建多个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建多个按钮.该按钮有50多个按钮.但我不想像我下面所做的那样一一创建.所有的按钮功能,大小,都是一样的.只是名称不同.任何人都可以帮助我,并给出一个想法.非常感谢.

I would like to create multiple buttons. The button more than 50 buttons. But I do not want to create it one by one like what I did below. All the button function, size, are the same. Only the name is different. Anyone can help me please, and give an idea. Thank you very much.

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(973,1177)
$Form.text                       = "Form"
$Form.TopMost                    = $false
$Form.BackColor                  = [System.Drawing.ColorTranslator]::FromHtml("#07326c")

$Groupbox3                       = New-Object system.Windows.Forms.Groupbox
$Groupbox3.height                = 269
$Groupbox3.width                 = 430
$Groupbox3.location              = New-Object System.Drawing.Point(262,825)

$Button9                         = New-Object system.Windows.Forms.Button
$Button9.text                    = "1B"
$Button9.width                   = 39
$Button9.height                  = 29
$Button9.location                = New-Object System.Drawing.Point(30,51)
$Button9.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button9.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button3                         = New-Object system.Windows.Forms.Button
$Button3.text                    = "Reserve"
$Button3.width                   = 87
$Button3.height                  = 30
$Button3.location                = New-Object System.Drawing.Point(134,215)
$Button3.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button3.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$TextBox1                        = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline              = $false
$TextBox1.width                  = 256
$TextBox1.height                 = 20
$TextBox1.location               = New-Object System.Drawing.Point(135,46)
$TextBox1.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$TextBox2                        = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline              = $false
$TextBox2.width                  = 258
$TextBox2.height                 = 20
$TextBox2.location               = New-Object System.Drawing.Point(134,96)
$TextBox2.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Name"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(21,48)
$Label1.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Label1.ForeColor                = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Label2                          = New-Object system.Windows.Forms.Label
$Label2.text                     = "Line ID/Phone"
$Label2.AutoSize                 = $true
$Label2.width                    = 25
$Label2.height                   = 10
$Label2.location                 = New-Object System.Drawing.Point(18,147)
$Label2.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Label2.ForeColor                = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Label3                          = New-Object system.Windows.Forms.Label
$Label3.text                     = "Temperature"
$Label3.AutoSize                 = $true
$Label3.width                    = 25
$Label3.height                   = 10
$Label3.location                 = New-Object System.Drawing.Point(18,96)
$Label3.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Label3.ForeColor                = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$TextBox3                        = New-Object system.Windows.Forms.TextBox
$TextBox3.multiline              = $false
$TextBox3.width                  = 260
$TextBox3.height                 = 20
$TextBox3.location               = New-Object System.Drawing.Point(134,146)
$TextBox3.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Button4                         = New-Object system.Windows.Forms.Button
$Button4.text                    = "Export"
$Button4.width                   = 85
$Button4.height                  = 30
$Button4.location                = New-Object System.Drawing.Point(309,215)
$Button4.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button4.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Label4                          = New-Object system.Windows.Forms.Label
$Label4.AutoSize                 = $true
$Label4.width                    = 25
$Label4.height                   = 10
$Label4.location                 = New-Object System.Drawing.Point(91,57)
$Label4.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Groupbox2                       = New-Object system.Windows.Forms.Groupbox
$Groupbox2.height                = 461
$Groupbox2.width                 = 350
$Groupbox2.location              = New-Object System.Drawing.Point(556,132)
$Groupbox2.BackColor             = [System.Drawing.ColorTranslator]::FromHtml("#dff9f2")

$Button10                        = New-Object system.Windows.Forms.Button
$Button10.text                   = "2B"
$Button10.width                  = 39
$Button10.height                 = 29
$Button10.location               = New-Object System.Drawing.Point(94,51)
$Button10.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button10.BackColor              = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button11                        = New-Object system.Windows.Forms.Button
$Button11.text                   = "3B"
$Button11.width                  = 39
$Button11.height                 = 29
$Button11.location               = New-Object System.Drawing.Point(158,51)
$Button11.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button11.BackColor              = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button12                        = New-Object system.Windows.Forms.Button
$Button12.text                   = "4B"
$Button12.width                  = 39
$Button12.height                 = 29
$Button12.location               = New-Object System.Drawing.Point(223,52)
$Button12.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button12.BackColor              = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button13                        = New-Object system.Windows.Forms.Button
$Button13.text                   = "5B"
$Button13.width                  = 39
$Button13.height                 = 29
$Button13.location               = New-Object System.Drawing.Point(287,52)
$Button13.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button13.BackColor              = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Label5                          = New-Object system.Windows.Forms.Label
$Label5.text                     = "RIGHT SIDE"
$Label5.AutoSize                 = $true
$Label5.width                    = 25
$Label5.height                   = 10
$Label5.location                 = New-Object System.Drawing.Point(558,109)
$Label5.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Label5.ForeColor                = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Label6                          = New-Object system.Windows.Forms.Label
$Label6.text                     = "LEFT SIDE"
$Label6.AutoSize                 = $true
$Label6.width                    = 25
$Label6.height                   = 10
$Label6.location                 = New-Object System.Drawing.Point(58,106)
$Label6.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Label6.ForeColor                = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "1A"
$Button1.width                   = 40
$Button1.height                  = 28
$Button1.location                = New-Object System.Drawing.Point(88,181)
$Button1.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button1.ForeColor               = [System.Drawing.ColorTranslator]::FromHtml("#000000")
$Button1.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button7                         = New-Object system.Windows.Forms.Button
$Button7.text                    = "5A"
$Button7.width                   = 39
$Button7.height                  = 29
$Button7.location                = New-Object System.Drawing.Point(328,183)
$Button7.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button7.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button8                         = New-Object system.Windows.Forms.Button
$Button8.text                    = "6A"
$Button8.width                   = 39
$Button8.height                  = 29
$Button8.location                = New-Object System.Drawing.Point(392,183)
$Button8.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button8.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button2                         = New-Object system.Windows.Forms.Button
$Button2.text                    = "2A"
$Button2.width                   = 39
$Button2.height                  = 29
$Button2.location                = New-Object System.Drawing.Point(144,181)
$Button2.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button2.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button5                         = New-Object system.Windows.Forms.Button
$Button5.text                    = "3A"
$Button5.width                   = 39
$Button5.height                  = 29
$Button5.location                = New-Object System.Drawing.Point(203,181)
$Button5.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button5.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Button6                         = New-Object system.Windows.Forms.Button
$Button6.text                    = "4A"
$Button6.width                   = 39
$Button6.height                  = 29
$Button6.location                = New-Object System.Drawing.Point(267,181)
$Button6.Font                    = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button6.BackColor               = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Groupbox1                       = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height                = 459
$Groupbox1.width                 = 420
$Groupbox1.location              = New-Object System.Drawing.Point(52,133)
$Groupbox1.BackColor             = [System.Drawing.ColorTranslator]::FromHtml("#dff9f2")

$ToolTip1                        = New-Object system.Windows.Forms.ToolTip

$Groupbox4                       = New-Object system.Windows.Forms.Groupbox
$Groupbox4.height                = 106
$Groupbox4.width                 = 417
$Groupbox4.location              = New-Object System.Drawing.Point(51,649)

$Button14                        = New-Object system.Windows.Forms.Button
$Button14.text                   = "Reset"
$Button14.width                  = 87
$Button14.height                 = 30
$Button14.location               = New-Object System.Drawing.Point(292,39)
$Button14.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button14.BackColor              = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$ComboBox1                       = New-Object system.Windows.Forms.ComboBox
$ComboBox1.width                 = 169
$ComboBox1.height                = 68
$ComboBox1.location              = New-Object System.Drawing.Point(34,51)
$ComboBox1.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Label7                          = New-Object system.Windows.Forms.Label
$Label7.text                     = "SEAT LIST"
$Label7.AutoSize                 = $true
$Label7.width                    = 25
$Label7.height                   = 10
$Label7.location                 = New-Object System.Drawing.Point(35,35)
$Label7.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Label7.ForeColor                = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Groupbox5                       = New-Object system.Windows.Forms.Groupbox
$Groupbox5.height                = 106
$Groupbox5.width                 = 353
$Groupbox5.location              = New-Object System.Drawing.Point(555,649)

$Label8                          = New-Object system.Windows.Forms.Label
$Label8.text                     = "SEAT LIST"
$Label8.AutoSize                 = $true
$Label8.width                    = 25
$Label8.height                   = 10
$Label8.location                 = New-Object System.Drawing.Point(24,35)
$Label8.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Label8.ForeColor                = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$ComboBox2                       = New-Object system.Windows.Forms.ComboBox
$ComboBox2.width                 = 169
$ComboBox2.height                = 68
$ComboBox2.location              = New-Object System.Drawing.Point(24,51)
$ComboBox2.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Button15                        = New-Object system.Windows.Forms.Button
$Button15.text                   = "Reset"
$Button15.width                  = 87
$Button15.height                 = 30
$Button15.location               = New-Object System.Drawing.Point(241,39)
$Button15.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button15.BackColor              = [System.Drawing.ColorTranslator]::FromHtml("#ffffff")

$Panel1                          = New-Object system.Windows.Forms.Panel
$Panel1.height                   = 1143
$Panel1.width                    = 944
$Panel1.location                 = New-Object System.Drawing.Point(11,15)

$Form.controls.AddRange(@($Groupbox3,$Label4,$Groupbox2,$Label5,$Label6,$Button1,$Button7,$Button8,$Button2,$Button5,$Button6,$Groupbox1,$Groupbox4,$Groupbox5,$Panel1))
$Groupbox2.controls.AddRange(@($Button9,$Button10,$Button11,$Button12,$Button13))
$Groupbox3.controls.AddRange(@($Button3,$TextBox1,$TextBox2,$Label1,$Label2,$Label3,$TextBox3,$Button4))
$Groupbox4.controls.AddRange(@($Button14,$ComboBox1,$Label7))
$Groupbox5.controls.AddRange(@($Label8,$ComboBox2,$Button15))


[void]$Form.ShowDialog()

这就是我尝试过的.非常感谢任何人的帮助.

That's what I tried. Really appreciate it for anyone's help.

推荐答案

继续我的评论,并提供将复选框和按钮动态添加到 WinForm 的粗略示例.

Continuing from my comment with rough example(s) of dynamically adding checkboxes and buttons to a WinForm.

Add-Type -AssemblyName  System.Drawing,
                        PresentationCore,
                        PresentationFramework,
                        System.Windows.Forms,
                        Microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()

在这个例子中,我只是使用一个数字数组来动态创建一列按钮.

In this example, I am just using a number array to dynamically create a column of buttons.

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(381,316)
$Form.text                       = "Auto Button UI"
$Form.TopMost                    = $false
$Form.BackColor                  = [System.Drawing.ColorTranslator]::FromHtml("#c9f6fe")

$i = 0

1..3 | 
ForEach-Object {"Button$PSitem"} | 
foreach{
    $CurrentButton          = $null

    $CurrentButton          = New-Object System.Windows.Forms.Button
    $CurrentButton.Location = "125,$(100+50*$i)"
    $CurrentButton.size     = '100,35'
    $CurrentButton.Text     = $PSItem
    $CurrentButton.Font     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
       
    $form.Controls.Add($CurrentButton)
    $i++
}

[void]$Form.ShowDialog()

这个例子添加了两列同名复选框和一行按钮.

This example adds two columns of same-named checkboxes and a row of buttons.

$Form               = New-Object system.Windows.Forms.Form
$Form.ClientSize    = New-Object System.Drawing.Point(381,316)
$Form.text          = "Copy/Paste - User Files/Config"
$Form.TopMost       = $True
$Form.BackColor     = [System.Drawing.ColorTranslator]::FromHtml("#c9f6fe")
$Form.StartPosition = 'CenterScreen'

$CheckboxName = @('desktop', 'downloads', 'pictures', 'videos', 'favorites' )

$i, $j, $k = 0, 0, 0

$CheckboxName | 
foreach{
    $CurrentCheckbox          = $null
    $CurrentCheckbox          = New-Object System.Windows.Forms.Checkbox
    $CurrentCheckbox.Location = "75,$(100+25*$i)"
    $CurrentCheckbox.Text     = $CheckboxName[$i]
    $CurrentCheckbox.Font     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

    switch ($PSItem)
    {
        'desktop'   {$desktop0   = $CurrentButton}
        'downloads' {$downloads0 = $CurrentButton}
        'pictures'  {$pictures0  = $CurrentButton}
        'videos'    {$videos0    = $CurrentButton}
        'favorites' {$favorites0 = $CurrentButton}
    }

    $i++
    $form.Controls.Add($CurrentCheckbox)
}

$CheckboxName | 
foreach{
    $CurrentCheckbox          = $null
    $CurrentCheckbox          = New-Object System.Windows.Forms.Checkbox
    $CurrentCheckbox.Location = "200,$(100+25*$j)"
    $CurrentCheckbox.Text     = $CheckboxName[$j]
    $CurrentCheckbox.Font     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

    switch ($PSItem)
    {
        'desktop'   {$desktop1   = $CurrentButton}
        'downloads' {$downloads1 = $CurrentButton}
        'pictures'  {$pictures1  = $CurrentButton}
        'videos'    {$videos1    = $CurrentButton}
        'favorites' {$favorites1 = $CurrentButton}
    }

    $j++
    $form.Controls.Add($CurrentCheckbox)
}


'Copy', 'Paste', 'Cancel' | 
foreach{
    $CurrentButton          = $null
    $CurrentButton          = New-Object System.Windows.Forms.Button
    $CurrentButton.Location = "$(50+100*$k), 275"
    $CurrentButton.Text     = $PSItem
    $CurrentButton.Font     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

    switch ($PSItem)
    {
        'Copy'   {$Copy   = $CurrentButton}
        'Paste'  {$Paste  = $CurrentButton}
        'Cancel' {$Cancel = $CurrentButton}
    }

    $k++
    $form.Controls.Add($CurrentButton)
}


[void]$Form.ShowDialog()

在这个例子中,我只是使用一个数字数组来动态创建一行按钮,并使用 New-Variable cmdlet 使用传入的数字动态命名它们.

In this example, I am just using a number array to dynamically create a row of buttons and dynamically naming them using the passed in number using the New-Variable cmdlet.

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(381,316)
$Form.text                       = "Auto Button UI"
$Form.TopMost                    = $false
$Form.BackColor                  = [System.Drawing.ColorTranslator]::FromHtml("#c9f6fe")

$i = 0
Get-Variable -Name 'Button*' | 
Remove-Variable

1..3 | 
foreach{
    $CurrentButton          = $null
    $CurrentButton          = New-Object System.Windows.Forms.Button
    $CurrentButton.Location = "$(50+100*$i), 275"
    $CurrentButton.Text     = $PSItem
    $CurrentButton.Font     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

    New-Variable "Button$PSitem" $CurrentButton

    $i++
    $form.Controls.Add($CurrentButton)
}

[void]$Form.ShowDialog()

这篇关于如何使用 PowerShell 创建多个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆