ComboBox.SelectedItem 给出 Null 值
ComboBox.SelectedItem giving Null value
卡在这卡了一整天。我正在使用 PowerShell 表单创建 UI。其中:
- 用户 select 来自第一个组合框的选项。点击按钮 Go
- 基于 selection,将出现一个包含另一个组合框的面板。
- 如果用户 select 在第一个组合框中有另一个选项,那么会出现另一个带有另一个组合框的面板
- 在 select 从面板组合框中选择选项后,用户单击开始按钮。
- 这导致一个函数将 selected 选项存储到一个变量。
问题
- 现在,当用户 select 从面板的组合框中选择选项时,我正在使用 $combobox.selecteditem.Tostring 来获取值。
但它给了我 NULL 结果。
这是我的代码..
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.Add("View")
$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
######### code Starts ###########
function Button1_Click()
{
$link = $comboBox2.SelectedItem.ToString();
$linktype = $comboBox3.SelectedItem.ToString();
}
####### end of code ######
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
问题是您的变量是在一个函数中设置的,因此它们的范围仅限于该函数。真的没有理由把它放在函数中,而是直接放在 .add_click()
中。或者,如果您觉得需要将它保留在函数中,您可以像使用 $global:ButtonClicked
一样设置变量的范围,并将它们设置为 $global:Link
和 $global:LinkType
.
编辑: Order 在 PowerShell 中确实有所不同,所以我确实在你的脚本中移动了一些东西以使其正常工作,但我能够获得值来反映当我把它们放在全局范围内时很好。
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
######### code Starts ###########
function Button1_Click()
{
write-host "Link = $global:link"
write-host "Link Type = $global:linktype"
}
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
####### end of code ######
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(@("View","Edit") )
#$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
当我 运行 设置两个组合框值并单击“开始”时,我在 ISE 中收到文本说明我将 Link 和 LinkType 变量设置为什么,以及能够回显 $global:link
和 $global:linktype
以查看正确分配的值。
卡在这卡了一整天。我正在使用 PowerShell 表单创建 UI。其中:
- 用户 select 来自第一个组合框的选项。点击按钮 Go
- 基于 selection,将出现一个包含另一个组合框的面板。
- 如果用户 select 在第一个组合框中有另一个选项,那么会出现另一个带有另一个组合框的面板
- 在 select 从面板组合框中选择选项后,用户单击开始按钮。
- 这导致一个函数将 selected 选项存储到一个变量。
问题
- 现在,当用户 select 从面板的组合框中选择选项时,我正在使用 $combobox.selecteditem.Tostring 来获取值。
但它给了我 NULL 结果。
这是我的代码..
$global:Button1Clicked = 0; Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing [System.Windows.Forms.Application]::EnableVisualStyles() [System.Windows.Forms.FormClosingEventHandler] $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = '1500,800' $Form.text = "NextUI " $Form.BackColor = 'White' $Form.TopMost = $true $Form.add_closing( { if($global:Button2Clicked) { $global:Button2Clicked = 0; $_.cancel = $true; } }) $panel1 = New-Object system.Windows.Forms.Panel $panel1.AutoSize = $true $panel1.Width = 1200 $panel1.Height = 200 $panel1.location = New-Object System.Drawing.Point(50,100) $panel1.Visible = $false $panel1.Controls.Add($label3) $panel1.Controls.Add($comboBox2) $panel1.BorderStyle = 1 $panel2 = New-Object system.Windows.Forms.Panel $panel2.AutoSize = $true $panel2.Width = 1200 $panel2.Height =200 $panel2.location = New-Object system.Drawing.Point(50,100) $panel2.Visible = $false $panel2.Controls.Add($label4) $panel2.Controls.Add($ComboBox3) $panel2.BorderStyle = 1 $Label1 = New-Object system.Windows.Forms.Label $Label1.text = "Select Module" $Label1.AutoSize = $true $Label1.location = New-Object System.Drawing.Point(35,50) $Label1.Font = 'segoe ui,9.5' $Label2 = New-Object system.Windows.Forms.Label $Label2.text = "SharePoint Settings" $Label2.AutoSize = $true $Label2.location = New-Object System.Drawing.Point(35,15) $Label2.Font = 'Segoe UI Semibold,9.5' $Label3 = New-Object system.Windows.Forms.Label $Label3.text = "Choose file and folder permission option" $Label3.AutoSize = $true $Label3.location = New-Object System.Drawing.Point(100,200) $Label3.Font = 'segoe ui,9.5' $Label4 = New-Object system.Windows.Forms.Label $Label4.AutoSize = $True $Label4.text = "File Permission" $Label4.location = New-Object System.Drawing.Point(100,200) $Label4.Font = 'Segoe UI ,9.5' ################ Module combo box ################ $ComboBox1 = New-Object system.Windows.Forms.ComboBox $ComboBox1.text = "Select Module" $ComboBox1.width = 200 $ComboBox1.height = 20 $ComboBox1.location = New-Object System.Drawing.Point(310, 45) $ComboBox1.Font = 'Microsoft Sans Serif,10' $combobox1.items.Add("ControlSettings") $combobox1.items.Add("NextSettings") ######## file and folder permission ############# $ComboBox2 = New-Object system.Windows.Forms.ComboBox $ComboBox2.text = "select an option" $ComboBox2.width = 200 $ComboBox2.height = 20 $ComboBox2.location = New-Object System.Drawing.Point(450, 200) $ComboBox2.Font = 'Microsoft Sans Serif,10' $ComboBox2.items.Add("View") $ComboBox2.items.Add("Edit") $ComboBox3 = New-Object system.Windows.Forms.ComboBox $ComboBox3.text = "select an option" $ComboBox3.width = 200 $ComboBox3.height = 20 $ComboBox3.location = New-Object System.Drawing.Point(450, 200) $ComboBox3.Font = 'Microsoft Sans Serif,10' $ComboBox3.items.Add("View") $ComboBox3.items.Add("Edit") Function Button2_Click() { if ($ComboBox1.SelectedIndex -eq 0) { $panel2.Visible = $true $panel1.Visible = $false } if ($ComboBox1.SelectedIndex -eq 1) { $panel1.Visible = $true $panel2.Visible = $false } } $Button1 = New-Object system.Windows.Forms.Button $Button1.text = "Start" $Button1.width = 150 $Button1.height = 30 $Button1.BackColor = '#F6CEE3' $Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK $Button1.location = New-Object System.Drawing.Point(500, 700) $Button1.Font = 'segoe ui,10' $Button1.Add_Click({ Button1_Click; $global:Button1Clicked = 1; }) ############# Button 'Go' ############# $Button2 = New-Object system.Windows.Forms.Button $Button2.text = "Go" $Button2.width = 100 $Button2.height = 30 $Button2.BackColor = '#F6CEE3' $Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK $Button2.location = New-Object System.Drawing.Point(680, 43) $Button2.Font = 'segoe ui,10' $Button2.Add_Click({ Button2_Click; $global:Button2Clicked = 1; }) ######### code Starts ########### function Button1_Click() { $link = $comboBox2.SelectedItem.ToString(); $linktype = $comboBox3.SelectedItem.ToString(); } ####### end of code ###### $form.Controls.Add($Panel1) $form.Controls.Add($Panel2) $form.Controls.Add($button1) $form.Controls.Add($comboBox1) $form.Controls.Add($button2) $form.Controls.Add($pictureBox1) $form.Controls.Add($label1) $form.Controls.Add($label2) [void]$Form.Add_Shown({ $Form.Activate() }) [void]$Form.ShowDialog()
问题是您的变量是在一个函数中设置的,因此它们的范围仅限于该函数。真的没有理由把它放在函数中,而是直接放在 .add_click()
中。或者,如果您觉得需要将它保留在函数中,您可以像使用 $global:ButtonClicked
一样设置变量的范围,并将它们设置为 $global:Link
和 $global:LinkType
.
编辑: Order 在 PowerShell 中确实有所不同,所以我确实在你的脚本中移动了一些东西以使其正常工作,但我能够获得值来反映当我把它们放在全局范围内时很好。
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
######### code Starts ###########
function Button1_Click()
{
write-host "Link = $global:link"
write-host "Link Type = $global:linktype"
}
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
####### end of code ######
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(@("View","Edit") )
#$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
当我 运行 设置两个组合框值并单击“开始”时,我在 ISE 中收到文本说明我将 Link 和 LinkType 变量设置为什么,以及能够回显 $global:link
和 $global:linktype
以查看正确分配的值。