想要将组合框中选定的值存储在变量 $x、$y 和 $z 中,但出现以下错误。
The property 'AcceptButton' cannot be found on this object. Verify that the property exists and can be set.
At line:71 char:5
+ $form1.AcceptButton = $OKButton
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
You cannot call a method on a null-valued expression.
At line:73 char:5
+ $form1.Controls.Add($OKButton)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:74 char:5
+ $Form1.Controls.Add($Label1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:75 char:5
+ $Form1.Controls.Add($Label2)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:76 char:5
+ $Form1.Controls.Add($Label3)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:77 char:5
+ $Form1.Controls.Add($Combobox1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:78 char:5
+ $Form1.Controls.Add($Combobox2)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:79 char:5
+ $Form1.Controls.Add($Combobox3)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:81 char:4
+ $result = $Form1.ShowDialog() |Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(600, 313)
$Form1.Text = 'Enter Your Details'
$Form1.FormBorderStyle = "FixedDialog"
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#~~< Label CLASSIFICATION 1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Point(295, 83)
$Label.Size = New-Object System.Drawing.Size(95, 23)
$Label.Text = "Location"
$Label.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Label.BackColor = "Transparent"
#~~< Label CLASSIFICATION 2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Point(295, 113)
$Label1.Size = New-Object System.Drawing.Size(95, 23)
$Label1.Text = "Facility"
$Label1.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Label1.BackColor = "Transparent"
#~~< Label CLASSIFICATION 3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Point(295, 143)
$Label2.Size = New-Object System.Drawing.Size(95, 23)
$Label2.Text = "Allocated by"
$Label2.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Label2.BackColor = "Transparent"
#~~< ComboBox 1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Classarray = @("Delhi","Punjab","UP","Assam","Mumbai","Haryana","MP")
$Combobox1 = New-Object System.Windows.Forms.ComboBox
$Combobox1.Location = New-Object System.Drawing.Point(400, 83)
$Combobox1.SelectedIndex = -1
$Combobox1.Size = New-Object System.Drawing.Size(170, 21)
$Combobox1.TabIndex = 5
ForEach ($Class in $Classarray) {
$Combobox1.Items.Add($Class)
} #end foreach
#~~< ComboBox 2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Classarray2 = @("D1","D2","D3","D4","D5","D6","D7")
$Combobox2 = New-Object System.Windows.Forms.ComboBox
$Combobox2.Location = New-Object System.Drawing.Point(400, 113)
$Combobox2.SelectedIndex = -1
$Combobox2.Size = New-Object System.Drawing.Size(170, 21)
$Combobox2.TabIndex = 6
foreach ($Class in $Classarray2) {
$Combobox2.Items.Add($Class)
} #end foreach
#~~< ComboBox 3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Classarray3 = @("Team1","Team2")
$Combobox3 = New-Object System.Windows.Forms.ComboBox
$Combobox3.Location = New-Object System.Drawing.Point(400, 143)
$Combobox3.SelectedIndex = -1
$Combobox3.Size = New-Object System.Drawing.Size(170, 21)
$Combobox3.TabIndex = 7
foreach ($Class in $Classarray3) {
$Combobox3.Items.Add($Class)
} #end foreach
$OKButton = New-Object System.Windows.Forms.Button
$okButton.Font = 'Calibri, 12.25pt'
$okButton.Location = '400, 250'
$okButton.Margin = '5, 5, 5, 5'
$okButton.Size = '130,40'
$okButton.BackColor ="LightGray"
$okButton.ForeColor ="black"
$okButton.Text = 'Submit'
$OKButton.DialogResult = 'OK'
$Form1.AcceptButton = $OKButton
$Form1.Controls.Add($OKButton)
$Form1.Controls.Add($Label1)
$Form1.Controls.Add($Label2)
$Form1.Controls.Add($Label3)
$Form1.Controls.Add($Combobox1)
$Form1.Controls.Add($Combobox2)
$Form1.Controls.Add($Combobox3)
$result = $Form1.ShowDialog() |Out-Null
if ($result -eq 'OK')
{
$x = $Combobox1.SelectedItem
$y = $Combobox2.SelectedItem
$z = $Combobox3.SelectedItem
Write-Host "Location" $x
Write-Host "Facility" $y
Write-Host "Asset" $z
}
Else
{
Exit
}
答案1
但由于您发布了代码和错误,这里对您的代码进行了快速重构,以显示您遇到的问题以及可能需要修复的地方。
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(600, 313)
$Form1.Text = 'Enter Your Details'
$Form1.FormBorderStyle = 'FixedDialog'
$Form1.StartPosition = 'CenterScreen'
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Point(295, 83)
$Label.Size = New-Object System.Drawing.Size(95, 23)
$Label.Text = 'Location'
$Label.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Label.BackColor = 'Transparent'
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Point(295, 113)
$Label1.Size = New-Object System.Drawing.Size(95, 23)
$Label1.Text = 'Facility'
$Label1.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Label1.BackColor = 'Transparent'
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Point(295, 143)
$Label2.Size = New-Object System.Drawing.Size(95, 23)
$Label2.Text = 'Allocated by'
$Label2.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Label2.BackColor = 'Transparent'
$Combobox1 = New-Object System.Windows.Forms.ComboBox
$Combobox1.Location = New-Object System.Drawing.Point(400, 83)
$Combobox1.SelectedIndex = -1
$Combobox1.Size = New-Object System.Drawing.Size(170, 21)
$Combobox1.TabIndex = 5
$Combobox1.Items.AddRange(@('Delhi','Punjab','UP','Assam','Mumbai','Haryana','MP'))
$Combobox2 = New-Object System.Windows.Forms.ComboBox
$Combobox2.Location = New-Object System.Drawing.Point(400, 113)
$Combobox2.SelectedIndex = -1
$Combobox2.Size = New-Object System.Drawing.Size(170, 21)
$Combobox2.TabIndex = 6
$Combobox2.Items.AddRange(@('D1','D2','D3','D4','D5','D6','D7'))
$Combobox3 = New-Object System.Windows.Forms.ComboBox
$Combobox3.Location = New-Object System.Drawing.Point(400, 143)
$Combobox3.SelectedIndex = -1
$Combobox3.Size = New-Object System.Drawing.Size(170, 21)
$Combobox3.TabIndex = 7
$Combobox3.Items.AddRange(@('Team1','Team2'))
$OKButton.Location = New-Object System.Drawing.Point(400,250)
$OKButton.Size = New-Object System.Drawing.Size(130,40)
$OKButton = New-Object System.Windows.Forms.Button
$okButton.Font = 'Calibri, 12.25pt'
$okButton.Margin = '5, 5, 5, 5'
$okButton.BackColor = 'LightGray'
$okButton.ForeColor = 'black'
$okButton.Text = 'Submit'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Form1.Controls.AddRange(
@(
$OKButton,
$Label,
$Label1,
$Label2,
$Combobox1,
$Combobox2,
$Combobox3
)
)
if ($Form1.ShowDialog() -eq 'OK')
{
"Location: $($Combobox1.SelectedItem)"
"Facility: $($Combobox2.SelectedItem)"
"Asset : $($Combobox3.SelectedItem)"
}
Else {Break}
# Results
<#
Location: Delhi
Facility: D1
Asset : Team1
#>