I want to create checkbox automatically consider to the total a file inside a folder I selected.
In my script, the checkbox always create one checkbox, but my file more than one. And the checkbox always appear before I select the folder from listbox. Please give me advice if anyone know about this, Thank you so much.
This is the result look like
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(500,700)
$objForm.StartPosition = "CenterScreen"
$objForm.Text = "Test"
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,70)
$objListBox.Size = New-Object System.Drawing.Size(260,30)
$objListBox.Height = 250
$objListBox.add_SelectedIndexChanged($SelectedFile)
$objForm.Controls.Add($objListBox)
$objChktBox = New-Object System.Windows.Forms.CheckBox
$objChktBox.Location = New-Object System.Drawing.Size(10,400)
$objChktBox.Size = New-Object System.Drawing.Size(400,3000)
$objChktBox.Height = 200
$objForm.Controls.Add($objChktBox)
# Populate list.
#(Get-ChildItem -Directory ".\").Name | ForEach-Object {[void] $objListBox.Items.Add($_)}
$SelectedFile=
{
$objChktBox.Text = (Get-ChildItem $objListbox.SelectedItem)
}
$objForm.ShowDialog()
Related
I'm trying to make a GUI for an easy tool I need to check if a specific certificate is included in the file and Output the current and next line. This already works.
If I set the variables $INT1 and $Serial1 manually within the code it also works as intended.
e.g. $INT1=31 and $Serial1="5AA61E07726DAC13".
I always get an Error that the variable is empty when inputing the same values into the GUI and clicking OK. What am I doing wrong?
The code:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(400,250)
$objForm.Topmost = $True
#Label+Textbox first question
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,30)
$objLabel.Size = New-Object System.Drawing.Size(800,20)
$objLabel.Text = "INT of certificate"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,50)
$objTextBox.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox)
#Label+Textbox second question
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,130)
$objLabel2.Size = New-Object System.Drawing.Size(800,20)
$objLabel2.Text = "Serialnumber of the certificate"
$objForm.Controls.Add($objLabel2)
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,150)
$objTextBox2.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox2)
$INT1 = $objTextBox.Text;
$Serial1 = $objTextBox2.Text;
#OK Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,175)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Name = "OK"
#$OKButton.DialogResult = "OK"
$OKButton.Add_Click({Invoke-WebRequest -Uri http://CENSORED.crl -OutFile crl.crl;
$test=certutil.exe -dump crl.crl |Out-String -stream | Select-String -pattern "$Serial1" -Context 0,1
[void] [Windows.Forms.MessageBox]::Show($test)})
$objForm.Controls.Add($OKButton)
[void] $objForm.ShowDialog()
Thank you!
Here's a rewrite of your code.
I'm assuming that you want variables $INT1 and $Serial1 to be available after the windows closes.
In that case, fill them inside the Click() scriptblock and refer to them using $script: scoping, otherwise they are just new variables local to the scriptblock.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# these variables need to visible inside the Click scriptblock of the button
# and be also available after the windows closes
$INT1 = $null
$Serial1 = $null
$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(400,250)
$objForm.Topmost = $True
#Label+Textbox first question
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,30)
$objLabel.Size = New-Object System.Drawing.Size(800,20)
$objLabel.Text = "INT of certificate"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,50)
$objTextBox.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox)
#Label+Textbox second question
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,130)
$objLabel2.Size = New-Object System.Drawing.Size(800,20)
$objLabel2.Text = "Serialnumber of the certificate"
$objForm.Controls.Add($objLabel2)
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,150)
$objTextBox2.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objTextBox2)
#OK Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,175)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Name = "OK"
#$OKButton.DialogResult = "OK"
$OKButton.Add_Click({
# you never seem to use variable $INT1, but if you do, refer to it as $script:INT1
# set the variables defined OUTSIDE the scriptblock using script-scoping
$script:INT1 = $objTextBox.Text
$script:Serial1 = $objTextBox2.Text
# create a full path and filename for the output of the Invoke-WebRequest call
$crlFile = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'crl.crl'
Invoke-WebRequest -Uri 'http://CENSORED.crl' -OutFile $crlFile
$test = certutil.exe -dump $crlFile | Out-String -Stream | Select-String -Pattern $script:Serial1 -Context 0,1
[void] [Windows.Forms.MessageBox]::Show($test)
# delete the temporary file
$crlFile | Remove-Item -Force
})
$objForm.Controls.Add($OKButton)
[void] $objForm.ShowDialog()
# clean up the form from memory'
$objForm.Dispose()
# here you can again see what is inside variables $INT1 and $Serial1
Write-Host "Last entered values: $INT1 $Serial1"
All user interaction with your form happens in this, blocking call:
[void] $objForm.ShowDialog()
See the ShowDialog() method.
Therefore, if you want to store results from that user interaction in variables, place the assignments after the call, and reference the controls of interest.
# ... form setup
# Show the form modally (as a dialog).
# The call blocks until the form is closed.
# Note:
# You may want to examine the return value in case there
# are multiple ways to close your form, and one of those ways
# signals having *canceled*, for instance.
$null = $objForm.ShowDialog()
# Now you can get values from the controls and assign them to (script) variables.
$INT1 = $objTextBox.Text
$Serial1 = $objTextBox2.Text
Note: If you need to set script-level variables - i.e., variables visible after a .ShowDialog() call - from inside script blocks serving as event delegates (e.g., the script block passed to $OKButton.Add_Click()), you need to use the $script: scope, given that such script blocks run in a child scope too - see this answer.
I want to be able to display the currently highlighted item from a scrolling list box, without having to click it.
I have set up the list box so that only one item is displayed. The selection changes via the scroll buttons. I want to be able to read the item displayed without having to click it - so .Add_Click is not suitable.
Example code below:
Many thanks
# Listbox test
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# set up form
$form = New-Object System.Windows.Forms.Form
$form.Text = "List Box Test"
$form.Size = New-Object System.Drawing.Size(500,200)
$form.StartPosition = "Manual"
$form.Location = New-Object System.Drawing.Point(10, 10)
# Set up list
$List1 = New-Object System.Windows.Forms.ListBox
$List1.Location = New-Object System.Drawing.Point(10, 10)
$List1.Height = 30
$List1.Width = 150
$List1.font = 'arial, 16pt'
[void] $List1.Items.Add('Clubs')
[void] $List1.Items.Add('Diamonds')
[void] $List1.Items.Add('Hearts')
[void] $List1.Items.Add('Spades')
$form.Controls.Add($List1)
# Question: how do I get the highlighted item from the list box to show here, before it is clicked?
# ----------------------------------------------------------------------------------------
# display selection
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Point(10,50)
$TextBox1.Size = New-Object System.Drawing.Size(300,30)
$TextBox1.Font = 'arial, 12pt'
$TextBox1.text = "Highlighted item to show here ..."
$form.Controls.Add($TextBox1)
# ----------------------------------------------------------------------------------------
# set up button
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Point(10,100)
$Button1.Size = New-Object System.Drawing.Size(50,20)
$Button1.Text = "OK"
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Controls.Add($Button1)
$form.Topmost = $true
$null = $form.ShowDialog()
Just add a SelectedIndexChanged event handler to the listbox:
$List1.Add_SelectedIndexChanged({
$TextBox1.Text = $this.SelectedItem.ToString() # or do: $this.items[$this.SelectedIndex].ToString()
})
Since a Listbox only contains text, you could leave out the .ToString().
P.S. Don't forget to remove the form from memory when all done with
$form.Dispose()
I'm very rusty in the area of powershell and trying to pick it back up but I have something bothering me that I can't seem to figure out. I've created a form that has two text inputs for file locations and a drop down list. What I'm attempting to do is compare the two files received in a text input. I've got the script already for the compare but what I'm trying to figure out is how I distinguish the script to be run from the selection of the drop down list and also parse the two text inputs from the form to the relevant compare script. Below is the generic form created. Any help would be much appreciated.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(700,700)
#You can use the below method as well
#$Form.Width = 400
#$Form.Height = 200
$form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "IAM Application Comparison Tool"
#application Drop down list
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Please select the application being compared"
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(10,10)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
#list of applications
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,50)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
[void] $listBox.Items.Add('Terradata')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
$form.Controls.Add($listBox)
#File path of previous months file
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Enter file path of previous months file"
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,200)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
#Input file path
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(20,300)
$textBox.Size = New-Object System.Drawing.Size(450,20)
$form.Controls.Add($textBox)
#$formIcon = New-Object system.drawing.icon ("$env:USERPROFILE\desktop\Blog\v.ico")
#$form.Icon = $formicon
#File path of current month
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Enter file path of current months file"
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(20,400)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
#Input file path
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(20,500)
$textBox.Size = New-Object System.Drawing.Size(450,20)
$form.Controls.Add($textBox)
#Run Required Compare script
$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(170,600)
$Okbutton.Size = New-Object System.Drawing.Size(200,30)
$Okbutton.Text = "Compare Files"
$Okbutton.Add_Click()
$Form.Controls.Add($Okbutton)
$Form.ShowDialog()
Create a function that will run whenever the button is clicked.
for instance, your button event will be:
$Okbutton.Add_Click({ BtnClick })
then add the function that will do the compare, whenever the button is clicked the function will be called:
function BtnClick{
# do something / compare your texts
#compare $TextBox1.Text $TextBox2.Text
}
you have to give a different variable name for each textbox, this how you'll be able to access the values:
$input1 = $TextBox1.Text
$input2 = $TextBox2.Text
$input3 = $ComboBox1.Text
The easiest way to have your code distinguish between the different objects is to name them upon creation, so use different variable names for the textboxes and the like.
To act on the button click, you already started by writing $Okbutton.Add_Click(), but since there is no scriptblock to actually do something there, nothing would happen.
The listbox can also react on an event, in this case that would be the SelectedIndexChanged event.
Please see the revised code below that shows how to implement these event handlers
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(700,700)
$Form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "IAM Application Comparison Tool"
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
#application Drop down list
$LabelDropDown = New-Object System.Windows.Forms.Label
$LabelDropDown.Text = "Please select the application being compared"
$LabelDropDown.AutoSize = $true
$LabelDropDown.Location = New-Object System.Drawing.Size(10,10)
$Form.Controls.Add($LabelDropDown)
# list of applications
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,50)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
# add the items to the listbox
$null = $listBox.Items.AddRange(#('Terradata','SomeApp','MyApp','ThirdPartyApp'))
# set the current selected item to be the first
$listBox.SelectedIndex = 0
# add functionality to react on index changed
$listBox.Add_SelectedIndexChanged({
# do something when the user chose a different app in the listbox
# for demo, just write the currently selected item in the console
Write-Host "Currently selected app in the listbox: $($this.SelectedItem)"
})
$Form.Controls.Add($listBox)
# File path of previous months file
$LabelPrevious = New-Object System.Windows.Forms.Label
$LabelPrevious.Text = "Enter file path of previous months file"
$LabelPrevious.AutoSize = $true
$LabelPrevious.Location = New-Object System.Drawing.Size(20,200)
$Form.Controls.Add($LabelPrevious)
# first Input file path
$textBoxFile1 = New-Object System.Windows.Forms.TextBox
$textBoxFile1.Location = New-Object System.Drawing.Point(20,300)
$textBoxFile1.Size = New-Object System.Drawing.Size(450,20)
$Form.Controls.Add($textBoxFile1)
# File path of current month
$LabelCurrent = New-Object System.Windows.Forms.Label
$LabelCurrent.Text = "Enter file path of current months file"
$LabelCurrent.AutoSize = $true
$LabelCurrent.Location = New-Object System.Drawing.Size(20,400)
$Form.Controls.Add($LabelCurrent)
# second Input file path
$textBoxFile2 = New-Object System.Windows.Forms.TextBox
$textBoxFile2.Location = New-Object System.Drawing.Point(20,500)
$textBoxFile2.Size = New-Object System.Drawing.Size(450,20)
$Form.Controls.Add($textBoxFile2)
# Run Required Compare script
$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(170,600)
$Okbutton.Size = New-Object System.Drawing.Size(200,30)
$Okbutton.Text = "Compare Files"
# add functionality for comparing files
$Okbutton.Add_Click({
# your compare function comparing the file in $textbox1 against the file in $textbox2
# IF both fields contain a valid file path and name of course ;)
# for demo just output in console
Write-Host "Compare file '$($textBoxFile1.Text)' to '$($textBoxFile2.Text)'"
})
$Form.Controls.Add($Okbutton)
$Form.ShowDialog()
# important! dispose of the form when done
$Form.Dispose()
P.S. Inside the scriptblocks for the Click and the SelectedIndexChanged events, you can use automatic variable $this, which refers to the current form control object.
I'm not entirely sure how to word this so bare with me. The orginization i work for is taking away a license for a software we use to connect to our network switches using a basic SSH connection. Just using PowerShell i know this command works for us. ssh username#IP_of_Switch now comes the fun part.
I want to make a GUI for our techs so they just have to select the switch name and not have to know every IP of all 100+ switches. I know for a drop down box it would look like this
$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(20,50)
$DropDownBox.Size = New-Object System.Drawing.Size(180,20)
$DropDownBox.DropDownHeight = 200
$Form.Controls.Add($DropDownBox)
$swList=#("1D","3D","3F","6E","Laundry","Chapel")
foreach ($sws in $swList) {$DropDownBox.Items.Add($sw)} #end foreach
What i want to do is have those Names be associated with their respective IP some how. AND HAVE A BUTTON TO CONNECT THEM TO SELECTED SWITCHI have a slight idea that i may need to have a txt file with the IP's. Any help would be greatly appreciated.
ComboBox is inherited from ListControl
You should provide Objects to ListItems. Each object should contain at least 2 properties: displayName and value ( you can change names of properties how you want ).
You need to set ComboBox's properties DisplayMember to name of property should be displayed.
Example:
In $FormEvent_Load scriptblock, we add items to ComboBox. Those items are objects with (at least) 2 properties - Name and Address. There we also set DisplayMember property of ComboBox to let it know, which property should be displayed in UI.
In $CBEvent_SelectedIndexChanged scriptblock, we get SelectedItem property from ComboiBox. SelectedItem contains original object we passed to ComboBox.Items.Add. There is Address field as you see, which can be used.
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$combobox1 = New-Object System.Windows.Forms.ComboBox
$label1 = New-Object System.Windows.Forms.Label
$FormEvent_Load = {
$combobox1.DisplayMember = 'Name'
$combobox1.Items.Add(
[PSCustomObject]#{
'Name' = 'Switch1'
'Address' = [ipaddress]::Parse('1.1.1.1')
})
$combobox1.Items.Add(
[PSCustomObject]#{
'Name' = 'Switch2'
'Address' = [ipaddress]::Parse('2.12.21.22')
})
}
$CBEvent_SelectedIndexChanged = {
$label1.Text = $combobox1.SelectedItem.Address.ToString()
}
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 292
$System_Drawing_Size.Height = 266
$form1.ClientSize = $System_Drawing_Size
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 83
$System_Drawing_Point.Y = 199
$label1.Location = $System_Drawing_Point
$label1.Text = 'Select IP'
$form1.Controls.Add($label1)
$form1.Controls.Add($combobox1)
$form1.add_Load($FormEvent_Load)
$ComboBox1.add_SelectedIndexChanged($CBEvent_SelectedIndexChanged)
$form1.ShowDialog()| Out-Null
This is just an example but it does work. I used a simple csv file to hold the switch IP Addresses. I edited an answer for the majority of this script so I will give credit for that Powershell default dropdown value
CSV File
1D,127.0.0.1
3D,127.0.0.2
3F,127.0.0.3
6E,127.0.0.4
Laundry,127.0.0.5
Chapel,127.0.0.6
Script
########################
# Edit This item to change the DropDown Values
$switchesFilePath = "C:\temp\switches.csv"
$switches = ConvertFrom-Csv (Get-Content -Path $switchesFilePath)
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$script:Choice = $DropDown.SelectedItem
Write-Host "The selected switch is $($script:Choice.Name) and it's IP is $($script:Choice.IPAddress)"
Write-Host "The ssh command would be ---- ssh username#$($script:Choice.IPAddress)"
$Form.Close()
}
function selectSwitch{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
$DropDown.SelectedItem = $DropDown.Items[0]
$DropDown.DisplayMember = 'Name'
[void] $DropDown.Items.AddRange($switches)
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,40)
$DropDownLabel.Text = "Select a network switch"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Connect to a switch"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
return $script:choice
}
$switch = selectSwitch
im creating a gui script for users to get certain files, its not finished yet but i have a problem i cant solve.
i want to have choices of which file they want + an option for them to insert a name that they want for the file that they chose.
the problem is the text box is cut in the middle and only if you write a lot you will see the end of your text.
please help! thanks a lot!
this is the full script: *again-not complete
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#This creates the path for the Json and also check if it is already there.
If(!(Test-Path -Path C:\Users\$env:USERNAME\documents\Json))
{
New-Item c:\users\$env:USERNAME\documents -ItemType directory -Name Json
$path = "c:\users\$env:USERNAME\documents\Json"
}
#creating the form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Ofir`s script"
$objForm.Size = New-Object System.Drawing.Size(480,200)
$objForm.StartPosition = "CenterScreen"
#creating the label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(20,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please check the relevant boxes:"
$objForm.Controls.Add($objLabel)
#This creates a checkbox called dsp.z
$objDspCheckbox = New-Object System.Windows.Forms.Checkbox
$objDspCheckbox.Location = New-Object System.Drawing.Size(20,40)
$objDspCheckbox.Size = New-Object System.Drawing.Size(280,20)
$objDspCheckbox.Text = "dsp.z"
$objDspCheckbox.TabIndex = 0
$objForm.Controls.Add($objDspCheckbox)
#This creates a checkbox called fpga.bin
$objFpgaCheckbox = New-Object System.Windows.Forms.Checkbox
$objFpgaCheckbox.Location = New-Object System.Drawing.Size(20,60)
$objFpgaCheckbox.Size = New-Object System.Drawing.Size(280,20)
$objFpgaCheckbox.Text = "fpga.bin"
$objFpgaCheckbox.TabIndex = 1
$objForm.Controls.Add($objFpgaCheckbox)
#This creates a checkbox called bootrom_uncmp.bin
$objBootCheckbox = New-Object System.Windows.Forms.Checkbox
$objBootCheckbox.Location = New-Object System.Drawing.Size(20,80)
$objBootCheckbox.Size = New-Object System.Drawing.Size(280,20)
$objBootCheckbox.Text = "bootrom_uncmp.bin"
$objBootCheckbox.TabIndex = 2
$objForm.Controls.Add($objBootCheckbox)
#This creates a label for the TextBox1
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(300,20)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "Change the name?:"
$objForm.Controls.Add($objLabel1)
#This creates the TextBox1
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(200,40)
$objTextBox1.Size = New-Object System.Drawing.Size(200,20)
$objTextBox1.TabIndex = 3
$objForm.Controls.Add($objTextBox1)
#ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(40,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click(
{
if ($objDspCheckbox.Checked -eq $true)
{
New-Item $path -itemtype file -name Dsp.json -value #"
"{"sys_ver":"01.01.01.02","RED":[],"RED_VA":[],"BLACK": [{"type":"6","type_descr":"DSP file","tar_name":"dsp.tar.gz","image_name":"dsp.z","CRC":"1234567","version":"01.01.01.02","metadata":"62056"}
],"BLACK_VA":[]
}"
"# ;$objForm.close()
}
elseif ($objFpgaCheckbox.Checked -eq $true)
{
New-Item $path -itemtype file -name Fpga.json -value #"
"
{"type":"4","type_descr":"FPGA file","tar_name":"FPGA.tar.gz","image_name":"fpga.z","CRC":"1234567","version":"01.01.01.02","metadata":"9730652"},
"
"#
;$objForm.close()
}
elseif ($objBootCheckbox.Checked -eq $true)
{
New-Item $path -itemtype file -name Boot.json -value "Hello3" ;$objForm.close()
}
})
$objForm.Controls.Add($OKButton)
#cancle Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(140,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
#Parameters (Need to add)
$dspname = $objTextBox1
#makes the form appear on top of the screen
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
the problem is the text box is cut in the middle and only if you write a lot you will see the end of your text. please help! thanks a lot!
The problem is that the TextBox control is hidden underneath your CheckBox controls, because you've configured them to be ridiculously wide.
Change:
$objDspCheckbox.Size = New-Object System.Drawing.Size(280,20)
to something more sensible, like 150px instead of 280px:
$objDspCheckbox.Size = New-Object System.Drawing.Size(150,20)
Do this for all the checkboxes and you'll find the problem goes away.
Alternatively, bring the $objTextBox1 control to the front by setting a ChildIndex of 0 on it's parent (the Form object itself):
$objForm.Controls.SetChildIndex($objTextBox1,0)
after adding all child controls (but before calling $objForm.ShowDialog())