Solved

powershell system.windows.forms

Posted on 2014-01-04
10
2,234 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 500 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 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