This code open GUI to Input Time & Date:
Add-Type -AssemblyName System.Windows.Forms
# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$font = New-Object System.Drawing.Font(“Consolas”, 13)
$mainForm.Text = ” Pick Time Frame”
$mainForm.Font = $font
$mainForm.ForeColor = “White”
$mainForm.BackColor = “DarkOliveGreen”
$mainForm.Width = 300
$mainForm.Height = 200
# DatePicker Label
$datePickerLabel = New-Object System.Windows.Forms.Label
$datePickerLabel.Text = “date”
$datePickerLabel.Location = “15, 10”
$datePickerLabel.Height = 22
$datePickerLabel.Width = 90
$mainForm.Controls.Add($datePickerLabel)
# MinTimePicker Label
$minTimePickerLabel = New-Object System.Windows.Forms.Label
$minTimePickerLabel.Text = “min-time”
$minTimePickerLabel.Location = “15, 45”
$minTimePickerLabel.Height = 22
$minTimePickerLabel.Width = 90
$mainForm.Controls.Add($minTimePickerLabel)
# DatePicker
$datePicker = New-Object System.Windows.Forms.DateTimePicker
$datePicker.Location = “110, 7”
$datePicker.Width = “150”
$datePicker.Format = [windows.forms.datetimepickerFormat]::custom
$datePicker.CustomFormat = “dd/MM/yyyy”
$mainForm.Controls.Add($datePicker)
# MinTimePicker
$minTimePicker = New-Object System.Windows.Forms.DateTimePicker
$minTimePicker.Location = “110, 42”
$minTimePicker.Width = “150”
$minTimePicker.Format = [windows.forms.datetimepickerFormat]::custom
$minTimePicker.CustomFormat = "HH:mm:ss"
$minTimePicker.ShowUpDown = $TRUE
$mainForm.Controls.Add($minTimePicker)
# OD Button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = “15, 130”
$okButton.ForeColor = “Black”
$okButton.BackColor = “White”
$okButton.Text = “OK”
$okButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($okButton)
[void] $mainForm.ShowDialog()
$datePicker | select value
Current Output:
9/30/2021 12:59:34 PM
Desired Output:
After Selecting New date in GUI, it outputs New date only but not new Time.
-> Require help to get Both GUI selected Date & Time .
Related
My script takes in 3 number inputs and performs functions accordingly. For now I need to stop the entire script and re-run if I have new numbers to input. How can I use the same session to enter new numbers?
Add-Type -assembly System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$main_form = New-Object System.Windows.Forms.Form
# Box size
$main_form.Text ='ABC Pattern Calculator'
$main_form.Width = 450
$main_form.Height = 350
$main_form.AutoSize = $true
# Label1
$intitalLabel = New-Object System.Windows.Forms.Label
$intitalLabel.Text = "Click on Bull or Bear tab"
$intitalLabel.Location = New-Object System.Drawing.Point(30,20)
$intitalLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$intitalLabel.AutoSize = $true
$main_form.Controls.Add($intitalLabel)
############################# TABS #######################################
# Tab master control
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '400,250'
$MainTab.Location = '30,50'
$MainTab.Multiline = $true
$MainTab.AutoSize = $true
$MainTab.Anchor = 'Top,Left,Bottom,Right'
# Tab pages
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.TabIndex = 1
$Tabpage1.Text = 'Bullish Pattern'
$TabPage1.Name = 'Tab1'
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.TabIndex = 2
$Tabpage2.Text = 'Bearish Pattern'
$TabPage2.Name = 'Tab2'
# Add tabs to tab control
$MainTab.Controls.AddRange(#($TabPage1,$TabPage2))
$main_form.controls.Add($MainTab)
########################## Bull Tab #######################################
# Textbox = Point A
$aBullTextbox = New-Object System.Windows.Forms.TextBox
$aBullTextbox.Location = New-Object System.Drawing.Point(82,30)
$aBullTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($aBullTextbox)
# Textbox = Point B
$bBullTextbox = New-Object System.Windows.Forms.TextBox
$bBullTextbox.Location = New-Object System.Drawing.Point(82,80)
$bBullTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($bBullTextbox)
# Textbox = Point C
$cBullTextbox = New-Object System.Windows.Forms.TextBox
$cBullTextbox.Location = New-Object System.Drawing.Point(82,136)
$cBullTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($cBullTextbox)
# Label1
$aBullLabel = New-Object System.Windows.Forms.Label
$aBullLabel.Text = "A Low:"
$aBullLabel.Location = New-Object System.Drawing.Point(10,30)
$aBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$aBullLabel.AutoSize = $true
$main_form.Controls.Add($aBullLabel)
# Label2
$bBullLabel = New-Object System.Windows.Forms.Label
$bBullLabel.Text = "B High:"
$bBullLabel.Location = New-Object System.Drawing.Point(10,80)
$bBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$bBullLabel.AutoSize = $true
$main_form.Controls.Add($bBullLabel)
# Label3
$cBullLabel = New-Object System.Windows.Forms.Label
$cBullLabel.Text = "C Low:"
$cBullLabel.Location = New-Object System.Drawing.Point(10,136)
$cBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$cBullLabel.AutoSize = $true
$main_form.Controls.Add($cBullLabel)
# Radio button = Calculate
$bullCalculateButton = New-Object System.Windows.Forms.Button
$bullCalculateButton.Location = New-Object System.Drawing.Size(50,180)
$bullCalculateButton.Size = New-Object System.Drawing.Size(110,23)
$bullCalculateButton.Text = "Calculate"
$main_form.Controls.Add($bullCalculateButton)
# Math function
$bullCalculateButton.Add_Click({
$aBullCalc = $aBullTextbox.Text
$bBullCalc = $bBullTextbox.Text
$cBullCalc = $cBullTextbox.Text
$pointD = $bBullCalc - $aBullCalc + $cBullCalc
# D Label
$dBullLabel = New-Object System.Windows.Forms.Label
$dBullLabel.Text = "Point D will be at:"
$dBullLabel.Location = New-Object System.Drawing.Point(230,50)
$dBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dBullLabel.AutoSize = $true
$main_form.Controls.Add($dBullLabel)
# Answer to D
$dcalcLabel = New-Object System.Windows.Forms.Label
$dcalcLabel.Text = $pointD
$dcalcLabel.Location = New-Object System.Drawing.Point(280,70)
$dcalcLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dcalcLabel.AutoSize = $true
$main_form.Controls.Add($dcalcLabel)
$TabPage1.Controls.AddRange(#($dcalcLabel,$dBullLabel))
})
$TabPage1.Controls.AddRange(#($aBullTextbox,$bBullTextbox,$cBullTextbox,$aBullLabel,$bBullLabel,$cBullLabel,$bullCalculateButton))
########################## Bear Tab #######################################
# Textbox = Point A
$aBearTextbox = New-Object System.Windows.Forms.TextBox
$aBearTextbox.Location = New-Object System.Drawing.Point(82,30)
$aBearTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($aBearTextbox)
# Textbox = Point B
$bBearTextbox = New-Object System.Windows.Forms.TextBox
$bBearTextbox.Location = New-Object System.Drawing.Point(82,80)
$bBearTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($bBearTextbox)
# Textbox = Point C
$cBearTextbox = New-Object System.Windows.Forms.TextBox
$cBearTextbox.Location = New-Object System.Drawing.Point(82,136)
$cBearTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($cBearTextbox)
# Label1
$aBearLabel = New-Object System.Windows.Forms.Label
$aBearLabel.Text = "A High:"
$aBearLabel.Location = New-Object System.Drawing.Point(10,30)
$aBearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$aBearLabel.AutoSize = $true
$main_form.Controls.Add($aBearLabel)
# Label2
$bBearLabel = New-Object System.Windows.Forms.Label
$bBearLabel.Text = "B Low:"
$bBearLabel.Location = New-Object System.Drawing.Point(10,80)
$bBearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$bBearLabel.AutoSize = $true
$main_form.Controls.Add($bBearLabel)
# Label3
$cBearLabel = New-Object System.Windows.Forms.Label
$cBearLabel.Text = "C High:"
$cBearLabel.Location = New-Object System.Drawing.Point(10,136)
$cBearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$cBearLabel.AutoSize = $true
$main_form.Controls.Add($cBearLabel)
# Radio button = Calculate
$BearCalculateButton = New-Object System.Windows.Forms.Button
$BearCalculateButton.Location = New-Object System.Drawing.Size(50,180)
$BearCalculateButton.Size = New-Object System.Drawing.Size(110,23)
$BearCalculateButton.Text = "Calculate"
$main_form.Controls.Add($BearCalculateButton)
# Math function
$BearCalculateButton.Add_Click({
$aBearCalc = $aBearTextbox.Text
$bBearCalc = $bBearTextbox.Text
$cBearCalc = $cBearTextbox.Text
$pointDbear = $aBearCalc - $bBearCalc - $cBearCalc
# D Label
$dbearLabel = New-Object System.Windows.Forms.Label
$dbearLabel.Text = "Point D will be at:"
$dbearLabel.Location = New-Object System.Drawing.Point(230,50)
$dbearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dbearLabel.AutoSize = $true
$main_form.Controls.Add($dbearLabel)
# Answer to D
$dbearcalcLabel = New-Object System.Windows.Forms.Label
$dbearcalcLabel.Text = $pointDbear
$dbearcalcLabel.Location = New-Object System.Drawing.Point(280,70)
$dbearcalcLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dbearcalcLabel.AutoSize = $true
$main_form.Controls.Add($dbearcalcLabel)
$TabPage2.Controls.AddRange(#($dbearcalcLabel,$dbearLabel))
})
$TabPage2.Controls.AddRange(#($aBearTextbox,$bBearTextbox,$cBearTextbox,$aBearLabel,$bBearLabel,$cBearLabel,$BearCalculateButton))
# To show the box
$main_form.ShowDialog()
My script takes in 3 number inputs and performs functions accordingly. For now I need to stop the entire script and re-run if I have new numbers to input. How can I use the same session to enter new numbers?
You can simplify it by adding the labels to the main Form only once outside the .Add_Click event, then inside the event you can do your calculation and update their .Text property. This example demonstrates how to do it for the "Bullish Pattern" tab and you can do the same, following this example, for the "Bearish Pattern" one:
# Both labels are now outside the Event!
$dBullLabel = [Windows.Forms.Label]#{
Text = ''
Location = [Drawing.Point]::new(230, 50)
Font = [Drawing.Font]::new("Microsoft Sans Serif", 12, [Drawing.FontStyle]::Bold)
AutoSize = $true
}
$dcalcLabel = [Windows.Forms.Label]#{
Text = ''
Location = [Drawing.Point]::new(280, 70)
Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [Drawing.FontStyle]::Bold)
AutoSize = $true
}
$main_form.Controls.AddRange(#($dBullLabel, $dcalcLabel))
$TabPage1.Controls.AddRange(#($dcalcLabel, $dBullLabel))
$bullCalculateButton.Add_Click({
# Inside the event, we do just calculation
$pointD = [double] $aBullTextbox.Text - $bBullTextbox.Text - $cBullTextbox.Text
# and update
$dBullLabel.Text = "Point D will be at:"
$dcalcLabel.Text = $pointD
})
currently i am working on an little PowerShell GUI Porject.
I have a Form within this Form there's a Button to call another Form that provides an Listbox with some items in it.
So far so good - i'm getting the results as expected.
Now i mark one of the items - click on OK - then it should do something.
And here is my Problem, it does nothing :D No Error, no warning, just nothing.
If i test with write-host "$($listBox.SelectedItem)" - i get the selected Item as i expect it.
But if you want to use it with get-adfsrelyingpartytrust -Name $listBox.SelectedItem - nothing happens.
If i execute the function without the rest (so that only the listbox appears) everything works as intended O_o.
Code:
function single_rpt{ $RPTForm = New-Object System.Windows.Forms.Form
$RPTForm.ClientSize = '400,200'
$RPTForm.Text = "Choose"
$RPTForm.BackColor = "#b2b2b2"
$RPTForm.AutoSize = $false
$RPTForm.StartPosition = 'CenterScreen'
$RPTForm.ControlBox = $true
$rptButton = New-Object System.Windows.Forms.Button
$rptButton.BackColor = "#ffffff"
$rptButton.Text = "OK"
$rptButton.Width = 90
$rptButton.Height = 30
$rptButton.Location = New-Object System.Drawing.Point(20,125)
$rptButton.Font = 'Microsoft Sans Serif,10'
$rptButton.ForeColor = "#000000"
$rptButton.UseVisualStyleBackColor = $true
$rptButton.DialogResult = 1
# Test List Box
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(20,50)
$listBox.Size = New-Object System.Drawing.Size(350,200)
$listBox.Height = 80
$RPTForm.Controls.AddRange(#($listBox,$rptButton))
$listboxArray = #()
$ADFS_RPTS = Get-AdfsRelyingPartyTrust
Foreach($rpt in $ADFS_RPTS)
{
#$Object = Add-Member -TypeName NoteProperty -Name Service -Value $service.Name
$listboxArray += $rpt.Name
}
$listBox.Items.AddRange($listboxArray)
[void]$RPTForm.ShowDialog()
if($RPTForm.DialogResult -eq "OK")
{
Get-AdfsReylingPartyTrus -Name $listBox.SelectedItem
}
}
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create Form with size, title and background color, etc.
$TESTForm = New-Object System.Windows.Forms.Form
$TESTForm.ClientSize = '250,300'
$TESTForm.Text = "TEST"
$TESTForm.BackColor = "#ffffff"
$TESTForm.AutoSize = $false
$TESTForm.StartPosition = 'CenterScreen'
$TESTForm.ControlBox = $true
$RPTSingle = New-Object System.Windows.Forms.Button
$RPTSingle.BackColor = "#ffffff"
$RPTSingle.Text = "RPTSingle"
$RPTSingle.Width = 90
$RPTSingle.Height = 30
$RPTSingle.Location = New-Object System.Drawing.Point(75,125)
$RPTSingle.Font = 'Microsoft Sans Serif,10'
$RPTSingle.ForeColor = "#000000"
$RPTSingle.UseVisualStyleBackColor = $true
$RPTSingle.Add_Click({single_rpt})
# Add a Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.BackColor = "#ffffff"
$CancelButton.Text = "Cancel"
$CancelButton.Width = 90
$CancelButton.Height = 30
$CancelButton.Location = New-Object System.Drawing.Point(75,250)
$CancelButton.Font = 'Microsoft Sans Serif,10'
$CancelButton.ForeColor = "#000000"
$CancelButton.UseVisualStyleBackColor = $true
$CancelButton.DialogResult = 2
$TESTForm.Controls.AddRange(#($RPTSingle,$CancelButton))
# Display the form
[void]$TESTForm.ShowDialog()
#------------------------------------#
I am trying to open the next page of my Winforms script and atm on first launch the error is get when clicking the next button is "you cannot call a method on a null-valued expression" [void] $Form_UserInformation.ShowDialog() being the issue code here. When I run it the second time it runs fine but this doesn't work for an EXE file as it closes it and doesn't remember the previous session.
# HomePage Form
$Form_HomePage = New-Object System.Windows.Forms.Form
$Form_HomePage.text = "New User Script"
$Form_HomePage.Size = New-Object System.Drawing.Size(400,400)
$Form_HomePage.FormBorderStyle = "FixedDialog"
$Form_HomePage.TopMost = $false
$Form_HomePage.MaximizeBox = $false
$Form_HomePage.MinimizeBox = $true
$Form_HomePage.ControlBox = "true"
$Form_HomePage.StartPosition = "CenterScreen"
$Form_HomePage.Font = "Segoe UI"
# Title label
$label_HomeTitle = New-Object System.Windows.Forms.Label
$label_HomeTitle.Location = New-Object System.Drawing.Size(47,8)
$label_HomeTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeTitle.Font = New-Object System.Drawing.Font("Segoe UI",24,[System.Drawing.FontStyle]::Regular)
$label_HomeTitle.TextAlign = "MiddleCenter"
$label_HomeTitle.Text = "Kuehne && Nagel"
$Form_HomePage.Controls.Add($label_HomeTitle)
# Subheading label
$label_HomeSubTitle = New-Object System.Windows.Forms.Label
$label_HomeSubTitle.Location = New-Object System.Drawing.Size(50,60)
$label_HomeSubTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeSubTitle.Font = New-Object System.Drawing.Font("Segoe UI",16,[System.Drawing.FontStyle]::Regular)
$label_HomeSubTitle.TextAlign = "MiddleCenter"
$label_HomeSubTitle.Text = "New User Script"
$Form_HomePage.Controls.Add($label_HomeSubTitle)
# Next button
$button_HomeStart = New-Object System.Windows.Forms.Button
$button_HomeStart.Location = New-Object System.Drawing.Size(75,200)
$button_HomeStart.Size = New-Object System.Drawing.Size(240,32)
$button_HomeStart.TextAlign = "MiddleCenter"
$button_HomeStart.Text = "Start"
$button_HomeStart.Add_Click({
[void] $Form_UserInformation.ShowDialog()
$Form_HomePage.Hide()
})
$Form_HomePage.Controls.Add($button_HomeStart)
# About button
$button_About = New-Object System.Windows.Forms.Button
$button_About.Location = New-Object System.Drawing.Size(75,250)
$button_About.Size = New-Object System.Drawing.Size(240,32)
$button_About.TextAlign = "MiddleCenter"
$button_About.Text = "About"
$button_About.Add_Click({
})
$Form_HomePage.Controls.Add($button_About)
# Exit button
$button_HomeExit = New-Object System.Windows.Forms.Button
$button_HomeExit.Location = New-Object System.Drawing.Size(75,300)
$button_HomeExit.Size = New-Object System.Drawing.Size(240,32)
$button_HomeExit.TextAlign = "MiddleCenter"
$button_HomeExit.Text = "Exit"
$button_HomeExit.Add_Click({
$Form_HomePage.Close()
})
$Form_HomePage.Controls.Add($button_HomeExit)
# Show Form
$Form_HomePage.Add_Shown({$Form_HomePage.Activate()})
[void] $Form_HomePage.ShowDialog()
# User Information Form
$Form_UserInformation = New-Object System.Windows.Forms.Form
$Form_UserInformation.text = "New User Script"
$Form_UserInformation.Size = New-Object System.Drawing.Size(400,400)
$Form_UserInformation.FormBorderStyle = "FixedDialog"
$Form_UserInformation.TopMost = $false
$Form_UserInformation.MaximizeBox = $false
$Form_UserInformation.MinimizeBox = $true
$Form_UserInformation.ControlBox = "true"
$Form_UserInformation.StartPosition = "CenterScreen"
$Form_UserInformation.Font = "Segoe UI"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a script that will do the following:
Create a Form with 2 ComboBoxes and a Text Box for user entry
I populate ComboBox 1 and populate ComboBox 2 based on the selection from ComboBox 1. Another thing that partially works is I can get Text to appear on the form based on the selection of ComboBox 1.
My Challenges are challenges I have, by priority are:
Challenge 1:
How can I change the the text on the form doesn't update if I change the selection in ComboBox 1. selection in ComboBox 1?
Challenge 2: (linked to Challenge 1)
I am looking for a way to combing ComboBox 1 with ComboBox 2 with the Text Box
Example: Comp1-Tst-MyTst?
Challenge 3:
I'm looking to import a CSV file / OR using a Get-AD.... for the variables instead of hard coding in the script.
Challenge 4:
I'm struggling with adding an Icon to the form
Challenge 5:
I'd like to prevent the Ok Button from appearing until a Checkbox is checked
Here's the code I have:
# Below is one of the Array's I'm adding
$ADSites=#("S01","S02","S03")
$ADSiteS01=#("AAA","BBB","CCC")
$ADSiteS02=#("DDD","EEE","FFF")
$ADSiteS03=#("GGG","HHH","JJJ")
####################################################################################################
##### Create Combo Boxes
####################################################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(625,625)
$Form.FormBorderStyle = "FixedToolWindow"
$Combobox1 = New-Object System.Windows.Forms.Combobox
$Combobox1.Location = New-Object System.Drawing.Size(26,25)
$Combobox1.Size = New-Object System.Drawing.Size(105,20)
$Combobox1.items.AddRange($ADSites)
$combobox2 = New-Object System.Windows.Forms.Combobox
$combobox2.Location = New-Object System.Drawing.Size(143,25)
$combobox2.Size = New-Object System.Drawing.Size(105,20)
$textBoxFPS = New-Object System.Windows.Forms.TextBox
$textBoxFPS.Location = New-Object System.Drawing.Point(26,75)
$textBoxFPS.Size = New-Object System.Drawing.Size(165,20)
$form.Controls.Add($textBoxFPS)
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)
################################################################################################
##### Create Text Box
################################################################################################
$textBoxFPS = New-Object System.Windows.Forms.TextBox
$textBoxFPS.Location = New-Object System.Drawing.Point(26,75)
$textBoxFPS.Size = New-Object System.Drawing.Size(165,20)
$form.Controls.Add($textBoxFPS)
################################################################################################
##### Add Labels for the Combo Boxes
###################################################################################################
$lbADSub = New-Object System.Windows.Forms.Label
$lbADSub.Text = "Select AD Site"; $lbADSub.Top = 5; $lbADSub.Left = 26; $lbADSub.Autosize = $true
$form.Controls.Add($lbADSub)
$lbDeptSub = New-Object System.Windows.Forms.Label
$lbDeptSub.Text = "Select Department"; $lbDeptSub.Top = 5; $lbDeptSub.Left = 143;
$lbDeptSub.Autosize = $true
$form.Controls.Add($lbDeptSub)
$lbFPSSub = New-Object System.Windows.Forms.Label
$lbFPSSub.Text = "Type in the Asset Tag"; $lbFPSSub.Top = 55; $lbFPSSub.Left = 26;
$lbFPSSub.Autosize = $true
$form.Controls.Add($lbFPSSub)
## CheckBox
$chkThis = New-Object Windows.Forms.checkbox
$chkThis.Text = "Verify New Computer Name" ; $chkThis.Left = 26; $chkThis.Top = 105;
$chkThis.AutoSize = $true
$chkThis.Checked = $false # set a default value
$form.Controls.Add($chkThis)
<#
$lbCompSub = New-Object System.Windows.Forms.Label
$lbCompSub.Text = "Verify Computer Name"; $lbCompSub.Top = 105; $lbCompSub.Left = 26;
$lbCompSub.Autosize = $true
$form.Controls.Add($lbCompSub)
#>
############################################################################################
##### Create Ok and Cancel Buttons
############################################################################################
$buttonPanel = New-Object Windows.Forms.Panel
$buttonPanel.Size = New-Object Drawing.Size #(400,40)
$buttonPanel.Dock = "Bottom"
## Creating the Ok Button
$okButton = New-Object Windows.Forms.Button
$okButton.Top = $cancelButton.Top ; $okButton.Left = $cancelButton.Left - $okButton.Width - 5
$okButton.Text = "Ok"
$okButton.DialogResult = "Ok"
$okButton.Anchor = "Left"
## Creating the Cancel Button
$cancelButton = New-Object Windows.Forms.Button
$cancelButton.Left = $buttonPanel.Height - $cancelButton.Height - 10; $cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
$cancelButton.Text = "Cancel"
$cancelButton.DialogResult = "Cancel"
$cancelButton.Anchor = "Right"
## Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
## Add the button panel to the form
$form.Controls.Add($buttonPanel)
## Set Default actions for the buttons
$form.AcceptButton = $okButton # ENTER = Ok
$form.CancelButton = $cancelButton # ESCAPE = Cancel
##################################################################################################
##### Now we do stuff
##################################################################################################
# Populate Combobox 2 When Combobox 1 changes
$ComboBox1_SelectedIndexChanged= {
$combobox2.Items.Clear() # Clear the list
$combobox2.Text = $null # Clear the current entry
Switch ($ComboBox1.Text) {
"S01"{
$ADSiteS01 | ForEach {
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(20,200)
$labelClub.Size = New-Object System.Drawing.Size(280,20)
$labelClub.Text = "$($combobox1.SelectedItem)-"
$form.Controls.Add($labelClub)
$combobox2.Items.Add($_)
#$labelClub.Text = "$($combobox2.SelectedItem)-"
}
}
"S02"{
$ADSiteS02 | ForEach {
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(20,200)
$labelClub.Size = New-Object System.Drawing.Size(280,20)
$labelClub.Text = "$($combobox1.SelectedItem)-"
$form.Controls.Add($labelClub)
$combobox2.Items.Add($_)
}
}
"S03"{
$ADSiteS03 | ForEach {
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(20,200)
$labelClub.Size = New-Object System.Drawing.Size(280,20)
$labelClub.Text = "$($combobox1.SelectedItem)-"
$form.Controls.Add($labelClub)
$combobox2.Items.Add($_)
}
}
}
}
$ComboBox1.add_SelectedIndexChanged($ComboBox1_SelectedIndexChanged)
$Form.ShowDialog()
This should help you out on the combo box issue and the OK button:
# Below is one of the Array's I'm adding
$ADSites=#("S01","S02","S03")
$ADSiteS01=#("AAA","BBB","CCC")
$ADSiteS02=#("DDD","EEE","FFF")
$ADSiteS03=#("GGG","HHH","JJJ")
Add-Type -AssemblyName System.Windows.Forms
#
####################################################################################################
##### Create Combo Boxes
####################################################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.Size = '625,625'
$Form.FormBorderStyle = "FixedToolWindow"
#
$Combobox1 = New-Object System.Windows.Forms.Combobox
$Combobox1.Location = '26,25'
$Combobox1.Size = '105,20'
$Combobox1.items.AddRange($ADSites)
#
$combobox2 = New-Object System.Windows.Forms.Combobox
$combobox2.Location = '143,25'
$combobox2.Size = '105,20'
#
#
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)
################################################################################################
##### Create Text Box
################################################################################################
$textBoxFPS = New-Object System.Windows.Forms.TextBox
$textBoxFPS.Location = '26,75'
$textBoxFPS.Size = '165,20'
$textBoxFPS.Text = 'xx'
$form.Controls.Add($textBoxFPS)
################################################################################################
##### Add Labels for the Combo Boxes
###################################################################################################
$lbADSub = New-Object System.Windows.Forms.Label
$lbADSub.Text = "Select AD Site"; $lbADSub.Top = 5; $lbADSub.Left = 26; $lbADSub.Autosize = $true
$form.Controls.Add($lbADSub)
$lbDeptSub = New-Object System.Windows.Forms.Label
$lbDeptSub.Text = "Select Department"; $lbDeptSub.Top = 5; $lbDeptSub.Left = 143;
$lbDeptSub.Autosize = $true
$form.Controls.Add($lbDeptSub)
$lbFPSSub = New-Object System.Windows.Forms.Label
$lbFPSSub.Text = "Type in the Asset Tag"; $lbFPSSub.Top = 55; $lbFPSSub.Left = 26;
$lbFPSSub.Autosize = $true
$form.Controls.Add($lbFPSSub)
## CheckBox
$chkThis = New-Object Windows.Forms.checkbox
$chkThis.Text = "Verify New Computer Name" ; $chkThis.Left = 26; $chkThis.Top = 105;
$chkThis.AutoSize = $true
$chkThis.Checked = $false # set a default value
$form.Controls.Add($chkThis)
############################
# Label for choice selection
############################
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = '20,200'
$labelClub.Size = '280,20'
$labelClub.Text = "-"
$form.Controls.Add($labelClub)
<#
$lbCompSub = New-Object System.Windows.Forms.Label
$lbCompSub.Text = "Verify Computer Name"; $lbCompSub.Top = 105; $lbCompSub.Left = 26;
$lbCompSub.Autosize = $true
$form.Controls.Add($lbCompSub)
#>
############################################################################################
##### Create Ok and Cancel Buttons
############################################################################################
$buttonPanel = New-Object Windows.Forms.Panel
$buttonPanel.Size = New-Object Drawing.Size #(400,40)
$buttonPanel.Dock = "Bottom"
## Creating the Ok Button
$okButton = New-Object Windows.Forms.Button
$okButton.Top = $cancelButton.Top ; $okButton.Left = $cancelButton.Left - $okButton.Width - 5
$okButton.Text = "Ok"
$okButton.DialogResult = "Ok"
$okButton.Anchor = "Left"
$okButton.Enabled = $false
## Creating the Cancel Button
$cancelButton = New-Object Windows.Forms.Button
$cancelButton.Left = $buttonPanel.Height - $cancelButton.Height - 10; $cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
$cancelButton.Text = "Cancel"
$cancelButton.DialogResult = "Cancel"
$cancelButton.Anchor = "Right"
## Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
## Add the button panel to the form
$form.Controls.Add($buttonPanel)
## Set Default actions for the buttons
$form.AcceptButton = $okButton # ENTER = Ok
$form.CancelButton = $cancelButton # ESCAPE = Cancel
##################################################################################################
##### Now we do stuff
##################################################################################################
# Populate Combobox 2 When Combobox 1 changes
$ComboBox1.add_SelectedIndexChanged({
$combobox2.Items.Clear() # Clear the list
$combobox2.Text = $null # Clear the current entry
Switch ($ComboBox1.Text) {
"S01"{
$ADSiteS01 | ForEach {
$combobox2.Items.Add($_)
}
}
"S02"{
$ADSiteS02 | ForEach {
$combobox2.Items.Add($_)
}
}
"S03"{
$ADSiteS03 | ForEach {
$combobox2.Items.Add($_)
}
}
}
$labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})
$ComboBox2.add_SelectedIndexChanged({
$labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})
$textBoxFPS.add_TextChanged({
$labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})
$chkThis.Add_CheckStateChanged({
If ($chkThis.Checked) {
$okButton.enabled = $true
}
Else {
$okButton.enabled = $false
}
})
$Form.ShowDialog()
If you use an AD cmdlet to retrieve site details into an object you should easily be able to populate lists from that object using the same method you have now.
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