0

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.

1
  • 1
    Though I cannot imagine a use case for a form with 50 buttons, WoW...; just put it in a loop passing in a list of button names, and increment the location property and other properties as needed in that loop. Commented Jan 13, 2021 at 8:27

1 Answer 1

2

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

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()
Sign up to request clarification or add additional context in comments.

5 Comments

Oh man, a gui! How cool(: I too have one but, i run into multiple issues using a separate runspace
yessir, just dont seem to pick it up well
UNderstood. RunSpaces are a challenge but are useful in some cases. You may want to look into thread Jobs [Start-ThreadJob] for inclusion for run space efforts.
Will do! Thank you. Ive actually never heard of that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.