How to use ShowCheckMargin properties in Powershell - forms

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

Related

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

Outputting to Convertto-html Cmdlet Powershell

Evening everyone,
I've spent a while having a play on google learning how to create a basic form with powershell. Currently I'm having a problem coming to grips with how to get this basic form to output to a html file at the click of a button I have managed to get it to work with the services ie -
Get-Service | ConvertTo-Html | Out-File "Location"
However i'm wanting to input data on my form and then once the submit button is pressed, it puts the data into a table and then outputs it to the html file.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "BasicForm"
$Form.Width = 800
$Form.AutoScroll = $True
#VRN label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,75)
$objLabel.Text = "Num:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#VRN Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,75)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,75)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,125)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,125)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Date label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,125)
$objLabel.Text = "Date:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Date Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Add Button docs
$Buttondocs = New-Object System.Windows.Forms.Button
$Buttondocs.Location = New-Object System.Drawing.Size(175,35)
$Buttondocs.Size = New-Object System.Drawing.Size(120,23)
$Buttondocs.Text = "Documentation"
$Form.Controls.Add($Buttondocs)
#Add Button docs event
$Buttondocs.Add_Click({Button_Click})
Function Button_Click()
{
Invoke-Item "C:\Users\Michael\Documents\PowershellProjects\EIMI\*.txt"
}
#Add Button html
$Buttonhtml = New-Object System.Windows.Forms.Button
$Buttonhtml.Location = New-Object System.Drawing.Size(35,35)
$Buttonhtml.Size = New-Object System.Drawing.Size(120,23)
$Buttonhtml.Text = "Submit Form"
$Form.Controls.Add($Buttonhtml)
#Add Button html event
$Buttonhtml.Add_Click({Button_Click})
Function Button_Click()
{
ConvertTo-Html | Out-File "C:\Users\Michael\Documents\PowershellProjects\EIMI\index.html"
}
#Add Question 1
$Question1 = New-Object System.Windows.Forms.Label
$Question1.Location = New-Object System.Drawing.Size(50,175)
$Question1.Size = New-Object System.Drawing.Size(500,50)
$Question1.Text = "Random Form Question."
$Form.Controls.Add($Question1)
$Form.ShowDialog()
The documentation invoke-item section is another problem altogether but I beleive I have found a solution to that one. Unless someone has a simple method. Apologise if this is just trivial nonsense but I'm learning from playing with things. Any suggestions on good authors would be greatly appreciated, or even tips on how to improve this in general.
Thanks Mike.

Header Check Box in DataGridView

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"`

Get number of dates as selected in Windows Forms MonthCalendar for Powershell

I am using Powershell to create a GUI calendar and I need to be able assign a variable to the number of dates selected as the user selects them. I imagine I would have to use an event but am not sure how to procede. Basically, if a user selects a range of more than 10 days I need a checkbox on the same form to be checked. I am doing something similar with a listbox but I can't seem to figure out how to do the same with the calendar. Thanks.
Listbox code:
$Listbox2 = New-Object System.Windows.Forms.ListBox
$Listbox2.Location = New-Object System.Drawing.Size(240,80)
$Listbox2.Size = New-Object System.Drawing.Size(140,210)
$Listbox2.Height = 210
$Listbox2.SelectionMode = "MultiExtended"
$Listbox2Content | ForEach-Object {[void] $Listbox2.Items.Add($_)}
$Listbox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$Listbox2.Add_SelectedValueChanged({
If (($Listbox2.SelectedItems).Count -ge 10) {$Checkbox2.Checked = $True}
If (($Listbox2.SelectedItems).Count -lt 10) {$Checkbox2.Checked = $False}
})
Calendar Code:
$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.Location = New-Object System.Drawing.Size(12,80)
$Calendar.ShowTodayCircle = $False
$Calendar.ShowToday = $True
$Calendar.MaxDate = (Get-Date).AddDays(1)
$Calendar.MinDate = $OldestLog
$Calendar.MaxSelectionCount = "$CalendarDateRange"
$MenuBox.Controls.Add($Calendar)
The $data_selected scriptblock prints the number of selected dates. I attached it to the DataSelected event so it fires when you click on a date or make a range selection.
Add-Type -AssemblyName System.Windows.Forms
$date_selected = {
write-host (($_.End - $_.Start).Days + 1)
}
$form = New-Object Windows.Forms.Form
$form.text = "Calendar"
$form.FormBorderStyle = 'FixedDialog'
$form.Size = New-Object Drawing.Size(190,190)
$cal = New-Object System.Windows.Forms.MonthCalendar
$cal.add_DateSelected($date_selected)
$form.Controls.Add($cal)
$form.Add_Shown($form.Activate())
$form.showdialog() | Out-Null