How to overcome scope issue for generic button function? - winforms

I'm trying to use a generic button function in a script I'm writing, and I want to be able to pass a scriptblock or function to the Add_Click() method, but I think I'm having issues with scope. The code below wont work, but if I add a function to the Add_Click method it'll run that function when the button is clicked, but that defeats the purpose of a generic button that can be called multiple times with different actions.
Function New_Button {
param(
[string]$Button_Text,
[int]$Button_X,
[int]$Button_Y,
[int]$Button_W,
[int]$Button_H,
[Object]$Button_Holder,
[scriptblock]$Action
)
$Object_Button = New-Object System.Windows.Forms.Button
$Object_Button.Text = $Button_Text
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = $Button_X
$System_Drawing_Point.Y = $Button_Y
$Object_Button.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = $Button_W
$System_Drawing_Size.Height = $Button_H
$Object_Button.Size = $System_Drawing_Size
$Object_Button.Add_Click({$Action.Invoke()})
$Button_Holder.Controls.Add($Object_Button)
return $Object_Button
}
[scriptblock]$Scriptblock = { Write-Host "It worked..."}
$Button_MyFirstButton = New_Button "Submit" 150 225 75 30 $Form_MainApplication $Scriptblock

Related

powershell combobox items to variable

I am trying to create a powershell Script with a Gui that gives the user a drop list of available drives and then when the user selects the drive and clicks ok the script maps that drive as M:\
But I cant work out how to get the selected item in the combo box to be passed into the variable $MapDrive
#List of Drives To Map
$DriveList = #("0. DGL_Data","1. P1","2. DGLFSG3","3. p3 (TPC)","4. p4","6. p6","7. p7","8. p8","9. p9","10. p10","11. p11","12. p12")
#Displays With Drive to Map
$MapDrive = convert.ToString($Dropdown.SelectedItem)
Function MapDrive {
If ($MapDrive -eq $DriveList[0]){
Write-Output Sucess > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt"}
ElseIf ($MapDrive -eq $DriveList[1]){
Write-Output BooYah > "C:\Users\andy.burton\Desktop\Practice Selector\Yes.txt"
}
Else {
Write-Output Failure > "C:\Users\andy.burton\Desktop\Practice Selector\Failed.txt"}
}
#test Function
Function test {
Write-Output $MapDrive > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt"
}
#Function to Create Form
Function GenerateForm {
#Define Drive Selector Main Form
Add-Type -AssemblyName System.Windows.Forms
$DGL = New-Object system.Windows.Forms.Form
$DGL.Text = "DGL Practice Manager"
$DGL.TopMost = $true
$DGL.BackgroundImage = [system.drawing.image]::FromFile("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical.jpg")
$DGL.Icon = New-Object system.drawing.icon("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical2.ico")
$DGL.Width = 600
$DGL.Height = 265
$DGL.MinimizeBox = $False
$DGL.MaximizeBox = $False
#Label to Display Instuctions
$label2 = New-Object system.windows.Forms.Label
$label2.Text = "Select which drive"
$label2.BackColor = "#e4f3fa"
$label2.AutoSize = $true
$label2.Width = 25
$label2.Height = 10
$label2.location = new-object system.drawing.point(20,28)
$label2.Font = "Microsoft Sans Serif,10"
$DGL.controls.Add($label2)
#Dropdown Box For Selecting Practice
$Dropdown = New-Object system.windows.Forms.ComboBox
$Dropdown.BackColor = "#e4f3fa"
$DropDown.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
$Dropdown.Width = 243
$Dropdown.Height = 20
$Dropdown.location = new-object system.drawing.point(21,73)
$Dropdown.Font = "Microsoft Sans Serif,10"
$DropDown.items.addrange($DriveList)
$DGL.controls.Add($Dropdown)
#Cancel Button to Cancel drive Selection
$Cancelbutton = New-Object system.windows.Forms.Button
$Cancelbutton.Text = "Cancel"
$Cancelbutton.Width = 60
$Cancelbutton.Height = 30
$Cancelbutton.location = new-object system.drawing.point(210,120)
$Cancelbutton.Font = "Microsoft Sans Serif,10"
$DGL.CancelButton = $Cancelbutton
$CancelButton.Add_Click({ $DGL.close();[System.Windows.Forms.Application]::Exit($null)})
$DGL.controls.Add($Cancelbutton)
#OK Button to Select Drive
$OKbutton = New-Object system.windows.Forms.Button
$OKbutton.Text = "OK"
$OKbutton.Width = 60
$OKbutton.Height = 30
$OKbutton.location = new-object system.drawing.point(140,120)
$OKbutton.Font = "Microsoft Sans Serif,10"
$DGL.AcceptButton = $OKbutton
$MapDrive = convert.ToString($Dropdown.SelectedItem)
#On click call PracticeSelectedCallBack to launch the application
$OKbutton.Add_Click({test ; $DGL.close()})
$DGL.controls.Add($OKbutton)
#Display the Form
$DGL.Add_Shown({$DGL.Activate()})
$DGL.ShowDialog()
}
GenerateForm
I also want to hide the powershell window but not the gui I have tried -window hidden but that hid everything
The ComboBox has several events that you can tie into that will do various things. One of the events is a SelectedIndexChanged. You can add that event to your ComboBox object and update $MapDrive
This code $MapDrive = convert.ToString($Dropdown.SelectedItem) will only fire once during the initial compile. You have to use events to trigger code changes after compile during runtime.
Also, in Powershell you can use the following command [System.Windows.Forms.ComboBox] | gm to get a list of the methods, and properties of the object. You can use [System.Windows.Forms.checkedlistbox].GetEvents() to get a list of events of an object.

PowerShell show array item one at a time

I have an array and want to display each item one at a time. Each item should also be shown at different times intervals. I think I should use Start-Sleep -s 5.
Here is my code so far.
function GenerateForm {
$a=
"Downloading Files",
"Setting up VPN Connection",
"Getting computer information",
"Install Complete",
"Cluster Flux Technologies",
"You are Free to close this application"
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$richTextBox1 = New-Object System.Windows.Forms.RichTextBox
$pictureBox1 = New-Object System.Windows.Forms.PictureBox
$btn1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$btn1_OnClick= {
$richTextBox1.Text = $a | Out-String
}
#----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,212,208,200)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 658
$System_Drawing_Size.Width = 1072
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\Users\502706436\Desktop\tight vnc\cluster.ico')
$form1.Name = "form1"
$form1.Text = "Cluster Flux Technologies: VNC Installer"
$richTextBox1.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$richTextBox1.Enabled = $False
$richTextBox1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,0)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 566
$System_Drawing_Point.Y = 570
$richTextBox1.Location = $System_Drawing_Point
$richTextBox1.Name = "richTextBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 39
$System_Drawing_Size.Width = 348
$richTextBox1.Size = $System_Drawing_Size
$richTextBox1.TabIndex = 2
$richTextBox1.Text = ""
$form1.Controls.Add($richTextBox1)
$pictureBox1.BackgroundImage = [System.Drawing.Image]::FromFile('C:\Users\502706436\Desktop\tight vnc\cluster2 logo.png')
$pictureBox1.BackgroundImageLayout = 2
$pictureBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 41
$System_Drawing_Point.Y = 35
$pictureBox1.Location = $System_Drawing_Point
$pictureBox1.Name = "pictureBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 454
$System_Drawing_Size.Width = 949
$pictureBox1.Size = $System_Drawing_Size
$pictureBox1.TabIndex = 1
$pictureBox1.TabStop = $False
$form1.Controls.Add($pictureBox1)
$btn1.DataBindings.DefaultDataSourceUpdateMode = 0
$btn1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,0)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 71
$System_Drawing_Point.Y = 570
$btn1.Location = $System_Drawing_Point
$btn1.Name = "btn1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 39
$System_Drawing_Size.Width = 292
$btn1.Size = $System_Drawing_Size
$btn1.TabIndex = 0
$btn1.TabStop = $False
$btn1.Text = "Click to install connection"
$btn1.UseVisualStyleBackColor = $True
$btn1.add_Click($btn1_OnClick)
$form1.Controls.Add($btn1)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Show the Form
$form1.ShowDialog()
} #End Function
#Call the Function
GenerateForm
If anyone knows or has advice on how to achieve this I would appreciate it.
Thanks
For updating the text ,you can use :
$btn1_OnClick= {
$a | %{ $richTextBox1.Text = $_ | Out-String;Start-Sleep -s 5}
}
but the above simplistic approach will freeze ui while the above block is running.you can try the below runspace approach ,which will ensure that ui is responsive during the update:
$btn1_OnClick= {
$Runspace = [runspacefactory]::CreateRunspace()
$PowerShell = [powershell]::Create()
$PowerShell.runspace = $Runspace
$Runspace.Open()
[void]$PowerShell.AddScript({
param($richTextBox1)
$a=
"Downloading Files",
"Setting up VPN Connection",
"Getting computer information",
"Install Complete",
"Cluster Flux Technologies",
"You are Free to close this application"
$a | %{ $richTextBox1.Text = $_ | Out-String;Start-Sleep -s 5;}
}).addargument($richTextBox1)
$AsyncObject = $PowerShell.BeginInvoke()
}

Start-Job\Receive-Job is hanging\freezing while getting results - PrimalForm CE GUI

I can start the job but how can I continue using my program while its getting the information, if I do a 'while' loop it just freezes the program until the results have completed.
Here is the script generated by PrimalForms CE edition, and my code is in button1_OnClick.
End result I want to have the script running to check for locked out AD users (but that can really be anything) then while that is running I want to review the previous results and unlock a user if needed...
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 7/09/2015 2:24 PM
# Generated By: stojanp2
########################################################################
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button3 = New-Object System.Windows.Forms.Button
$button2 = New-Object System.Windows.Forms.Button
$listView1 = New-Object System.Windows.Forms.ListView
$button1 = New-Object System.Windows.Forms.Button
$Users = New-Object System.Windows.Forms.ColumnHeader
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button3_OnClick =
{
#TODO: Place custom script here
}
$button1_OnClick =
{
Start-Job -Name FindUsers -ScriptBlock { Search-ADAccount -LockedOut | Select-Object * }#end Start-Job
Do {
Start-Sleep -Seconds 1
write-host "waiting" # so i can see its doing something in the console
} Until (#(Get-Job -Name FindUsers).State -eq "Completed")
Write-Host "done"
$us = Receive-Job -Name FindUsers
ForEach ($u IN $us){
$listView1.Items.Add($u.sAMAccountName)
}
Get-Job -Name FindUsers | Remove-Job -Force
}
$button2_OnClick =
{
#TODO: Place custom script here
}
$handler_listView1_SelectedIndexChanged =
{
#TODO: Place custom script here
}
$OnLoadForm_StateCorrection =
{ #Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 469
$System_Drawing_Size.Width = 271
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Primal Form"
$button3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 142
$System_Drawing_Point.Y = 418
$button3.Location = $System_Drawing_Point
$button3.Name = "button3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 117
$button3.Size = $System_Drawing_Size
$button3.TabIndex = 3
$button3.Text = "button3"
$button3.UseVisualStyleBackColor = $True
$button3.add_Click($button3_OnClick)
$form1.Controls.Add($button3)
$button2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 418
$button2.Location = $System_Drawing_Point
$button2.Name
= "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 117
$button2.Size = $System_Drawing_Size
$button2.TabIndex = 2
$button2.Text = "button2"
$button2.UseVisualStyleBackColor = $True
$button2.add_Click($button2_OnClick)
$form1.Controls.Add($button2)
$listView1.Columns.Add($Users)|Out-Null
$listView1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 73
$listView1.Location = $System_Drawing_Point
$listView1.Name = "listView1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 339
$System_Drawing_Size.Width = 247
$listView1.Size = $System_Drawing_Size
$listView1.TabIndex = 1
$listView1.UseCompatibleStateImageBehavior = $False
$listView1.View = 1
$listView1.add_SelectedIndexChanged($handler_listView1_SelectedIndexChanged)
$form1.Controls.Add($listView1)
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 55
$System_Drawing_Size.Width = 247
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 0
$button1.Text = "button1"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_OnClick)
$form1.Controls.Add($button1)
$Users.Name = "Users"
$Users.Text = "User Names"
$Users.Width = 116
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog() | Out-Null
} #End Function
#Call the Function
GenerateForm
I can't speak about PrimalForm but in PowerShell Studio there is a seperate button called Button - Start Job in the section Toolbox > Control Sets:
When using this type of button an extra function is provided called Add-JobTracker that keeps track of the job progress and makes sure that the job is ran in the background in its own PowerShell session. So it doesn't freeze the interface and even updates a progress bar if you will.
More info on the SAPIEN page PowerShell Studio: Creating responsive forms.
The fact that your are doing a loop to check every time the status of the job is why the interface freezes. It will stop every time at that point and only frees up again when the job is completely done.
Maybe if you update PrimalForms to the latest version you'll have this function to.

How to merge cells in DataGridView using Powershell

Dear Powershell savants,
I recently discovered that using WinForms with PowerShell is a great way to create small yet really useful GUIs. I'm currently working with DataGridView and am trying to get fancy with visual properties - specifically, I was wondering if there was a way to merge cells so that I could make 'sub-headers' reflecting which parent node from my tree is associated with selected children. If that doesn't make much sense, what I'm trying to accomplish is depicted in the following:
Is there a way to merge and center cells or at least a way to remove grid lines on certain cells to achieve the depicted salmon colored row effect?
This person and this other person asked similar questions although a PowerShell solution isn't readily translated.
Base code without tree functionality follows (adapted from technet post):
#The following is adapted from https://gallery.technet.microsoft.com/ScriptCenter/3dcf0354-e7a7-482c-86f1-2e75809a502d/
function Get-ProcessInfo {
$array = New-Object System.Collections.ArrayList
$Script:procInfo = Get-Process | Select Id,Name,Path,Description | sort -Property Name
$array.AddRange($procInfo)
$dataGridView1.DataSource = $array
$form1.refresh()
}
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 2/24/2010 11:38 AM
# Generated By: Ravikanth Chaganti (http://www.ravichaganti.com/blog)
########################################################################
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$label1 = New-Object System.Windows.Forms.Label
$button3 = New-Object System.Windows.Forms.Button
$button2 = New-Object System.Windows.Forms.Button
$button1 = New-Object System.Windows.Forms.Button
$dataGridView1 = New-Object System.Windows.Forms.DataGridView
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button3_OnClick=
{
#$Form1.Close()
#$dataGridView1.SelectedRows | Foreach {$dataGridView1.Rows[$_.Index].DefaultCellStyle.BackColor = "Red"}
# $System_Windows_Forms_DataGridViewCellStyle_1.BackColor = 'White'
# $dataGridView1.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1
#Wipe Highlights without refreshing form
for($i=0; $i -le $dataGridView1.RowCount-1; $i++) {$dataGridView1.Rows[$i].DefaultCellStyle.BackColor = "White"}
}
$button1_OnClick=
{
#Get-ProcessInfo
}
$button2_OnClick=
{
$dataGridView1.SelectedRows | Foreach {$dataGridView1.Rows[$_.Index].DefaultCellStyle.BackColor = "Red"}
# $selectedRow = $dataGridView1.SelectedRows[0].Index
# if (($procid=$Script:procInfo[$selectedRow].Id)) {
#Stop-Process -Id $procid -Confirm
# $dataGridView1.Rows.Item($selectedRow).DefaultCellStyle.BackColor = "Red"
#}
}
$OnLoadForm_UpdateGrid=
{
Get-ProcessInfo
}
#----------------------------------------------
#region Generated Form Code
$form1.Text = "Primal Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 517
$System_Drawing_Size.Height = 414
$form1.ClientSize = $System_Drawing_Size
$label1.TabIndex = 4
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 155
$System_Drawing_Size.Height = 23
$label1.Size = $System_Drawing_Size
$label1.Text = "Process Manager"
$label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,2,3,0)
$label1.ForeColor = [System.Drawing.Color]::FromArgb(255,0,102,204)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 13
$label1.Location = $System_Drawing_Point
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Name = "label1"
$form1.Controls.Add($label1)
$button3.TabIndex = 3
$button3.Name = "button3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button3.Size = $System_Drawing_Size
$button3.UseVisualStyleBackColor = $True
$button3.Text = "Clear Highlights"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 429
$System_Drawing_Point.Y = 378
$button3.Location = $System_Drawing_Point
$button3.DataBindings.DefaultDataSourceUpdateMode = 0
$button3.add_Click($button3_OnClick)
$form1.Controls.Add($button3)
$button2.TabIndex = 2
$button2.Name = "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button2.Size = $System_Drawing_Size
$button2.UseVisualStyleBackColor = $True
$button2.Text = "Highlight"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 230
$System_Drawing_Point.Y = 378
$button2.Location = $System_Drawing_Point
$button2.DataBindings.DefaultDataSourceUpdateMode = 0
$button2.add_Click($button2_OnClick)
$form1.Controls.Add($button2)
$button1.TabIndex = 1
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "Refresh"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 379
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($button1_OnClick)
$form1.Controls.Add($button1)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 492
$System_Drawing_Size.Height = 308
$dataGridView1.Size = $System_Drawing_Size
$dataGridView1.DataBindings.DefaultDataSourceUpdateMode = 0
$dataGridView1.Name = "dataGridView1"
$dataGridView1.DataMember = ""
$dataGridView1.TabIndex = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 48
$dataGridView1.Location = $System_Drawing_Point
$form1.Controls.Add($dataGridView1)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Add Form event
$form1.add_Load($OnLoadForm_UpdateGrid)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm
Is this possible?
This describes how to implement "handlers for both events in order to provide a gradient selection background and some custom foreground content that spans multiple columns."
To answer this question I browsed through the doc here. Microsoft documentation doesn't always go into depth on how to use APIs, but when it does, it's worth reading, as is the case in this instance.

Transfering file chosen text in Browse Dialog box to the Text Input field in Powershell

Trying for the first time a custom dialog box in Powershell v3. I wanted to add a browse button to the form. I have it so it shows the browse button but I cant seem to figure out the part where we take the file name from the OpenFileDialog object and get it to appear in the objTextBox field. I've researched but cant seem to find any article that explains this part of the process.
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Name = 'Text1'
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
#File Browser Code.
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property #{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
}
$browse_button = New-Object system.Windows.Forms.Button
$browse_button.Text = "Choose...."
$browse_button.Location = New-Object System.Drawing.Size(10,75)
$browse_button.Size = New-Object System.Drawing.Size(100,27)
$browse_button.Add_Click({[void]$FileBrowser.ShowDialog()})
$objForm.Controls.Add($browse_button)
I think I would approach it slightly differently. I'd make a function (which I just happen to keep on hand) that displays the Browse Files dialog, and outputs a string. Then for the Add_Click set the textbox's value = the function. Something like:
Function Get-FilePath{
[CmdletBinding()]
Param(
[String]$Filter = "|*.*",
[String]$InitialDirectory = "C:\")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $InitialDirectory
$OpenFileDialog.filter = $Filter
[void]$OpenFileDialog.ShowDialog()
$OpenFileDialog.filename
}
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Name = 'Text1'
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$browse_button = New-Object system.Windows.Forms.Button
$browse_button.Text = "Choose...."
$browse_button.Location = New-Object System.Drawing.Size(10,75)
$browse_button.Size = New-Object System.Drawing.Size(100,27)
$browse_button.Add_Click({$objTextBox.Text = Get-FilePath -InitialDirectory "$env:UserProfile\Desktop"})
$objForm.Controls.Add($browse_button)
$browse_button.Text = "Choose...."
$browse_button.Location = New-Object System.Drawing.Size(10,75)
$browse_button.Size = New-Object System.Drawing.Size(100,27)
$browse_button.Add_Click({[void]$FileBrowser.ShowDialog()})
$objForm.Controls.Add($browse_button)

Resources