Solved

powershell system.windows.forms

Posted on 2014-01-04
10
2,043 Views
Last Modified: 2014-01-04
Hi I need some help designing a GUI. I know some of the basics (text boxes ect). But what I really need is
1.) A browse box so the user can look through the file system to find the file. Once they have selected the file I need it be assigned to a $variable so I can use it with the Import-Csv cmdlet.
2.) 3 x Tick boxs linking to 3 Variables ($tick1, $tick2 & $tick3) and assigning a value of 1 if ticked and 0 if unticked
3.) ok and cancel buttons (when cancel is selected I need it to exit the whole script not just proceed)

Thanks in advance
0
Comment
Question by:Dead_Eyes
  • 5
  • 5
10 Comments
 
LVL 18

Expert Comment

by:Steven Harris
Comment Utility
1.) A browse box so the user can look through the file system to find the file. Once they have selected the file I need it be assigned to a $variable so I can use it with the Import-Csv cmdlet.

Try the following: ($csvImport holds the file contents)

Function Get-FileName
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

$csvImport = import-csv $file

Open in new window


2.) 3 x Tick boxs linking to 3 Variables ($tick1, $tick2 & $tick3) and assigning a value of 1 if ticked and 0 if unticked

Have you played around with If/Else statements to set the value?

if($checkbox.checked -eq $true) {
$tick1 = 1
}
Else($checkbox.checked -eq $false {
$tick1 = 0
}

Open in new window


3.) ok and cancel buttons (when cancel is selected I need it to exit the whole script not just proceed)

Close the form and keep processing:

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,100)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$form.Close()})
$Form.Controls.Add($OKButton)

Open in new window


Cancel the entire operation:

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,100)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({[System.Windows.Forms.Application]::Exit($null)})
$Form.Controls.Add($CancelButton)

Open in new window




If we throw it all together in a simple form, we get the following:

$form = new-object System.Windows.Forms.Form
$form.width = 200
$form.Height = 200

$Checkbox1 = new-object System.Windows.Forms.Checkbox
$Checkbox1.Text = "CheckBox1"
$Checkbox1.Location = new-object System.Drawing.Size(10,10)
$Checkbox1.size = new-object System.Drawing.Size(100,20)
$Form.Controls.Add($Checkbox1)

$Checkbox2 = new-object System.Windows.Forms.Checkbox
$Checkbox2.Text = "CheckBox2"
$Checkbox2.Location = new-object System.Drawing.Size(10,30)
$Checkbox2.size = new-object System.Drawing.Size(100,20)
$Form.Controls.Add($Checkbox2)

$Checkbox3 = new-object System.Windows.Forms.Checkbox
$Checkbox3.Text = "CheckBox2"
$Checkbox3.Location = new-object System.Drawing.Size(10,50)
$Checkbox3.size = new-object System.Drawing.Size(100,20)
$Form.Controls.Add($Checkbox3)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,100)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$form.Close()})
$Form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,100)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({[System.Windows.Forms.Application]::Exit($null)})
$Form.Controls.Add($CancelButton)

$form.ShowDialog()

if($Checkbox1.Checked -eq $true){$tick1=1}Else{$tick1=0}
if($Checkbox2.Checked -eq $true){$tick2=1}Else{$tick2=0}
if($Checkbox3.Checked -eq $true){$tick3=1}Else{$tick3=0}


[System.Windows.Forms.MessageBox]::Show("Checkbox1 value is set to " + $tick1)
[System.Windows.Forms.MessageBox]::Show("Checkbox2 value is set to " + $tick2)
[System.Windows.Forms.MessageBox]::Show("Checkbox3 value is set to " + $tick3)

Open in new window

0
 

Author Comment

by:Dead_Eyes
Comment Utility
Hi sorry only just saw the post. thanks for the great explanations some of this code is really simplifying things. I can't get the browse bit right though. Setting it as a function seems like a good idea but the people I need to run this script would run a mile if they saw a cmd prompt so really need to give them a nice simple browse box (click browse button scroll though an explorer style interface and select the file). I can't seem to find anything under system.windows.forms that matches though.(thanks again for if / else bit I have never used else before).
0
 
LVL 18

Expert Comment

by:Steven Harris
Comment Utility
the people I need to run this script would run a mile if they saw a cmd prompt so really need to give them a nice simple browse box (click browse button scroll though an explorer style interface and select the file).


I am not sure I follow you at this point.  When you run the code below:

Function Get-FileName
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

$csvImport = import-csv $file

Open in new window


You will get the Windows File Browser to open (as you normally see when opening files).

Windows Browser
The contents of whatever file is selected is stored as the $csvImport variable.
0
 

Author Comment

by:Dead_Eyes
Comment Utility
I am running into this error
PS C:\Windows\system32> Function Get-FileName
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

$csvImport = import-csv $file
Import-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply
an argument that is not null or empty and then try the command again.
At line:14 char:25
+ $csvImport = import-csv $file
+                         ~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.Impo
   rtCsvCommand
0
 
LVL 18

Accepted Solution

by:
Steven Harris earned 500 total points
Comment Utility
Did you select a File?  It is acting as if you canceled the Explorer Window.

Instead of pushing it to a variable, try the following to return the selected file's name:

Function Get-FileName
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

write-host $file

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Dead_Eyes
Comment Utility
I could not select a file it did not show an explorer menu. the updated code runs but again no explorer window :(
PS C:\Windows\system32> Function Get-FileName
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

write-host $file


PS C:\Windows\system32>
0
 
LVL 18

Expert Comment

by:Steven Harris
Comment Utility
What version of PowerShell?
0
 

Author Comment

by:Dead_Eyes
Comment Utility
v4
0
 

Author Closing Comment

by:Dead_Eyes
Comment Utility
My bad I must have had something in the background running that was messing with the code. thanks for a great explanation and your patience
0
 
LVL 18

Expert Comment

by:Steven Harris
Comment Utility
Glad you got it worked out!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Set OWA language and time zone in Exchange for individuals, all users or per database.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now