How to use maximize groupbox using GUI in PowerShell? - powershell

I would like to create a group box in my GUI form. I use windowsState maximized for my form. And I want to use the group box, and I need to make the group box also maximized but combine with margin and padding. So the size and position of the group box will not change even the resolution screen change.
I tried this, but it does not work.
Anyone can help me. Thank you.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.text = "Form"
$Form.TopMost = $false
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $false
$Form.startposition = "centerscreen"
$Form.WindowState = 'Maximized'
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.text = "Group Box"
$Groupbox1.location = New-Object System.Drawing.Point(8,13)
$Groupbox1.Padding = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
$Groupbox1.Margin = 2,2,2,2
$Form.controls.AddRange(#($Groupbox1))
[void]$Form.ShowDialog()

You would have to create a panel first of 100% width and height. Then put the groupbox inside the panel, this should work:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.text = "Form"
$Form.TopMost = $false
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $false
$Form.startposition = "centerscreen"
$Form.WindowState = 'Maximized'
$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 = "Group Box"
$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"
$form.controls.add($Panel)
$panel.controls.AddRange(#($Groupbox1))
[void]$Form.ShowDialog()

Related

Change Color of the TitleBar in Windows.Forms (Powershell)

How can Change the Color of the Titelbar ?
Picture GUI
My Code is in Powershell and not C#
[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object Drawing.Point 710,509
$form.text = "Windows_Optimisation_Pack...."
$form.StartPosition = "CenterScreen"
$form.ForeColor='#aaaaaa'
$form.BackColor='#212121'
$form.MinimizeBox = $false
$form.MaximizeBox = $false
I already tried different commands, but none worked.
I can only find Solutions for C#.

Powershell linking datagridview to an array

I am currently trying to load a json file that is a DB into a datagridview within a form.
I currnetly have the JSON file loading into an array. What I cant figure out is how to get the array to load into the datagridview so I can display the data of what is in said array from within the form.
I dont think im too far off any help would be much appreciated.
#JSON DB Front end
$jsonDB = Get-Content 'C:\Support\HardwareCollection.json' | Out-String | ConvertFrom-Json
#jsonDb has $jsonDB.Date and $jsonDB.username
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(1050,425)
$form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "Hardware Collection"
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(500,200)
$dataGridView.Location = new-object system.drawing.size(1,150)
$dataGridView.ColumnCount = 2
$dataGridView.Columns[0].Name = "Date"
$dataGridView.Columns[1].Name = "Username"
$form.Controls.Add($dataGridView)
$datagridview.DataSource = $jsonDB
$Form.ShowDialog()
Many thanks!!!
You're right - you were close - the dataGridView likes .NET types for its data sources, not Powershell objects. So, just create an ArrayList from your Powershell collection, and it works:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Data
Add-Type -AssemblyName System.Collections
$jsonDB = Get-Content 'C:\Support\HardwareCollection.json' | Out-String | ConvertFrom-Json
$tableData = New-Object System.Collections.ArrayList
$tableData.AddRange($jsonDB)
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(1050, 425)
$Form.MaximizeBox = $False
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "Hardware Collection"
$dataGridView = New-Object System.Windows.Forms.DataGridView -Property #{
Size = New-Object System.Drawing.Size(500, 200)
Location = New-Object System.Drawing.Size(1, 150)
ColumnHeadersVisible = $True
DataSource = $tableData
}
$Form.Controls.Add($dataGridView)
$Form.ShowDialog()

Powershell GUI tabbed layout script placement

I wrote six small gui scripts that I'd like to place in a single file, for the sake of converting said file into rxecutable with ps2exe.
I've found a script ,here on stack that is perfect for what I want. Unfortunatelly I cann't find any info on script placement within tabs and MS documentation leads me to ISE tabs, which is not helpfull.
Say I'd like to place this
Add-Type -AssemblyName PresentationFramework
$button_click = {
$folder = $textBox1.Text;
$pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?', '', '4');
If($pytanie -eq 'Yes')
{Remove-Item –path $folder –recurse -Force};
$test = Test-Path $folder;
if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety', '', '0')}}
$label2 = New-Object System.Windows.Forms.Label
$label2.AutoSize = $True
$label2.Text = ("Scieżka")
$label2.Location = New-Object System.Drawing.Point (10,30)
$label2.Size = New-Object System.Drawing.Size (25,70)
$label2.Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Bold)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mapowanie' ### Text to be displayed in the title
$form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true ### Optional - Opens on top of other windows
$form.Controls.AddRange(#($textBox1,$button, $label2))
$form.ShowDialog()
within a tab. How to do it?
I think that it the
$Tab1.Controls.Add($button)
You are looking for.
For example
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false
$ApplicationForm.Size = "800,600"
$FormTabControl = New-object System.Windows.Forms.TabControl
$FormTabControl.Size = "755,475"
$FormTabControl.Location = "25,75"
$ApplicationForm.Controls.Add($FormTabControl)
$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab1.UseVisualStyleBackColor = $True
$Tab1.Name = "Tab1"
$Tab1.Text = "Tab1”
$FormTabControl.Controls.Add($Tab1)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)
$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab2.UseVisualStyleBackColor = $True
$Tab2.Name = "Tab2"
$Tab2.Text = "Tab2”
$FormTabControl.Controls.Add($Tab2)
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)
# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()

How to make GUI size fixed size in every computer using powershell?

I have an GUI, I want to make the size of the GUI will be the same no matter size of the computer. I tried this, but when I execute in the bigger display, the size of the GUI is bigger, if I execute in small display, the GUI become small size.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1085,650'
$Form.text = "FORM"
$Form.BackColor = "#f6f6f6"
$Form.AutoSize = $true
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $false
$Form.startposition = "centerscreen"
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height = 592
$Groupbox1.width = 1047
$Groupbox1.text = "INFO"
$Groupbox1.Font = 'Microsoft Sans Serif,9'
$Groupbox1.location = New-Object System.Drawing.Point(18,14)
$Groupbox1.AutoSize = $true
$Groupbox1.ForeColor = "#032d5d"
$Button1 = New-Object system.Windows.Forms.Button
$Button1.BackColor = "#136aa4"
$Button1.text = "OK"
$Button1.width = 70
$Button1.height = 27
$Button1.location = New-Object System.Drawing.Point(960,57)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button1.AutoSize = $true
$Button1.ForeColor = "#ffffff"
Try specifying autosize and autoscaling off maybe?
$form.AutoScale = $false
$form.AutoSize = $false
You have to be careful with this though, you run the risk of the form being unreadable on very high resolution displays.

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.