Reputation: 103
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".
Upvotes: 0
Views: 353
Reputation: 196
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.
}
Upvotes: 2
Reputation: 17691
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..."
}
Upvotes: 0