组合框选择变量
ComboBox selection to variable
我正在编写一个 GUI 脚本以将 mapp 网络共享作为驱动器。我已经(在其他 SO 用户的帮助下)创建了一个工作组合框,列出了未使用的驱动器盘符。我现在想做的是在单击按钮后将选定的组合框项目捕获到一个变量中,以便在另一个尚未实现的脚本部分中使用。到目前为止,我已经设法让脚本 'write-host' 选定的项目。请帮忙
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Test'
$form.Size = New-Object System.Drawing.Size(280,400)
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true
$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object System.Drawing.Size(50,20)
$button.Location = New-Object System.Drawing.Size(20,20)
$button.Text = "Literki"
$button.Add_Click($button_click)
$button2 = New-Object System.Windows.Forms.Button
$button2.Size = New-Object System.Drawing.Size(50,20)
$button2.Location = New-Object System.Drawing.Size(20,80)
$button2.Text = "Text"
$button2.Add_Click($button_click2)
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$comboBox1.Location = New-Object System.Drawing.Point(80, 55)
$comboBox1.Size = New-Object System.Drawing.Size(98, 10)
$comboBox1.Add_SelectedIndexChanged(
{ $A =$ComboBox1.Text
write-host $A}
)
$ComboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$AllLetters = 67..90 | ForEach-Object {[char]$_ + ":"}
$UsedLetters = Get-WmiObject Win32_LogicalDisk | Select -Expand DeviceID
$FreeLetters = $AllLetters | Where-Object {$UsedLetters -notcontains $_}
ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Letter) }
$button_click = {Write-Host $FreeLetters}
$button_click2 = {write-host $A}
$form.Controls.AddRange(@($button, $combobox1,$button2))
$form.ShowDialog()
您需要限定变量的范围,以便它在函数外部可用。
$comboBox1.Add_SelectedIndexChanged({
$Script:A =$ComboBox1.Text
})
$button_click2 = {Write-Host $Script:A}
有关详细信息,请参阅 https://www.varonis.com/blog/powershell-variable-scope/
我正在编写一个 GUI 脚本以将 mapp 网络共享作为驱动器。我已经(在其他 SO 用户的帮助下)创建了一个工作组合框,列出了未使用的驱动器盘符。我现在想做的是在单击按钮后将选定的组合框项目捕获到一个变量中,以便在另一个尚未实现的脚本部分中使用。到目前为止,我已经设法让脚本 'write-host' 选定的项目。请帮忙
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Test'
$form.Size = New-Object System.Drawing.Size(280,400)
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true
$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object System.Drawing.Size(50,20)
$button.Location = New-Object System.Drawing.Size(20,20)
$button.Text = "Literki"
$button.Add_Click($button_click)
$button2 = New-Object System.Windows.Forms.Button
$button2.Size = New-Object System.Drawing.Size(50,20)
$button2.Location = New-Object System.Drawing.Size(20,80)
$button2.Text = "Text"
$button2.Add_Click($button_click2)
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$comboBox1.Location = New-Object System.Drawing.Point(80, 55)
$comboBox1.Size = New-Object System.Drawing.Size(98, 10)
$comboBox1.Add_SelectedIndexChanged(
{ $A =$ComboBox1.Text
write-host $A}
)
$ComboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$AllLetters = 67..90 | ForEach-Object {[char]$_ + ":"}
$UsedLetters = Get-WmiObject Win32_LogicalDisk | Select -Expand DeviceID
$FreeLetters = $AllLetters | Where-Object {$UsedLetters -notcontains $_}
ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Letter) }
$button_click = {Write-Host $FreeLetters}
$button_click2 = {write-host $A}
$form.Controls.AddRange(@($button, $combobox1,$button2))
$form.ShowDialog()
您需要限定变量的范围,以便它在函数外部可用。
$comboBox1.Add_SelectedIndexChanged({
$Script:A =$ComboBox1.Text
})
$button_click2 = {Write-Host $Script:A}
有关详细信息,请参阅 https://www.varonis.com/blog/powershell-variable-scope/