I have an GUI to select a file. I use OpenFileDialog. But when I press the button to open the file, the target folder sometime different. I want to make it default folder when I click the button.
Function Sel_File($InitialDirectory)
{
Add-Type -AssemblyName System.Windows.Forms
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.Title = "Please Select File"
$OpenFileDialog.InitialDirectory = $InitialDirectory
$OpenFileDialog.filter = “All files (*.*)| *.*”
If ($OpenFileDialog.ShowDialog() -eq "Cancel")
{
[System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0,
[System.Windows.Forms.MessageBoxIcon]::Exclamation)
} $Global:SelectedFile = $OpenFileDialog.SafeFileName
}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.AutoSize = $true
$Form.text = "Auto GM Creation"
$Form.TopMost = $true
#----------------------
$ChooseML_L = New-Object system.Windows.Forms.Label
$ChooseML_L.text = "MLs"
$ChooseML_L.AutoSize = $true
$ChooseML_L.width = 25
$ChooseML_L.height = 10
$ChooseML_L.location = New-Object System.Drawing.Point(28,20)
$ChooseML_L.ForeColor = "#000000"
$SelectML = New-Object system.Windows.Forms.TextBox
$SelectML.AutoSize = $true
$SelectML.width = 150
$SelectML.height = 30
$SelectML.location = New-Object System.Drawing.Point(120,40)
$SelectML.Text = "Selected ML"
$ChooseML = New-Object System.Windows.Forms.Button
$ChooseML.text = "Select File"
$ChooseML.AutoSize = $true
$ChooseML.width = 90
$ChooseML.height = 20
$ChooseML.location = New-Object System.Drawing.Point(28,38)
$ChooseML.ForeColor = "#ffffff"
$ChooseML.BackColor = "#093c76"
$ChooseML.Add_Click({Sel_File
$SelectML.Text = $Global:SelectedFile
})
#----------
$Apply = New-Object system.Windows.Forms.Button
$Apply.BackColor = "#6996c8"
$Apply.text = "Apply"
$Apply.width = 99
$Apply.height = 30
$Apply.location = New-Object System.Drawing.Point(320,190)
#----------
$Cancel = New-Object system.Windows.Forms.Button
$Cancel.BackColor = "#6996c8"
$Cancel.text = "Cancel"
$Cancel.width = 98
$Cancel.height = 30
$Cancel.location = New-Object System.Drawing.Point(450,190)
$Cancel.Add_Click({$Form.Close()})
#-----------
$Prefix = New-Object system.Windows.Forms.Label
$Prefix.text = "Prefix"
$Prefix.AutoSize = $true
$Prefix.width = 25
$Prefix.height = 10
$Prefix.location = New-Object System.Drawing.Point(28,80)
$Prefix.ForeColor = "#000000"
$NB = New-Object system.Windows.Forms.RadioButton
$NB.text = "NB"
$NB.AutoSize = $true
$NB.BackColor = "#4a90e2"
$NB.width = 104
$NB.height = 20
$NB.location = New-Object System.Drawing.Point(28,100)
$DPC = New-Object system.Windows.Forms.RadioButton
$DPC.text = "DPC"
$DPC.AutoSize = $true
$DPC.BackColor = "#4a90e2"
$DPC.width = 104
$DPC.height = 20
$DPC.location = New-Object System.Drawing.Point(100,100)
$Form.Controls.AddRange(#($ChooseML, $Prefix, $ChooseML_L, $Apply, $Cancel, $SelectML, $NB, $DPC))
[void] $Form.ShowDialog()
With this code I can go to any folder.
My expectation I can go to a specific folder that I need and choose the file.
Updated
This is working
Function Sel_File
{
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = “C:\Users\XX”
$OpenFileDialog.Title = "Please Select File"
$OpenFileDialog.filter = “All files (*.*)| *.*”
If ($OpenFileDialog.ShowDialog() -eq "Cancel")
{
[System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0,
[System.Windows.Forms.MessageBoxIcon]::Exclamation)
} $Global:SelectedFile = $OpenFileDialog.SafeFileName
}
If you want keep the last folder you can do it:
$OpenFileDialog.RestoreDirectory = $True
If you want just specify your initial directory modify your code like this:
Sel_File "c:\temp"
Related
I want to monitor the date and time of a file. I wrote the code that do the job as I want but I can't reposition the gui window. I tried all I could find like "start-job" or create a new runspace but I don't get any results in richtextbox. Any suggestion is welcome.
$targetFile = "full path"
# Function - Add Text to RichTextBox
function Add-RichTextBox{
[CmdletBinding()]
param ($text)
#$richtextbox_output.Text += "`tCOMPUTERNAME: $ComputerName`n"
$richtext.Text += "$text"
$richtext.Text += "`n"
}
# Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Monitor script"
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'
$Font = New-Object System.Drawing.Font("Tahoma",11)
$Form.Font = $Font
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,40)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = (Get-Date).ToString()
$form.Controls.Add($label)
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(150,220)
$StartButton.Size = New-Object System.Drawing.Size(100,33)
$StartButton.Text = 'Start'
#$StartButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $StartButton
$form.Controls.Add($StartButton)
$StartButton.Add_click({
while($true){
$lastdate = Get-ChildItem $targetFile
$Date = $lastdate.LastAccessTime.ToString()
Add-RichTextBox "$date"
$richtext.SelectionStart = $richtext.TextLength
$richText.ScrollToCaret()
#$richtext.refresh()
#$form.refresh()
Start-sleep 30
}
})
## the Rich text box
$richtext = new-object System.Windows.Forms.RichTextBox
$richtext.Location = New-Object System.Drawing.Point(20,60)
$richtext.multiline = $true
$richtext.Name = "Results"
$richtext.text = "Results:`n"
$richtext.scrollbars = "Both"
$richtext.Height = 120
$richtext.width = 350
$richtext.font = new-object system.drawing.font "Lucida Console",10
$Form.controls.add($richtext)
$Form.Add_Shown({$Form.Activate()})
$form.ShowDialog()
Continuing from my comment to use a Timer on your form (if you absolutely do not want a FileSystemWatcher), here's how you can do that:
$targetFile = "D:\Test\blah.txt"
$lastAccessed = (Get-Date) # a variable to keep track of the last LastAccessTime of the file
# Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Monitor script"
$form.Size = New-Object System.Drawing.Size(400,300)
$form.StartPosition = 'CenterScreen'
$Font = New-Object System.Drawing.Font("Tahoma",11)
$form.Font = $Font
# Label
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20,40)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = (Get-Date).ToString()
$form.Controls.Add($label)
# Button
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Point(150,200)
$StartButton.Size = New-Object System.Drawing.Size(100,33)
$StartButton.Text = 'Start'
$form.Controls.Add($StartButton)
$StartButton.Add_click({
$timer.Enabled = $true
$timer.Start()
})
# RichTextBox
$richtext = New-Object System.Windows.Forms.RichTextBox
$richtext.Location = New-Object System.Drawing.Point(20,60)
$richtext.Multiline = $true
$richtext.Name = "Results"
$richtext.Text = "Results:`r`n"
$richtext.ScrollBars = "Both"
$richtext.Height = 120
$richtext.Width = 350
$richtext.Font = New-Object System.Drawing.Font "Lucida Console",10
$form.Controls.Add($richtext)
# Timer
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 30000 # 30 seconds
$timer.Enabled = $false # disabled at first
$timer.Add_Tick({
$file = Get-Item -Path $targetFile -ErrorAction SilentlyContinue
# if we can find the file and its last AccessedTime is not
# the same as we already stored in variable $lastAccessed
# use script-scoping here, so $script:lastAccessed instead of $lastAccessed
if ($file -and $file.LastAccessTime -gt $script:lastAccessed) {
$richtext.AppendText("$($file.LastAccessTime.ToString())`r`n")
$script:lastAccessed = $file.LastAccessTime # remember this new datetime
}
})
$form.ShowDialog()
# Important: Clean up
$timer.Stop()
$timer.Dispose()
$richtext.Dispose()
$form.Dispose()
I have been stuck in this for whole day. I am creating a UI using PowerShell forms. In that:
user select an option from 1st combobox. click button Go
Based on the selection, a panel will appear having another combobox.
if user select another option in 1st combobox then another panel appears with another combobox
After selecting options from panel comboboxes, user clicks on start button.
This leads to a function which stores the selected options to a variable.
Problem
Now when user selects the options from the comboboxes of panels, I am using $combobox.selecteditem.Tostring to get the values.
But it gives me NULL result.
Here is my code..
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.Add("View")
$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
######### code Starts ###########
function Button1_Click()
{
$link = $comboBox2.SelectedItem.ToString();
$linktype = $comboBox3.SelectedItem.ToString();
}
####### end of code ######
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
The issue is that your variables are being set in a function, so they are scoped to that function. There's really no reason to put that in a function, put it directly in the .add_click() instead. Or if you feel that you need to keep it in the function you can scope the variable like you did with $global:ButtonClicked and set them to $global:Link and $global:LinkType.
Edit: Order does make a difference in PowerShell, so I did move some stuff around in your script to make it work right, but I was able to get values to reflect fine when I put them in the global scope.
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
######### code Starts ###########
function Button1_Click()
{
write-host "Link = $global:link"
write-host "Link Type = $global:linktype"
}
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
####### end of code ######
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(#("View","Edit") )
#$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
When I ran that, set the two combobox values, and clicked Start I got text in the ISE saying what I set the Link and LinkType variables to, and was able to echo $global:link and $global:linktype to see the values correctly assigned.
I got a problem with PowerShell and its IF-Cmdlet.
It can also be the TextBox where I want to input a variable to set a default text. In either way it doesn't work as intended. What it should do exactly is described im the Code^^
Thanks to all people helping me out^^
It is not for work or anything...its just a little project I try^^
Oh...and sorry for my bad English(maybe), I'm from Germany.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function windowInstall1()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Adding a Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Welcome to the TimeStamp Installation Setup"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Adding a Label
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,50)
$Label2.Text = ("To Continue the Installation please press " + '"' + "Continue" + '"')
$Label2.Size = New-Object System.Drawing.Size(450,20)
$Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label2)
#Adding a Label
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,71)
$Label3.Text = ("Else press " + '"' + "Exit" + '"' + " to cancel the installation")
$Label3.Size = New-Object System.Drawing.Size(450,20)
$Label3.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label3)
#Adding a button
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status1 = "true"
$window.Dispose()
windowInstall2
})
$window.Controls.Add($windowButton1)
#Adding a button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status1 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
}
function windowInstall2()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Adding a Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Choose your Program"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Adding RadioButtons in a GroupBox
$radioButton1 = New-Object System.Windows.Forms.RadioButton
$radioButton2 = New-Object System.Windows.Forms.RadioButton
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$groupBox1.Controls.AddRange(
#(
$radioButton1,
$radioButton2
))
$groupBox1.Location = New-Object System.Drawing.Point(10, 60)
$groupBox1.Name = 'groupBox'
$groupBox1.Size = New-Object System.Drawing.Size(160, 100)
$groupBox1.Text = 'Programms'
# radioButton1
$radioButton1.Location = New-Object System.Drawing.Point(8, 32)
$radioButton1.Name = 'Button1'
$radioButton1.Text = 'TimesStamp'
$radioButton1.Size = New-Object System.Drawing.Size(150, 20)
# radioButton2
$radioButton2.Location = New-Object System.Drawing.Point(8, 64)
$radioButton2.Name = 'Button2'
$radioButton2.Text = 'TimeStamp with Text'
$radioButton2.Size = New-Object System.Drawing.Size(150, 20)
$window.Controls.Add($groupBox1)
#Adding a Button
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status2 = "true"
$window.Dispose()
windowInstall3
})
$window.Controls.Add($windowButton1)
#Adding a Button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status2 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$status2
}
function folderDialog()
{
#Choose a Folder
$ChooseFolder = New-Object System.Windows.Forms.FolderBrowserDialog
$ChooseFolder.Description = 'Select the Saving Location Folder'
$ChooseFolder.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$global:tempDir = $ChooseFolder.SelectedPath
}
function windowInstall3()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
#Another Label(Header)
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Setup Install Location"
$Label1.Size = New-Object System.Drawing.Size(450,40)
$Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label1)
#Another Label
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,80)
$Label2.Text = "Choose the Folder where the Installation should take place"
$Label2.Size = New-Object System.Drawing.Size(450,40)
$Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
$window.Controls.Add($Label2)
#Here is where I have Problems
#If $tempDir is empty it should put a default Path
#If not it should use $tempDir
$windowTextBox1 = New-Object System.Windows.Forms.TextBox
$windowTextBox1.Location = New-Object System.Drawing.Size(10,130)
$windowTextBox1.Size = New-Object System.Drawing.Size(300,150)
if($tempDir = "")
{
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else
{
$windowTextBox1.Text = $tempDir
}
$window.Controls.Add($windowTextBox1)
$windowButton1 = New-Object System.Windows.Forms.Button
$windowButton1.Location = New-Object System.Drawing.Size(100,500)
$windowButton1.Size = New-Object System.Drawing.Size(75,50)
$windowButton1.Text = "Continue"
$windowButton1.Add_Click({
$global:status3 = "true"
$window.Dispose()
})
$window.Controls.Add($windowButton1)
#Add another Button
$windowButton2 = New-Object System.Windows.Forms.Button
$windowButton2.Location = New-Object System.Drawing.Size(300,500)
$windowButton2.Size = New-Object System.Drawing.Size(75,50)
$windowButton2.Text = "Exit"
$windowButton2.Add_Click({
$global:status3 = "false"
$window.Dispose()
})
$window.Controls.Add($windowButton2)
[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property #{TopMost = $true }))
$status3
}
windowInstall1
The equal sign here is incorrect.
= is used for variable assignment
You should be using the -eq operator instead.
Correct
if ($tempDir -eq "") {
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
$windowTextBox1.Text = $tempDir
}
Incorrect
# This is not a valid IF condition
if ($tempDir = "") {
$windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
$windowTextBox1.Text = $tempDir
}
Additionally, if your $tempdir variable is $null instead of an empty script, this will be seen as if $tempdir is correctly populated. To cover both an empty string and a $null value, you can use [String]::IsNullOrEmpty($tempdir) in your condition statement.
I need to create a checkbox automatically depends on folder I selected. I create ComboBox, then in the ComboBox, I can select which folder that I want to select. Inside of my folder, I have some file. The file consist of some extension file. I just need to pick 2 extension file from the folder, example(*.txt and *.csv).
After I select the folder, the checkBox will create automatically, the total of the checkBox depends on how many file exist in that folder with specific extension(*.txt and *.csv).
In my code, I already do some stuff, which is select the folder that I need to select, but still struggle with the checkBox. Anyone can help me please. Thank you so much. I really appreciate for the help.
I put my script in the ##2nd Updated.
Updated
Consider to #f6a4 answer, this is the result
The first picture is I just use this path to get the folder
'D:\Data\'
In the first picture, I already double click the folder1 and folder2, but the file do not appear.
The second picture, I specify the folder path
'D:\Data\folder1'
The second picture, the file appear because I specify the folder in the path, so the folder name do not appear in the please select folder box and return this error $CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFold ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
2n Updated
I updated my script. But the file only appear 1, once I click the folder. And when I change to click other folder, It does not appear the file.
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Global:status = "inactive"
$Global:array = New-Object System.Collections.Generic.List[System.Object]
$Form = New-Object system.Windows.Forms.Form
$Form.text = "BPS Image Automation Utility"
$Form.BackColor = "#f6f6f6"
$Form.AutoSize = $true
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $false
$Form.startposition = "centerscreen"
$Form.WindowState = 'Maximized'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Please select the image"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(50,50)
$Label1.Font = 'Microsoft Sans Serif,10'
$Label1.ForeColor = "#000000"
$label1.AutoSize = $true
$Button3 = New-Object system.Windows.Forms.Button
$Button3.BackColor = "#136aa4"
$Button3.ForeColor = "#ffffff"
$Button3.text = "Done"
$Button3.width = 90
$Button3.height = 32
$Button3.AutoSize = $true
$Button3.UseCompatibleTextRendering = $True
$Button3.UseVisualStyleBackColor = $False
# $Button3.location = New-Object System.Drawing.Point(1700,920)
$Button3.Font = 'Microsoft Sans Serif,10'
# $Button3.Visible = $false
$Button2 = New-Object system.Windows.Forms.Button
$Button2.BackColor = "#136aa4"
$Button2.ForeColor = "#ffffff"
$Button2.text = "Delete"
$Button2.width = 90
$Button2.height = 32
$Button2.UseCompatibleTextRendering = $True
$Button2.UseVisualStyleBackColor = $False
$Button2.AutoSize = $true
# $Button2.location = New-Object System.Drawing.Point(1600,920)
$Button2.Font = 'Microsoft Sans Serif,10'
# $Button2.Visible = $false
$Panel = New-Object System.Windows.Forms.TableLayoutPanel
$panel.Dock = "Fill"
$panel.ColumnCount = 1
$panel.RowCount = 1
$panel.CellBorderStyle = "single"
$panel.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$panel.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.text = "Job Handling"
$Groupbox1.Font = 'Microsoft Sans Serif,9'
$Groupbox1.AutoSize = $true
$Groupbox1.ForeColor = "#032d5d"
$Groupbox1.location = New-Object System.Drawing.Point(8,13)
$Groupbox1.Padding = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
$Groupbox1.Dock = "fill"
$Groupbox1.UseCompatibleTextRendering = $True
$Groupbox2 = New-Object system.Windows.Forms.Groupbox
$Groupbox2.text = "Job Information"
$Groupbox2.Font = 'Microsoft Sans Serif,9'
$Groupbox2.AutoSize = $true
$Groupbox2.ForeColor = "#032d5d"
$Groupbox2.Dock = "None"
$Groupbox2.UseCompatibleTextRendering = $True
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.BackColor = "#e8f3ff"
$ComboBox1.width = 190
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(35,80)
$ComboBox1.Font = 'Microsoft Sans Serif,12'
$ComboBox1.AutoSize = $true
$ImageList = #(Get-ChildItem -Directory "D:\Process")
foreach ($img in $ImageList) {
$ComboBox1.Items.Add($img)
}
$ComboBox1.Add_Click({
if($ComboBox1.SelectedItem){
$Checkbox.Visible = $true
}
})
$Checkboxes += New-Object System.Windows.Forms.CheckBox
$Checkboxes = #()
$y = 20
$files = Get-ChildItem "D:\Process\$img" -Filter *.txt, *.csv
$files
foreach ($file in $files)
{
$Checkbox = New-Object System.Windows.Forms.CheckBox
$Checkbox.Text = $file
$Checkbox.Location = New-Object System.Drawing.Size(10,$y)
$Checkbox.Size = New-Object System.Drawing.Size(330,20)
$y += 30
$Groupbox2.Controls.Add($Checkbox)
$Checkboxes += $Checkbox
$Checkbox.Visible = $false
}
$Form.controls.AddRange(#($Panel))
$Panel.controls.AddRange(#($Groupbox1))
$Groupbox1.Controls.AddRange(#($Groupbox2, $ComboBox1, $Label1, $Button3, $Button2))
[void]$Form.Show()
$g2w = $Form.Width - 90
$g2h = $Form.Height - 270
$g2h
$Groupbox2.location = New-Object System.Drawing.Point(35,110)
$Groupbox2.size = New-Object System.Drawing.Size($g2w,$g2h)
$Groupbox2.AutoSize = $true
$bt2_w = $g2w - 55
$bt2_h = $g2h + 130
$Button2.location = New-Object System.Drawing.Point($bt2_w,$bt2_h)
$bt3_w = $g2w - 160
$Button3.location = New-Object System.Drawing.Point($bt3_w,$bt2_h)
$Form.Visible = $false
[void]$Form.ShowDialog()
This should do exactly what you want. By double click on a Directory you can browse through subdirectories as well.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# Global variables
$GH = [hashtable]::Synchronized(#{})
$GH.FolderPath = 'C:\Users\myUser\Desktop\csvfiles'
$GH.CurrentFolderPath = $GH.FolderPath
$GH.FileMask = #('*.txt','*.csv')
# windows form
$form = New-Object System.Windows.Forms.Form
$form.Visible = $false
[void]$form.SuspendLayout()
$form.Text = "File Selection"
$form.ClientSize = New-Object System.Drawing.Size(320,430)
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
# tab control
$CTRL_TabCtrl = New-Object System.Windows.Forms.TabControl
$CTRL_TabCtrl.Location = New-Object System.Drawing.Point(5,5)
$CTRL_TabCtrl.Size = New-Object System.Drawing.Size(310,420)
[void]$form.Controls.Add($CTRL_TabCtrl)
$CTRL_Tab1 = New-Object System.Windows.Forms.TabPage
$CTRL_Tab1.AutoSize = $true
$CTRL_Tab1.Text = 'Main'
$CTRL_Tab1.TabIndex = 1
[void]$CTRL_TabCtrl.Controls.Add($CTRL_Tab1)
# list folder
$CTRL_label10 = New-Object System.Windows.Forms.Label
$CTRL_label10.Location = New-Object System.Drawing.Point(10,10)
$CTRL_label10.Size = New-Object System.Drawing.Size(260,20)
$CTRL_label10.Name = 'Label10'
$CTRL_label10.Text = 'Please select a folder:'
[void]$CTRL_Tab1.Controls.Add($CTRL_label10)
$CTRL_ListFolder = New-Object System.Windows.Forms.Listbox
$CTRL_ListFolder.Location = New-Object System.Drawing.Point(10,30)
$CTRL_ListFolder.Size = New-Object System.Drawing.Size(280,60)
$CTRL_ListFolder.SelectionMode = [System.Windows.Forms.SelectionMode]::One
$CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFolderPath -Directory).Name )
$CTRL_ListFolder.Enabled = $true
$CTRL_ListFolder.Add_MouseDoubleClick( {
$listFolder_innerevent = $true
if( $CTRL_ListFolder.SelectedItem -eq '..' ) {
$GH.CurrentFolderPath = $GH.CurrentFolderPath.Substring( 0, $GH.CurrentFolderPath.LastIndexOf( '\' ) )
[void]$CTRL_ListFolder.Items.Clear()
if( $GH.CurrentFolderPath.Length -gt $GH.FolderPath.Length ) {
[void]$CTRL_ListFolder.Items.Add( '..' )
}
$CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFolderPath -Directory).Name )
[void]$CTRL_CheckListBox.Items.Clear()
$files = (Get-ChildItem -Path ($GH.CurrentFolderPath + '\*') -Include $GH.FileMask -File ).Name
if( $files ) {
[void]$CTRL_CheckListBox.Items.AddRange( $files )
}
}
else {
if( (Get-ChildItem -Path ($GH.CurrentFolderPath + '\' + $CTRL_ListFolder.SelectedItem) -Directory).Name ) {
$GH.CurrentFolderPath += '\' + $CTRL_ListFolder.SelectedItem
[void]$CTRL_ListFolder.Items.Clear()
[void]$CTRL_ListFolder.Items.Add( '..' )
[void]$CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFolderPath -Directory).Name )
[void]$CTRL_CheckListBox.Items.Clear()
$files = (Get-ChildItem -Path ($GH.CurrentFolderPath + '\*') -Include $GH.FileMask -File ).Name
if( $files ) {
[void]$CTRL_CheckListBox.Items.AddRange( $files )
}
}
}
} )
[void]$CTRL_Tab1.Controls.Add($CTRL_ListFolder)
# list folder with check boxes for files
$CTRL_label12 = New-Object System.Windows.Forms.Label
$CTRL_label12.Location = New-Object System.Drawing.Point(10,100)
$CTRL_label12.Size = New-Object System.Drawing.Size(260,20)
$CTRL_label12.Name = 'Label12'
$CTRL_label12.Text = 'Files found:'
[void]$CTRL_Tab1.Controls.Add($CTRL_label12)
$CTRL_CheckListBox = New-Object System.Windows.Forms.CheckedListbox
$CTRL_CheckListBox.Location = New-Object System.Drawing.Point(10,120)
$CTRL_CheckListBox.Size = New-Object System.Drawing.Size(280,230)
$CTRL_CheckListBox.CheckOnClick = $true
$CTRL_CheckListBox.Enabled = $true
$files = (Get-ChildItem -Path ($GH.CurrentFolderPath + '\*') -Include $GH.FileMask -File ).Name
if( $files ) {
[void]$CTRL_CheckListBox.Items.AddRange( $files )
}
[void]$CTRL_Tab1.Controls.Add($CTRL_CheckListBox)
$CTRL_OKButton1 = New-Object System.Windows.Forms.Button
$CTRL_OKButton1.Location = New-Object System.Drawing.Point(70,365)
$CTRL_OKButton1.Size = New-Object System.Drawing.Size(75,23)
$CTRL_OKButton1.Text = 'OK'
$CTRL_OKButton1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $CTRL_OKButton1
[void]$CTRL_Tab1.Controls.Add($CTRL_OKButton1)
$CTRL_CancelButton1 = New-Object System.Windows.Forms.Button
$CTRL_CancelButton1.Location = New-Object System.Drawing.Point(150,365)
$CTRL_CancelButton1.Size = New-Object System.Drawing.Size(75,23)
$CTRL_CancelButton1.Text = 'Cancel'
$CTRL_CancelButton1.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CTRL_CancelButton1
[void]$CTRL_Tab1.Controls.Add($CTRL_CancelButton1)
[void]$form.ResumeLayout()
$userInput = $form.ShowDialog()
if ($userInput -eq [System.Windows.Forms.DialogResult]::OK) {
# User clicked OK Button
}
I have an GUI, I want to open a folder and select a file. When I execute my code from ISE, it works. but when I run from another environment with cmd, it shows some error
Exception calling "ShowDialog" with "0" arguument(S): "Creating an instance of the COM component with CLSID.....
Function Sel_File
{
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = "P:\Temp\MM"
$OpenFileDialog.Title = "Please Select File"
$OpenFileDialog.filter = “All files (*.*)| *.*”
If ($OpenFileDialog.ShowDialog() -eq "Cancel")
{
[System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0,
[System.Windows.Forms.MessageBoxIcon]::Exclamation)
} $Global:SelectedFile = $OpenFileDialog.SafeFileName
}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.AutoSize = $true
$Form.text = "Auto GM Creation"
$Form.TopMost = $true
#----------------------
$ChooseML_L = New-Object system.Windows.Forms.Label
$ChooseML_L.text = "MLs"
$ChooseML_L.AutoSize = $true
$ChooseML_L.width = 25
$ChooseML_L.height = 10
$ChooseML_L.location = New-Object System.Drawing.Point(28,20)
$ChooseML_L.ForeColor = "#000000"
$SelectML = New-Object system.Windows.Forms.TextBox
$SelectML.AutoSize = $true
$SelectML.width = 150
$SelectML.height = 30
$SelectML.location = New-Object System.Drawing.Point(120,40)
$SelectML.Text = "Selected ML"
$ChooseML = New-Object System.Windows.Forms.Button
$ChooseML.text = "Select File"
$ChooseML.AutoSize = $true
$ChooseML.width = 90
$ChooseML.height = 20
$ChooseML.location = New-Object System.Drawing.Point(28,38)
$ChooseML.ForeColor = "#ffffff"
$ChooseML.BackColor = "#093c76"
$ChooseML.Add_Click({Sel_File
$SelectML.Text = $Global:SelectedFile
})
#----------
$Apply = New-Object system.Windows.Forms.Button
$Apply.BackColor = "#6996c8"
$Apply.text = "Apply"
$Apply.width = 99
$Apply.height = 30
$Apply.location = New-Object System.Drawing.Point(320,190)
#----------
$Cancel = New-Object system.Windows.Forms.Button
$Cancel.BackColor = "#6996c8"
$Cancel.text = "Cancel"
$Cancel.width = 98
$Cancel.height = 30
$Cancel.location = New-Object System.Drawing.Point(450,190)
$Cancel.Add_Click({$Form.Close()})
#-----------
$Prefix = New-Object system.Windows.Forms.Label
$Prefix.text = "Prefix"
$Prefix.AutoSize = $true
$Prefix.width = 25
$Prefix.height = 10
$Prefix.location = New-Object System.Drawing.Point(28,80)
$Prefix.ForeColor = "#000000"
$NB = New-Object system.Windows.Forms.RadioButton
$NB.text = "NB"
$NB.AutoSize = $true
$NB.BackColor = "#4a90e2"
$NB.width = 104
$NB.height = 20
$NB.location = New-Object System.Drawing.Point(28,100)
$DPC = New-Object system.Windows.Forms.RadioButton
$DPC.text = "DPC"
$DPC.AutoSize = $true
$DPC.BackColor = "#4a90e2"
$DPC.width = 104
$DPC.height = 20
$DPC.location = New-Object System.Drawing.Point(100,100)
$Form.Controls.AddRange(#($ChooseML, $Prefix, $ChooseML_L, $Apply, $Cancel, $SelectML, $NB, $DPC))
[void]$Form.ShowDialog()
This is my code updated. This is working using PS ISE, but I tried execute it from WinPE environment, It shows those error above.
Could anyone help me please. Thank you
this works ... but i think the use of LoadWithPartialName has been deprecated. i can't find the "new way" at this time, tho. [blush]
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") |
Out-Null
$SelectFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$SelectFileDialog.InitialDirectory = 'c:\temp'
$SelectFileDialog.Filter = 'Extensionless files (*)|*'
$SelectFileDialog.Title = 'Please select a file and then click [OK]'
$SelectFileDialog.ShowDialog() |
Out-Null
$SelectFileDialog.FileName
I believe it'll work if you remove | Out-Null from this line
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog | Out-Null
Just need to add this, it works well.
Thank you for all the contribution
$OpenFileDialog.AutoUpgradeEnabled =$false