-
Notifications
You must be signed in to change notification settings - Fork 0
/
GatherInputsForm.ps1
227 lines (196 loc) · 9.03 KB
/
GatherInputsForm.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
function GatherInputsForm ($title, $inputs, $autoSubmit = $false, $labelWidth = 150) {
# Wrap everything but final output to avoid unwanted output
$null = .{
# Put input objects in order
$inputs = $inputs.GetEnumerator() | sort-object { $_.value.index };
# Load Assembly for creating form & buttos
[void][System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.VisualBasic”)
[void][Windows.Forms.Application]::EnableVisualStyles()
# Define the form size & placement
$formOutput = @{};
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = $labelWidth + 250;
$form.Height = ($inputs.Count + 1) * 50;
$form.Text = $title;
$Form.MinimizeBox = $False;
$Form.MaximizeBox = $False;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$counter = 0;
$top = 15;
$textLabel = @{};
$textBox = @{};
$folderBrowserEvent = @{};
# build form content
foreach ($key in $inputs) {
# Add controls
if ($key.Value.InputType -eq "CheckBox") {
$textBox[$key.Name] = New-Object “System.Windows.Forms.CheckBox”;
if ($key.Value.DefaultValue -eq $true) {
$textBox[$key.Name].CheckState = "Checked";
}
}
elseif ($key.Value.InputType -eq "DirChooser") {
# Add textbox with tag
$textBox[$key.Name] = New-Object “System.Windows.Forms.TextBox”;
$textBox[$key.Name].Tag = $key.Name;
$textBox[$key.Name].Text = $key.Value.DefaultValue;
# open folder browser and set output to the textbox with value
$folderBrowserEvent = [System.EventHandler] {
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog;
[void]$FolderBrowser.ShowDialog();
$textBox[$this.Tag].Text = $FolderBrowser.SelectedPath;
};
# define browse button
$browseButton = New-Object “System.Windows.Forms.Button”;
$browseButton.Text = "...";
$browseButton.Top = $top;
$browseButton.Width = 25;
$browseButton.Height = 20;
$browseButton.Left = $labelWidth + 15 + 175;
$browseButton.Tag = $key.Name;
[void]$browseButton.Add_Click($folderBrowserEvent);
$form.Controls.Add($browseButton);
}
elseif ($key.Value.InputType -eq "TextArea") {
$textBox[$key.Name] = New-Object “System.Windows.Forms.TextBox”;
$textBox[$key.Name].Text = $key.Value.DefaultValue;
}
else {
$textBox[$key.Name] = New-Object “System.Windows.Forms.TextBox”;
$textBox[$key.Name].Text = $key.Value.DefaultValue;
}
$textBox[$key.Name].Left = $labelWidth + 15;
$textBox[$key.Name].Top = $top;
if ($key.Value.InputType -eq "DirChooser") {
$textBox[$key.Name].Width = 175;
}
else {
$textBox[$key.Name].Width = 200;
}
[void]$form.Controls.Add($textBox[$key.Name]);
#Add labels
$textLabel[$key.Name] = New-Object “System.Windows.Forms.Label”;
$textLabel[$key.Name].Left = 15;
$textLabel[$key.Name].Top = $top;
$textLabel[$key.Name].Width = $labelWidth;
$textLabel[$key.Name].Text = $key.Value.Label;
[void]$form.Controls.Add($textLabel[$key.Name]);
if ($key.Value.InputType -eq "TextArea") {
$top = $top + 70;
$textBox[$key.Name].MultiLine = $true;
$textBox[$key.Name].Height = 60;
}
else {
$top = $top + 30;
}
$counter++;
}
$textBox["formCanceled"] = New-Object "System.Windows.Forms.Textbox";
$textBox["formCanceled"].Text = "True";
$textBox["formCanceled"].Visible = $true;
# define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 15;
$button.Top = $top;
$button.Width = 100;
$button.Text = “Ok”;
# define Cancel button
$buttonCancel = New-Object “System.Windows.Forms.Button”;
$buttonCancel.Left = 130;
$buttonCancel.Top = $top;
$buttonCancel.Width = 100;
$buttonCancel.Text = “Cancel”;
$top = $top + 85;
$form.Height = $top;
# Ok Event
$eventHandler = [System.EventHandler]{
$passedValidation = $true;
foreach ($key in $inputs) {
# Cant run validation on a checkbox as value will always be true or false
if ($textBox[$key.Name].GetType().Name -eq "CheckBox") {
continue;
}
# Make background default color (white)
$textBox[$key.Name].BackColor = "White";
# if Required is set and text is empty highlight
if (($key.Value.Required -eq $true) -and ($textBox[$key.Name].Text -eq "")) {
$textBox[$key.Name].BackColor = "Yellow";
$passedValidation = $false;
}
# if Required if is set, check
if ($key.Value.RequiredIf -ne $null) {
# get value of "Other Property"
$currentValue = $textBox[$key.Value.RequiredIf.OtherProperty].Text;
# if other property is checkbox set value to true or false
if ($textBox[$key.Value.RequiredIf.OtherProperty].GetType().Name -eq "CheckBox") {
if ($textBox[$key.Value.RequiredIf.OtherProperty].CheckState -eq "Checked") {
$currentValue = $true;
}
else {
$currentValue = $false;
}
}
# Check required if conditions
if (($key.Value.RequiredIf.Condition -eq "EqualTo") -and ($currentValue -eq $key.Value.RequiredIf.Value) -and ($textBox[$key.Name].Text -eq "")) {
$textBox[$key.Name].BackColor = "Yellow";
$passedValidation = $false;
}
if (($key.Value.RequiredIf.Condition -eq "NotEqualTo") -and ($currentValue -ne $key.Value.RequiredIf.Value)-and ($textBox[$key.Name].Text -eq "")) {
$textBox[$key.Name].BackColor = "Yellow";
$passedValidation = $false;
}
}
}
# Passed validation
if ($passedValidation -eq $true) {
# Form was not canceled
$textBox["formCanceled"].Text = "False";
# fill out the output object
foreach ($key in $inputs) {
if ($key.Value.InputType -eq "CheckBox") {
if ($textBox[$key.Name].CheckState -eq "Checked") {
[void]$formOutput.Add($key.Name, $true);
}
else {
[void]$formOutput.Add($key.Name, $false);
}
}
else {
[void]$formOutput.Add($key.Name, $textBox[$key.Name].Text);
}
}
# Close the form
[void]$form.Close();
}
else {
[void][System.Windows.Forms.MessageBox]::Show("There are required fields that do not have a value. Required fields have been highlighted, please enter a value and try again.");
}
};
[void]$button.Add_Click($eventHandler);
# Cancel Event
$CancelEventHandler = [System.EventHandler]{
$textBox["formCanceled"].Text = "True";
[void]$form.Close();
};
[void]$buttonCancel.Add_Click($CancelEventHandler);
# Form Open Event
$formOpenEventHandler = [System.EventHandler] {
if ($autoSubmit -eq $true) {
$button.PerformClick();
}
};
# Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($buttonCancel);
$form.Controls.Add($cancelBox);
$form.Add_Shown($formOpenEventHandler);
[void]$form.ShowDialog();
}
if ($textBox["formCanceled"].Text -eq "True") {
Write-host "User canceled out of input dialog... Script aborting.";
Exit
}
#return values
$formOutput
}