-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPS-Forms.ps1
executable file
·672 lines (587 loc) · 27.1 KB
/
PS-Forms.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# Ref: https://github.com/Zerg00s/powershell-forms
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
# https://www.supinfo.com/articles/single/6362-multilingual-powershell-script
$Lang = Get-Culture | Select-Object -ExpandProperty Name
Function PrepareENString() {
$EN.OK = "OK"
$EN.CANCEL = "Cancel"
$EN.FONTFACENAME = "Times New Roman"
}
Function PrepareZHCNString() {
$ZHCN.OK = "确定"
$ZHCN.CANCEL = "取消"
$InstalledFontCollection = New-Object System.Drawing.Text.InstalledFontCollection
if ($InstalledFontCollection.Families | Where-Object {$_ -eq "微软雅黑"}) {
$ZHCN.FONTFACENAME = "微软雅黑"
} else {
$ZHCN.FONTFACENAME = "宋体"
}
}
$EN = @{}
$ZHCN = @{}
Switch ($Lang) {
"zh-CN" {PrepareZHCNString}
Default {PrepareENString}
}
Function GetLangText($id) {
# Get the $id string in language
Switch ($Lang) {
"zh-CN" {
$text = $ZHCN.$id
}
Default {
$text = $EN.$id
}
}
# Replace the placeholders with the expected value
$regexp = [regex]'<XXX>'
foreach ($arg in $args) {
$text = $regexp.Replace($text, [String]$arg,1)
}
return $text
}
$FONTFACENAME = GetLangText("FONTFACENAME") | Out-String
enum ControlType {
RadioButtons = 1
ComboBox = 2
}
<#
.DESCRIPTION
Capture user's input by presenting the choices as a group of radio buttons.
.EXAMPLE
PS C:\> Get-FormArrayItem -items $(get-childitem) -key name -dialogTitle "Select file" -ControlType RadioButtons
.EXAMPLE
PS C:\> Get-FormArrayItem -items $(get-childitem) -key name -dialogTitle "Select file" -ControlType ComboBox
.EXAMPLE
PS C:\> $subscription = Get-FormArrayItem -items $accounts -key "name" -dialogTitle "Choose subscription"
Display the list items. In this case it's an array of of Azure subscriptions.
.EXAMPLE
PS C:\> $location = Get-FormArrayItem -items $locations -key "displayName" -dialogTitle "Choose location" -defaultValue "Canada Central"
Display a form with the list of Azure Locations where "Canada Central" is selected by default.
.PARAMETER items
An array of objects
.PARAMETER key
Name of the property that will serve as a unique key for the radio buttons. This property will also be used to render text beside each radio button.
.PARAMETER dialogTitle
Descriptive Title of the dialog box.
.OUTPUTS
Returns an array item (proper object) that was selected via the form by clicking a radio button.
#>
function Get-FormArrayItem {
Param(
[Parameter(Mandatory = $true)] $items,
[Parameter(Mandatory = $false)] $key,
[Parameter(Mandatory = $false)] $dialogTitle,
[Parameter()] $defaultValue ,
[ControlType]
[Parameter(Mandatory = $false)] $ControlType = [ControlType]::RadioButtons
)
if ($items.Count -eq 0) {
Write-Host "'$dialogTitle' dialog did not contain any items. Returning `$null"
return $null
}
if ($null -eq $key) {
$key = "Name"
}
if ($null -eq $dialogTitle) {
$dialogTitle = "Choose one:"
}
#-----------------------------------------------------------------------
# Create a group that will contain your radio buttons
#-----------------------------------------------------------------------
# $TitleLabel = New-Object System.Windows.Forms.Label
# $TitleLabel.Location = New-Object System.Drawing.Point(40, 5)
# $TitleLabel.Text = $dialogTitle
# $TitleLabel.Font = New-Object System.Drawing.Font($FONTFACENAME, 13)
# $TitleLabel.Width = 550
$Panel = New-Object System.Windows.Forms.Panel
$Panel.Location = New-Object System.Drawing.Point(40, 30)
$Panel.Size = New-Object System.Drawing.Size(600, 1)
$Panel.text = $dialogTitle
$inputControlY = 10
$radioButtonHeight = 20
$spaceBetweenControls = $radioButtonHeight + 10
$Panel.Height = $spaceBetweenControls + 20
$MaximumPanelHeight = 290
$RadioFont = New-Object System.Drawing.Font($FONTFACENAME, 12)
$OKButton = New-Object System.Windows.Forms.Button
if ($ControlType -eq [ControlType]::RadioButtons) {
#-----------------------------------------------------------------------
# Create the collection of radio buttons
#-----------------------------------------------------------------------
foreach ($item in $items) {
$RadioButton = New-Object System.Windows.Forms.RadioButton
$RadioButton.Location = New-Object System.Drawing.Point(20, $inputControlY)
$RadioButton.Size = New-Object System.Drawing.Size(550, $radioButtonHeight)
$RadioButton.Font = $RadioFont
$props = Get-Member -InputObject $item -MemberType NoteProperty
$prop = $props | Where-Object { $_.name -eq $key }
$propValue = $item | Select-Object -ExpandProperty $prop.Name
$RadioButton.Text = $propValue
if ($RadioButton.Text -eq $defaultValue) {
$RadioButton.Checked = $true
} else {
$RadioButton.Checked = $false
}
$Panel.Controls.AddRange($RadioButton)
$inputControlY += $spaceBetweenControls
if ($Panel.Height -lt $MaximumPanelHeight) {
$Panel.Height += $spaceBetweenControls
} else {
$Panel.BorderStyle = 1
$Panel.VerticalScroll.Enabled = $true;
$Panel.VerticalScroll.Visible = $true;
$Panel.AutoScroll = $true;
}
}
}
if ($ControlType -eq [ControlType]::ComboBox) {
#-----------------------------------------------------------------------
# Add Combobox with autocomplete
#-----------------------------------------------------------------------
$ComboBox = New-Object system.Windows.Forms.ComboBox
$ComboBox.text = "comboBox"
$ComboBox.width = 580
$ComboBox.height = 50
$ComboBox.Location = New-Object System.Drawing.Point(10, 20)
$ComboBox.Font = New-Object System.Drawing.Font($FONTFACENAME, 12)
############## DropDown #################
$ComboBox.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDown;
foreach ($item in $items) {
$suppress = $ComboBox.Items.add($item)
}
$ComboBox.SelectedIndex = 1
############## AutoComplite #################
$ComboBox.AutoCompleteSource ='CustomSource'
$ComboBox.AutoCompleteMode ='SuggestAppend'
foreach ($item in $items) {$ComboBox.AutoCompleteCustomSource.AddRange($item)}
$choice = $ComboBox.Text
######## If Combobox text is different from list or null ###############
$ComboBox.add_TextChanged({Combobox})
$Panel.Controls.Add($ComboBox)
$Panel.Height += $spaceBetweenControls
function Combobox {
if ($null -eq $ComboBox.SelectedItem) {
$OKButton.Enabled = $false
} else {
$OKButton.Enabled = $true
}
}
}
#-----------------------------------------------------------------------
# Add an OK button
#-----------------------------------------------------------------------
$ActionButtonHeight = 40
$ActionButtonY = $Panel.size.Height + $Panel.Location.Y + 10
# $OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(130, $ActionButtonY)
$OKButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$OKButton.Text = GetLangText("OK")
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
#-----------------------------------------------------------------------
#Add a cancel button
#-----------------------------------------------------------------------
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(255, $ActionButtonY)
$CancelButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$CancelButton.Text = GetLangText("CANCEL")
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form = New-Object System.Windows.Forms.Form
$Form.width = $Panel.size.Width + 80
$Form.height = $OKButton.Location.Y + $OKButton.Height + 60
$Form.Text = $dialogTitle
$Form.Topmost = $true
$Form.StartPosition = "CenterScreen"
#-----------------------------------------------------------------------
# Set the font of the text to be used within the form
#-----------------------------------------------------------------------
$Form.Font = New-Object System.Drawing.Font($FONTFACENAME, 10)
$form.Controls.AddRange(@($TitleLabel, $Panel, $OKButton, $CancelButton))
#-----------------------------------------------------------------------
# Assign the Accept and Cancel options in the form to the corresponding buttons
#-----------------------------------------------------------------------
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
#-----------------------------------------------------------------------
# Activate the form
#-----------------------------------------------------------------------
$form.Add_Shown( { $form.Activate() })
$dialogResult = $form.ShowDialog()
#-----------------------------------------------------------------------
# Get the results of the dialog
#-----------------------------------------------------------------------
if ($dialogResult -eq "OK") {
if ($ControlType -eq [ControlType]::RadioButtons) {
$checkedControl = $Panel.Controls | Where-Object {
([System.Windows.Forms.RadioButton]$_).Checked -eq $true
}
$selectedValue = $checkedControl.Text
}
if ($ControlType -eq [ControlType]::ComboBox) {
$selectedValue = $ComboBox.Text
}
return $items | Where-Object {
$propValue = $_ | Select-Object -ExpandProperty $prop.Name
$propValue.ToString() -eq $selectedValue
}
} else {
# throw "Execution was cancelled by the user"
return $null
}
}
<#
.DESCRIPTION
Capture user's input by presenting the choices as a group of radio buttons.
.EXAMPLE
PS C:\> Get-FormArrayItems -items $(get-childitem) -key name -dialogTitle "Select files"
.PARAMETER items
An array of objects
.PARAMETER key
Name of the property that will serve as a unique key for the radio buttons. This property will also be used to render text beside each radio button.
.PARAMETER dialogTitle
Descriptive Title of the dialog box.
.OUTPUTS
Returns an array item (proper object) that was selected via the form by clicking a radio button.
#>
function Get-FormArrayItems {
Param(
[Parameter(Mandatory = $true)] $items,
[Parameter(Mandatory = $false)] $key,
[Parameter(Mandatory = $false)] $dialogTitle
)
if ($items.Count -eq 0) {
Write-Host "'$dialogTitle' dialog did not contain any items. Returning `$null"
return $null
}
if ($null -eq $key) {
$key = "Name"
}
if ($null -eq $dialogTitle) {
$dialogTitle = "Choose one:"
}
#-----------------------------------------------------------------------
# Create a group that will contain your radio buttons
#-----------------------------------------------------------------------
# $TitleLabel = New-Object System.Windows.Forms.Label
# $TitleLabel.Location = New-Object System.Drawing.Point(40, 5)
# $TitleLabel.Text = $dialogTitle
# $TitleLabel.Font = New-Object System.Drawing.Font($FONTFACENAME, 13)
# $TitleLabel.Width = 550
$Panel = New-Object System.Windows.Forms.Panel
$Panel.Location = New-Object System.Drawing.Point(40, 30)
$Panel.Size = New-Object System.Drawing.Size(600, 1)
$Panel.text = $dialogTitle
$inputControlY = 10
$radioButtonHeight = 20
$spaceBetweenControls = $radioButtonHeight + 10
$Panel.Height = $spaceBetweenControls + 20
$MaximumPanelHeight = 290
$RadioFont = New-Object System.Drawing.Font($FONTFACENAME, 12)
$OKButton = New-Object System.Windows.Forms.Button
#-----------------------------------------------------------------------
# Create the collection of radio buttons
#-----------------------------------------------------------------------
foreach ($item in $items) {
$CheckBox = New-Object System.Windows.Forms.CheckBox
$CheckBox.Location = New-Object System.Drawing.Point(20, $inputControlY)
$CheckBox.Size = New-Object System.Drawing.Size(350, $radioButtonHeight)
$CheckBox.Font = $RadioFont
$props = Get-Member -InputObject $item -MemberType Property
$prop = $props | Where-Object { $_.name -eq $key }
$propValue = $item | Select-Object -ExpandProperty $prop.Name
$CheckBox.Text = $propValue
if ($CheckBox.Text -eq $defaultValue) {
$CheckBox.Checked = $true
} else {
$CheckBox.Checked = $false
}
$Panel.Controls.AddRange($CheckBox)
$inputControlY += $spaceBetweenControls
if ($Panel.Height -lt $MaximumPanelHeight) {
$Panel.Height += $spaceBetweenControls
} else {
$Panel.BorderStyle = 1
$Panel.VerticalScroll.Enabled = $true;
$Panel.VerticalScroll.Visible = $true;
$Panel.AutoScroll = $true;
}
}
#-----------------------------------------------------------------------
# Add an OK button
#-----------------------------------------------------------------------
$ActionButtonHeight = 40
$ActionButtonY = $Panel.size.Height + $Panel.Location.Y + 10
# $OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(130, $ActionButtonY)
$OKButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$OKButton.Text = GetLangText("OK")
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
#-----------------------------------------------------------------------
#Add a cancel button
#-----------------------------------------------------------------------
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(255, $ActionButtonY)
$CancelButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$CancelButton.Text = GetLangText("CANCEL")
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form = New-Object System.Windows.Forms.Form
$Form.width = $Panel.size.Width + 80
$Form.height = $OKButton.Location.Y + $OKButton.Height + 60
$Form.Text = $dialogTitle
$Form.Topmost = $true
$Form.StartPosition = "CenterScreen"
#-----------------------------------------------------------------------
# Set the font of the text to be used within the form
#-----------------------------------------------------------------------
$Form.Font = New-Object System.Drawing.Font($FONTFACENAME, 10)
$form.Controls.AddRange(@($TitleLabel, $Panel, $OKButton, $CancelButton))
#-----------------------------------------------------------------------
# Assign the Accept and Cancel options in the form to the corresponding buttons
#-----------------------------------------------------------------------
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
#-----------------------------------------------------------------------
# Activate the form
#-----------------------------------------------------------------------
$form.Add_Shown( { $form.Activate() })
$dialogResult = $form.ShowDialog()
#-----------------------------------------------------------------------
# Get the results of the dialog
#-----------------------------------------------------------------------
if ($dialogResult -eq "OK") {
$checkedControls = $Panel.Controls | Where-Object {
([System.Windows.Forms.CheckBox]$_).Checked -eq $true
}
$checkedKeys = $checkedControls | ForEach-Object {$_.Text }
return $items | Where-Object {
$propValue = $_ | Select-Object -ExpandProperty $prop.Name
if (-Not ($null -eq $checkedKeys)) {
$checkedKeys.contains($propValue.ToString())
}
}
} else {
# throw "Execution was cancelled by the user"
return $null
}
}
<#
.DESCRIPTION
Simple Dialog box that returns $true if user clicks "Yes" and $false if "No" is clicked.
Useful if you want to make your script interactive and user-friendly.
.EXAMPLE
PS C:\> $createNewResourceGroup = Get-FormBinaryAnswer "Do you want to create a new Resource Group?"
Displays a dialog box with Yes and No buttons.
.INPUTS
dialogTitle
.OUTPUTS
$true or $false
#>
function Get-FormBinaryAnswer {
Param(
[Parameter(Position = 0)] $dialogTitle
)
$options = @([pscustomobject]@{Value = $true; Name = "Yes" }, [pscustomobject]@{Value = $false; Name = "No" })
$answer = Get-FormArrayItem -items $options -key "Name" -dialogTitle $dialogTitle
return $answer.Value
}
<#
.DESCRIPTION
Long description
.EXAMPLE
PS C:\> $UserName = Get-FormStringInput "Enter your name"
Shows a dialog with an input box with OK and Cancel buttons.
.EXAMPLE
PS C:\> $resourceGroupName = Get-FormStringInput "Enter new resource group name" -defaultValue "Comments-Notification"
Shows a dialog with an input where "Comments-Notification" is pre-populated.
.OUTPUTS
returns a string that user typed in an input box.
#>
function Get-FormStringInput {
Param(
[Parameter(Mandatory = $true, Position = 0)] $dialogTitle,
[Parameter(Position = 1)] $defaultValue
)
$label = New-Object System.Windows.Forms.Label
$label.Size = New-Object System.Drawing.Size(600, 50)
$label.Location = New-Object System.Drawing.Point(10, 10)
$label.Text = $dialogTitle
$label.Font = New-Object System.Drawing.Font($FONTFACENAME, 12)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Size = New-Object System.Drawing.Size(600, 50)
$textBox.Location = New-Object System.Drawing.Point(10, 60)
$textBox.Font = New-Object System.Drawing.Font($FONTFACENAME, 15)
$textBox.Text = $defaultValue
#-----------------------------------------------------------------------
# Add an OK button
#-----------------------------------------------------------------------
$ActionButtonHeight = 40
$ActionButtonY = 140
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(130, $ActionButtonY)
$OKButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$OKButton.Text = GetLangText("OK")
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
#-----------------------------------------------------------------------
# Add an cancel button
#----------------------------------------------------------------------
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(255, $ActionButtonY)
$CancelButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$CancelButton.Text = GetLangText("CANCEL")
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form = New-Object System.Windows.Forms.Form
$Form.width = $textBox.size.Width + 50
$Form.height = $OKButton.Location.Y + $OKButton.Height + 80
$Form.Text = $dialogTitle
$Form.Topmost = $true
$Form.StartPosition = "CenterScreen"
#-----------------------------------------------------------------------
# Set the font of the text to be used within the form
#-----------------------------------------------------------------------
$Form.Font = New-Object System.Drawing.Font($FONTFACENAME, 10)
#-----------------------------------------------------------------------
# Add all the Form controls on one line
#-----------------------------------------------------------------------
$form.Controls.AddRange(@($label, $textBox, $OKButton, $CancelButton))
# Assign the Accept and Cancel options in the form to the corresponding buttons
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
#-----------------------------------------------------------------------
# Activate the form
#-----------------------------------------------------------------------
$form.Add_Shown( { $form.Activate() })
#-----------------------------------------------------------------------
# Get the results from the button click
#-----------------------------------------------------------------------
$dialogResult = $form.ShowDialog()
#-----------------------------------------------------------------------
# If the OK button is selected
#-----------------------------------------------------------------------
if ($dialogResult -eq "OK") {
return $textBox.Text
} else {
# throw "Execution was cencelled by the user"
return $null
}
}
<#
.DESCRIPTION
Displays the dynamic form that renders one text box for every object's property.
When user populates the form and clicks OK - returned object contains property values from the form's inputs
.EXAMPLE
PS C:\> $preDeployInputs = @{
suffix = ""
TimeZone = "Central Standard Time"
sendGridRegistrationEmail = "ENTER_VALID_EMAIL_ADDRESS"
fromEmail = "[email protected]"
}
PS C:\> $preDeployInputs = Get-FormItemProperties -item $preDeployInputs -dialogTitle "Fill these required fields"
Displays the form that has 4 text inputs. When user populates them and clicks OK - returned object contains property values from the form's inputs
.INPUTS
PSCustomObject or a Hashtable
.OUTPUTS
Returns the same object that was passed in, but with properties populated via the Form that was displayed
#>
function Get-FormItemProperties {
Param(
[Parameter(Mandatory = $true)] $item,
[Parameter(Mandatory = $false)] $dialogTitle
)
#-----------------------------------------------------------------------
# Convert the object to hashtable
#-----------------------------------------------------------------------
if ($item.GetType().Name -eq "PSCustomObject") {
$hashTable = @{ }
$item.psobject.properties | ForEach-Object { $hashTable[$_.Name] = $_.Value }
$item = $hashTable
}
if($null -eq $dialogTitle){
$dialogTitle = "Fill out values:"
}
#-----------------------------------------------------------------------
# Create a group that will contain textboxes
#-----------------------------------------------------------------------
$GroupBox = New-Object System.Windows.Forms.GroupBox
$GroupBox.Location = New-Object System.Drawing.Point(40, 30)
$GroupBox.Size = New-Object System.Drawing.Size(600, 1)
$GroupBox.text = $dialogTitle
$textboxY = 40
$textboxHeight = 20
$spaceBetweenTextboxes = $textboxHeight + 22
$GroupBox.Height += $spaceBetweenTextboxes + 20
$textboxes = @()
foreach ($key in $item.Keys) {
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Point(20, $textboxY)
$Label.Size = New-Object System.Drawing.Size(450, $textboxHeight)
$Label.Text = $key
$textboxY += $spaceBetweenTextboxes / 2
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Point(20, $textboxY)
$TextBox.Size = New-Object System.Drawing.Size(550, $textboxHeight)
$TextBox.Text = $item[$key]
$GroupBox.Controls.AddRange(@($Label, $TextBox))
$textboxes += $TextBox
$textboxY += $spaceBetweenTextboxes
$TextBox.Font = New-Object System.Drawing.Font($FONTFACENAME, 12)
$GroupBox.Height += $spaceBetweenTextboxes * 1.5
}
#-----------------------------------------------------------------------
# Add an OK button
#-----------------------------------------------------------------------
$ActionButtonHeight = 40
$ActionButtonY = $GroupBox.size.Height + $GroupBox.Location.Y + 10
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(130, $ActionButtonY)
$OKButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$OKButton.Text = GetLangText("OK")
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
#-----------------------------------------------------------------------
# Add an Cancel button
#-----------------------------------------------------------------------
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(255, $ActionButtonY)
$CancelButton.Size = New-Object System.Drawing.Size(100, $ActionButtonHeight)
$CancelButton.Text = GetLangText("CANCEL")
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form = New-Object System.Windows.Forms.Form
$Form.width = $GroupBox.size.Width + 100
$Form.height = $OKButton.Location.Y + $OKButton.Height + 60
$Form.Text = $dialogTitle
$Form.Topmost = $true
$Form.StartPosition = "CenterScreen"
#-----------------------------------------------------------------------
# Set the font of the text to be used within the form
#-----------------------------------------------------------------------
$Form.Font = New-Object System.Drawing.Font($FONTFACENAME, 10)
$form.Controls.AddRange(@($GroupBox, $OKButton, $CancelButton))
#-----------------------------------------------------------------------
# Assign the Accept and Cancel options in the form to the corresponding buttons
#-----------------------------------------------------------------------
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
#-----------------------------------------------------------------------
# Activate the form
#-----------------------------------------------------------------------
$form.Add_Shown( { $form.Activate() })
#-----------------------------------------------------------------------
# Get the results from the button click
#-----------------------------------------------------------------------
$dialogResult = $form.ShowDialog()
#-----------------------------------------------------------------------
# If the OK button is selected
#-----------------------------------------------------------------------
if ($dialogResult -eq "OK") {
$hashTable = @{ }
$i = 0;
foreach ($key in $item.Keys) {
$hashTable[$key] = $textboxes[$i].Text
$i++;
}
return $hashTable
} else {
# throw "Execution was cancelled by the user"
return $null
}
}