Header Check Box in DataGridView - powershell

can anyone show me an example on how to add header check box in a datagridview with powershell? I am searching on the web with similar example but i can find one. I am trying to create a select all checkbox
Your help is very much appreciated. Thanks.
$form = New-Object system.Windows.Forms.Form
$dataGrid1 = New-Object System.Windows.Forms.DataGridView
$dataGrid1.Location = New-Object Drawing.Point 20,90
$dataGrid1.size = New-Object Drawing.Point 260,200
$dataGrid1.MultiSelect = $false
$dataGrid1.ColumnHeadersVisible = $true
$dataGrid1.RowHeadersVisible = $false
$datagridviewcheckboxcolumn1 = New-Object 'System.Windows.Forms.DataGridViewCheckBoxColumn'
$dataGrid1.Columns.Add($datagridviewcheckboxcolumn1) | Out-Null
$form.Controls.Add($dataGrid1)
$form.ShowDialog()

Something like this?
$datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
$Col1 = New-Object 'System.Windows.Forms.DataGridViewCheckBoxColumn'
# datagridview1
$datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
[void]$datagridview1.Columns.Add($Col1)
$datagridview1.Dock = 'Fill'
$datagridview1.Location = '0, 0'
$datagridview1.Name = "datagridview1"
$datagridview1.Size = '284, 262'
$datagridview1.TabIndex = 0
# Col1
$Col1.HeaderText = "Add Header Here"
$Col1.Name = "Add Name Here"`

Related

How to use ShowCheckMargin properties in Powershell

I'm trying to create a form with a menu in PowerShell. I want to have a check item in my menu. Please check the picture. I did some research and I found out that I can use the showCheckMargin properties but I'm not sure how to accomplish what I have in the picture. Any ideas ?
below is my form
$SubMenu_Open = New-Object System.Windows.Forms.ToolStripMenuItem("Open")
$SubMenu_Save = New-Object System.Windows.Forms.ToolStripMenuItem("Save")
$SubMenu_Exit = New-Object System.Windows.Forms.ToolStripMenuItem("Exit")
$Menu_Settings = New-Object System.Windows.Forms.ToolStripMenuItem("Settings")
$SubMenu_Rest = New-Object System.Windows.Forms.ToolStripMenuItem("Reset")
$SubMenu_Logs = New-Object System.Windows.Forms.ToolStripMenuItem("Deployment Logs")
$SubMenu_Errors = New-Object System.Windows.Forms.ToolStripMenuItem("Errors Logs")
$Menu_View = New-Object System.Windows.Forms.ToolStripMenuItem("View")
$SubMenu_DarkTheme = New-Object System.Windows.Forms.ToolStripMenuItem("Dark theme")
$SubMenu_LightTheme = New-Object System.Windows.Forms.ToolStripMenuItem("Light theme")
$Menu_Help = New-Object System.Windows.Forms.ToolStripMenuItem("Help")
$Menu_About = New-Object System.Windows.Forms.ToolStripMenuItem("About")
#4. Add the Components to the Form
#-----------------------------------------------------------------------------------------
$Menu_File.DropDownItems.Add($SubMenu_Open)
$Menu_File.DropDownItems.Add($SubMenu_Save)
$Menu_File.DropDownItems.Add($SubMenu_Exit)
$Menu_Settings.DropDownItems.Add($SubMenu_Rest)
$Menu_Settings.DropDownItems.Add($SubMenu_Logs)
$Menu_Settings.DropDownItems.Add($SubMenu_Errors)
$Menu_View.DropDownItems.Add($SubMenu_DarkTheme)
$Menu_View.DropDownItems.Add($SubMenu_LightTheme)
$MainMenu.Items.Add($Menu_File)
$MainMenu.Items.Add($Menu_Settings)
$MainMenu.Items.Add($Menu_View)
$MainMenu.Items.Add($Menu_Help)
$MainMenu.Items.Add($Menu_About)```

System.Windows.Forms.TabPage - add_LostFocus and add_GotFocus not working?

I have a form with multiple TabPages. I have buttons that I want to only be visible when one tab is in focus. Here's my code:
$form = New-Object system.Windows.Forms.Form
$form.ClientSize = New-Object System.Drawing.Point(350,380)
$form.text = "Form"
$tabcontrol = New-object System.Windows.Forms.TabControl
$tabcontrol.Size = New-Object System.Drawing.Point(330,330)
$tabcontrol.Location = New-Object System.Drawing.Point(10,10)
$form.Controls.Add($tabcontrol)
$tab0 = New-object System.Windows.Forms.Tabpage
$tab0.DataBindings.DefaultDataSourceUpdateMode = 0
$tab0.UseVisualStyleBackColor = $True
$tab0.Text = "Tab0"
$tab0.AutoScroll = $true
$tabcontrol.Controls.Add($tab0)
$add_button = New-Object System.Windows.Forms.Button
$add_button.Size = New-Object System.Drawing.Size(23,23)
$add_button.Location = New-Object System.Drawing.Point(10,350)
$add_button.Text = "+"
$form.Controls.Add($add_button)
$remove_button = New-Object System.Windows.Forms.Button
$remove_button.Size = New-Object System.Drawing.Size(23,23)
$remove_button.Location = New-Object System.Drawing.Point(43,350)
$remove_button.Text = "-"
$form.Controls.Add($remove_button)
$tab0.add_lostFocus({
$add_button.Hide()
$remove_button.Hide()
})
$tab0.add_gotFocus({
$add_button.Show()
$remove_button.Show()
})
$tab1 = New-object System.Windows.Forms.Tabpage
$tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$tab1.UseVisualStyleBackColor = $True
$tab1.Text = "Tab1"
$tabcontrol.Controls.Add($tab1)
Despite the add_GotFocus, and add_LostFocus on $tab0, when I change between tabs, the buttons stay visible.
What am I doing wrong and how can I accomplish this?
I believe the events you're looking for are Enter and Leave:
$tab0.Add_Leave({
$add_button, $remove_button | ForEach-Object Hide
})
$tab0.Add_Enter({
$add_button, $remove_button | ForEach-Object Show
})

How to create CheckBox automatically using PowerShell?

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()

Why OpenFileDialog found if execute from ISE but not from cmd?

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

Powershell DataGridView - looping through rows in checkbox column to see if checked

I'm trying to create a powershell form that will get all processes into a datagridview (which works OK so far). I'm then adding an additional column infront of the datasource with each row as a checkbox.
I'd then like to press a button on my form which starts the Function called Do-Grid, and it is meant to go through and see which checkboxes in this first column have been checked.
And this is where I get stuck. I'm unable to figure out how to loop through each row/cell only in this column and see which checkbox has been ticked (or have the value of true).
Thank you.
Function GenerateForm {
[Void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ButtonUpdateGrid = New-Object Windows.Forms.Button
$ButtonUpdateGrid.Location = New-Object Drawing.Point 7,15
$ButtonUpdateGrid.size = New-Object Drawing.Point 90,23
$ButtonUpdateGrid.Text = "&Get Packages"
$ButtonUpdateGrid.add_click({Get-info})
$Button1 = New-Object Windows.Forms.Button
$Button1.Location = New-Object Drawing.Point 200,15
$Button1.size = New-Object Drawing.Point 90,23
$Button1.Text = "columns"
$button1.add_Click($button1_OnClick)
$ButtonStartExport = New-Object Windows.Forms.Button
$ButtonStartExport.Location = New-Object Drawing.Point 100,15
$ButtonStartExport.size = New-Object Drawing.Point 90,23
$ButtonStartExport.Text = "rows"
$ButtonStartExport.add_click({Do-Grid})
$dataGridView1 = New-Object System.Windows.Forms.DataGridView
$dataGridView1.Location = New-Object Drawing.Point 7,40
$dataGridView1.size = New-Object Drawing.Point 1000,700
$dataGridView1.MultiSelect = $false
$dataGridView1.ColumnHeadersVisible = $true
$dataGridView1.RowHeadersVisible = $false
$form = New-Object Windows.Forms.Form
$form.text = "Package Exporter v0.1"
$form.Size = New-Object Drawing.Point 1024, 768
$form.topmost = 1
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$form.Controls.Add($dataGridView1)
$form.controls.add($Button1)
$form.controls.add($ButtonStartExport)
$form.controls.add($ButtonUpdateGrid)
#$form.add_Load($OnLoadForm)
$form.ShowDialog()
}
Function Get-info{
If($datagridview1.columncount -gt 0){
$dataGridview1.DataSource = $null
$DataGridView1.Columns.RemoveAt(0)
}
$Column1 = New-Object System.Windows.Forms.DataGridViewCheckBoxColumn
$Column1.width = 30
$Column1.name = "Exp"
$DataGridView1.Columns.Add($Column1)
$array = New-Object System.Collections.ArrayList
$Script:procInfo = get-process | Select-Object name, company, description, product, id, vm, fileversion
$array.AddRange($procInfo)
$dataGridview1.DataSource = $array
$form.refresh()
}
Function Do-Grid{
#?????????????????????????????????? not really sure what to do here
for($i=0;$i -le $datagridview1.Rows.Count;$i++){
write-host $datagridview1.Rows[$i].value
}
}
Generate Form
Function Do-Grid{
for($i=0;$i -lt $datagridview1.RowCount;$i++){
if($datagridview1.Rows[$i].Cells['exp'].Value -eq $true)
{
write-host "cell #$i is checked"
#uncheck it
#$datagridview1.Rows[$i].Cells['exp'].Value=$false
}
else
{
#check it
#$datagridview1.Rows[$i].Cells['exp'].Value=$true
write-host "cell #$i is not-checked"
}
}
}