0

i have just made a simple powershell gui. you can find the code below. it is running fine. how can i add a tab to my existing gui, so that i have a second part where i can add some actions, like another button with a action behind it.

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

1 Answer 1

0

Firstly, don't use the same variable name for two objects (buttons)!

There's plenty of articles available to demonstrate this, but at a minimum you'll need to add something like this:

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

Comments

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.