?
Solved

powershell system.windows.forms

Posted on 2014-01-04
10
Medium Priority
?
2,319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39756322
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
ID: 39756463
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
ID: 39756503
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
WatchGuard's M Series Appliances - Miecom Approved

WatchGuard's newest M series appliances were put to the test by Miercom.  We had great results and outperformed all of our competitors in both stateless and stateful traffic throghput scenarios! Ready to see how your UTM appliance stacked up? Download the Miercom Report!

 

Author Comment

by:Dead_Eyes
ID: 39756592
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 2000 total points
ID: 39756614
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
 

Author Comment

by:Dead_Eyes
ID: 39756622
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
ID: 39756649
What version of PowerShell?
0
 

Author Comment

by:Dead_Eyes
ID: 39756677
v4
0
 

Author Closing Comment

by:Dead_Eyes
ID: 39756702
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
ID: 39756786
Glad you got it worked out!
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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