0

I'm looking to setup a GUI that will determine the next step of a first time setup application. I originally programed the whole thing using batch but would like a more user friendly look. The program basically uninstalls bloatware that computer manufacturers load on their computers and installs some useful programs. This is my first time using PowerShell. This is what I have so far:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Pauk Inc."
$objForm.Size = New-Object System.Drawing.Size(300,175) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
{$objForm.Close()}})

$YesButton = New-Object System.Windows.Forms.Button
$YesButton.Location = New-Object System.Drawing.Size(70,95)
$YesButton.Size = New-Object System.Drawing.Size(75,23)
$YesButton.Text = "Sure"
$YesButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($YesButton)

$NoButton = New-Object System.Windows.Forms.Button
$NoButton.Location = New-Object System.Drawing.Size(155,95)
$NoButton.Size = New-Object System.Drawing.Size(75,23)
$NoButton.Text = "No Thanks"
$NoButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($NoButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(55,50) 
$objLabel.Size = New-Object System.Drawing.Size(280,25) 
$objLabel.Text = "Would you like to install Anti Virus?"
$objForm.Controls.Add($objLabel)  

$objForm.Topmost = $True

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

$x

Essentially what I'm looking to do is Install the AV if they click "Sure", and don't if they click "No Thanks".

2
  • Ok, so what's your question to us exactly? Where are you getting stuck? What have you attempted already, and what were the results? Commented Apr 29, 2016 at 16:51
  • Okay, so in .bat you can ask a question. echo Do you want to continue? (y or n) set p/ answer= and whatever the person types tells the program what to follow next. if %answer% == y goto yes if %answer% == n goto no I want to do the same thing except in PowerShell and with a click of a button rather than typing y or n. Commented Apr 30, 2016 at 14:47

2 Answers 2

2

If you're just after yes/no responses, a MessageBox requires much less code.

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

[System.Windows.Forms.DialogResult]$result = [System.Windows.Forms.MessageBox]::Show("Would you like to install anti-virus?", "Install Anti-Virus?", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question, [System.Windows.Forms.MessageBoxDefaultButton]::Button2)

if ($result -eq ([System.Windows.Forms.DialogResult]::Yes))
{
    # User selected yes.
}
else
{
    # User selected no.
}
Sign up to request clarification or add additional context in comments.

1 Comment

The outcome I'm looking for is the whole program running in a gui. So once I get a better understanding of how to use PowerShell commands I plan to expand the size of that box and add different buttons, along with colors and possibly small animations (If that's possible) things like a percent bar and whatnot. I hope that makes a bit of sense to you. Thank you for your submission.
0

You can do this just like you would in any Windows Forms app. You haven't set a DialogResult for the buttons, so there's no way to to differentiate the buttons when you click them. Just replace the last lines with this:

$YesButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$result = $objForm.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    Write-Host "Installing..."
}

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.