Следующее изображение GIF является примером диалога, написанного на языке программирования C #, который содержит четыре элемента управления!
Теперь я хочу использовать PowerShell для достижения той же функциональности, я сделал следующий дизайн (например, первый диалог на картинке GIF):
Первый элемент управления, имя ID - Ввод
Второй элемент управления, имя идентификатора MultipleSelect
Третий элемент управления, идентификатор которого InlineRadioOption
Четвертый элемент управления, имя ID - флажок
Когда я нажимаю кнопку OK, я хочу вывести значения всех элементов управления в объект формата JSON, например: $ CustomDialogResults
Позже я могу получить значение каждого элемента управления следующим образом (например, второе окно сообщения на изображении GIF):
$CustomDialogResults.Input
$CustomDialogResults.MultipleSelect
$CustomDialogResults.InlineRadioOption
$CustomDialogResults.Checkbox
Я хочу знать, как выразить объекты в формате JSON?
function Show-dialog_psf {
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$labelInlinRadios = New-Object 'System.Windows.Forms.Label'
$panel1 = New-Object 'System.Windows.Forms.Panel'
$radiobuttonRadioOption3 = New-Object 'System.Windows.Forms.RadioButton'
$radiobuttonRadioOption1 = New-Object 'System.Windows.Forms.RadioButton'
$radiobuttonRadioOption2 = New-Object 'System.Windows.Forms.RadioButton'
$Checkbox = New-Object 'System.Windows.Forms.CheckBox'
$MultipleSelect = New-Object 'System.Windows.Forms.CheckedListBox'
$labelMultipleSelect = New-Object 'System.Windows.Forms.Label'
$input = New-Object 'System.Windows.Forms.TextBox'
$labelInput = New-Object 'System.Windows.Forms.Label'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
# User Generated Script
#----------------------------------------------
$form1_Load = {
#TODO: Initialize Form Controls here
}
$buttonOK_Click = {
$CustomDialogResults = @{
#$CustomDialogResults.Input,
#$CustomDialogResults.MultipleSelect,
#$CustomDialogResults.InlineRadioOption,
#$CustomDialogResults.Checkbox,
}
[void][System.Windows.Forms.MessageBox]::Show($CustomDialogResults, 'Title') # Casting the method to [void] suppresses the output.
}
# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------
$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$buttonOK.remove_Click($buttonOK_Click)
$form1.remove_Load($form1_Load)
$form1.remove_Load($Form_StateCorrection_Load)
$form1.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
}
#endregion Generated Events
#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
$panel1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($labelInlinRadios)
$form1.Controls.Add($panel1)
$form1.Controls.Add($Checkbox)
$form1.Controls.Add($MultipleSelect)
$form1.Controls.Add($labelMultipleSelect)
$form1.Controls.Add($input)
$form1.Controls.Add($labelInput)
$form1.Controls.Add($buttonOK)
$form1.AcceptButton = $buttonOK
$form1.AutoScaleDimensions = '6, 13'
$form1.AutoScaleMode = 'Font'
$form1.ClientSize = '436, 288'
$form1.FormBorderStyle = 'FixedDialog'
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.Name = 'form1'
$form1.StartPosition = 'CenterScreen'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# labelInlinRadios
#
$labelInlinRadios.AutoSize = $True
$labelInlinRadios.Location = '21, 172'
$labelInlinRadios.Name = 'labelInlinRadios'
$labelInlinRadios.Size = '63, 17'
$labelInlinRadios.TabIndex = 10
$labelInlinRadios.Text = 'Inlin Radios'
$labelInlinRadios.UseCompatibleTextRendering = $True
#
# panel1
#
$panel1.Controls.Add($radiobuttonRadioOption3)
$panel1.Controls.Add($radiobuttonRadioOption1)
$panel1.Controls.Add($radiobuttonRadioOption2)
$panel1.AutoSize = $True
$panel1.Location = '95, 162'
$panel1.Name = 'panel1'
$panel1.Size = '329, 32'
$panel1.TabIndex = 9
#
# radiobuttonRadioOption3
#
$radiobuttonRadioOption3.Location = '217, 5'
$radiobuttonRadioOption3.Name = 'radiobuttonRadioOption3'
$radiobuttonRadioOption3.Size = '105, 24'
$radiobuttonRadioOption3.TabIndex = 7
$radiobuttonRadioOption3.TabStop = $True
$radiobuttonRadioOption3.Text = 'Radio option3'
$radiobuttonRadioOption3.UseCompatibleTextRendering = $True
$radiobuttonRadioOption3.UseVisualStyleBackColor = $True
#
# radiobuttonRadioOption1
#
$radiobuttonRadioOption1.Location = '7, 4'
$radiobuttonRadioOption1.Name = 'radiobuttonRadioOption1'
$radiobuttonRadioOption1.Size = '105, 24'
$radiobuttonRadioOption1.TabIndex = 5
$radiobuttonRadioOption1.TabStop = $True
$radiobuttonRadioOption1.Text = 'Radio option1'
$radiobuttonRadioOption1.UseCompatibleTextRendering = $True
$radiobuttonRadioOption1.UseVisualStyleBackColor = $True
#
# radiobuttonRadioOption2
#
$radiobuttonRadioOption2.Location = '112, 4'
$radiobuttonRadioOption2.Name = 'radiobuttonRadioOption2'
$radiobuttonRadioOption2.Size = '105, 24'
$radiobuttonRadioOption2.TabIndex = 6
$radiobuttonRadioOption2.TabStop = $True
$radiobuttonRadioOption2.Text = 'Radio option2'
$radiobuttonRadioOption2.UseCompatibleTextRendering = $True
$radiobuttonRadioOption2.UseVisualStyleBackColor = $True
#
# Checkbox
#
$Checkbox.Location = '102, 200'
$Checkbox.Name = 'Checkbox'
$Checkbox.Size = '104, 24'
$Checkbox.TabIndex = 8
$Checkbox.Text = 'checkbox1'
$Checkbox.UseCompatibleTextRendering = $True
$Checkbox.UseVisualStyleBackColor = $True
#
# MultipleSelect
#
$MultipleSelect.CheckOnClick = $True
$MultipleSelect.FormattingEnabled = $True
[void]$MultipleSelect.Items.Add('Multiple Select option1')
[void]$MultipleSelect.Items.Add('Multiple Select option2')
[void]$MultipleSelect.Items.Add('Multiple Select option3')
$MultipleSelect.Location = '121, 68'
$MultipleSelect.Name = 'MultipleSelect'
$MultipleSelect.Size = '277, 79'
$MultipleSelect.TabIndex = 4
$MultipleSelect.UseCompatibleTextRendering = $True
#
# labelMultipleSelect
#
$labelMultipleSelect.AutoSize = $True
$labelMultipleSelect.Location = '27, 68'
$labelMultipleSelect.Name = 'labelMultipleSelect'
$labelMultipleSelect.Size = '78, 17'
$labelMultipleSelect.TabIndex = 3
$labelMultipleSelect.Text = 'Multiple Select'
$labelMultipleSelect.UseCompatibleTextRendering = $True
#
# input
#
$input.Location = '121, 31'
$input.Name = 'input'
$input.Size = '277, 20'
$input.TabIndex = 2
$input.Text = 'input text'
#
# labelInput
#
$labelInput.AutoSize = $True
$labelInput.Location = '76, 34'
$labelInput.Name = 'labelInput'
$labelInput.Size = '29, 17'
$labelInput.TabIndex = 1
$labelInput.Text = 'Input'
$labelInput.UseCompatibleTextRendering = $True
#
# buttonOK
#
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '349, 253'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$buttonOK.UseCompatibleTextRendering = $True
$buttonOK.UseVisualStyleBackColor = $True
$buttonOK.add_Click($buttonOK_Click)
$panel1.ResumeLayout()
$form1.ResumeLayout()
#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($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()
} #End Function
#Call the form
Show-dialog_psf | Out-Null