GUI help

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Posts: 1
Last visit: Mon Mar 27, 2023 11:45 am

GUI help

Post by JaredPSS »

Hello! I am trying to create an app that will have several buttons on the main page. Right now I have two buttons, and when you click the first button "Network" it makes a panel visible which has 2 more buttons. I was manually positioning them, but they overlap my panel buttons when they show up. Ill add a screenshot that helps explain it better. Thanks for any help!

# Create a new form object
$form = New-Object System.Windows.Forms.Form

# Set the form's properties
$form.Text = "Main Window"
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$form.AutoSize = $true

# Create a button object called "Network" that toggles the additional buttons when clicked
$button1A = New-Object System.Windows.Forms.Button
$button1A.Location = New-Object System.Drawing.Point(20, 20)
$button1A.Size = New-Object System.Drawing.Size(100, 30)
$button1A.Text = "Network"

# Create a panel object to hold the additional buttons
$panel = New-Object System.Windows.Forms.Panel
$panel.Location = New-Object System.Drawing.Point(20, 60)
$panel.Size = New-Object System.Drawing.Size(100, 60)
$panel.Visible = $false

# Create two new button objects within the panel object
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Point(0, 0)
$button1.Size = New-Object System.Drawing.Size(100, 30)
$button1.Text = "Button 1"

$button2 = New-Object System.Windows.Forms.Button
$button2.Location = New-Object System.Drawing.Point(0, 30)
$button2.Size = New-Object System.Drawing.Size(100, 30)
$button2.Text = "Button 2"

# Add an event handler to toggle the visibility of the panel when the button is clicked
if ($panel.Visible)
$panel.Visible = $false
$panel.Visible = $true

# Show the form
$form.ShowDialog() > $null

function Main {
The Main function starts the project application.

.PARAMETER Commandline
$Commandline contains the complete argument string passed to the script packager executable.

Use this function to initialize your script and to call GUI forms.

To get the console output in the Packager (Forms Engine) use:
$ConsoleOutput (Type: System.Collections.ArrayList)
Param ([String]$Commandline)

#TODO: Add initialization script here (Load modules and check requirements)


$script:ExitCode = 0 #Set the exit code for the Packager
Posts: 15398
Last visit: Fri May 26, 2023 4:13 pm
Answers: 29
Has voted: 4 times
Been upvoted: 32 times

Re: GUI help

Post by jvierra »

The code is not helpful as it just displays a form. The "Main" function will never be called.

It would be better if you posted the PSF file as an attachment.