首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ComboBox.SelectedItem给出零值

ComboBox.SelectedItem给出零值
EN

Stack Overflow用户
提问于 2021-06-03 15:30:05
回答 1查看 61关注 0票数 0

我被困在这里面一整天了。我正在使用PowerShell表单创建一个UI。在这方面:

  • 用户从第一个组合框中选择一个选项。根据所选选项单击Go
  • ,将出现一个具有另一个组合框的面板。如果用户在第一个组合框中选择另一个选项,则另一个面板在从面板组合框中选择选项后出现另一个组合框
  • ,用户单击开始按钮。
  • 将导致将所选选项存储到变量的函数。

f 211

问题

  • 现在当用户从面板的组合框中选择选项时,我使用$combobox.selecteditem.Tostring来获取值。,但它给我的结果为空。这里是我的代码。System.Windows.Forms.Application::EnableVisualStyles() $global:Button1Clicked = 0;加式-AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing 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;$global:Button2Clicked=;( }) $panel1 =新对象system.Windows.Forms.Panel $panel1.AutoSize = $true $panel1.Width = 1200 $panel1.Height =200 $panel1.location =新对象system.Drawing.Point(50,100) $panel1.Visible = $false $panel1.Controls.Add($label3) $panel1.Controls.Add($comboBox2) $panel1.BorderStyle =1 $panel2 =新对象system.Windows.Forms.Panel =$panel1.BorderStyle= 1200 = 200 = New-Object (50,100)=$false $panel2.Controls.Add($label4) $panel2.Controls.Add($ComboBox3) $panel2.BorderStyle =1 $Label1 =新对象system.Windows.Forms.Label $Label1.text = "Select“$Label1.AutoSize = $true $Label1.location = New-Object System.Drawing.Point(35,50) $Label1.Font = 'segoe ui,9.5‘$Label2 =新对象system.Windows.Forms.Label $Label2.text = "SharePoint Settings“$Label2.AutoSize = $true $Label2.location = New-Object System.Drawing.Point(35,15) $Label2.Font = 'Segoe UI半粗体,9.5’$Label3 =新对象system.Windows.Forms.Label $Label3.text =”选择文件和文件夹权限选项“$Label3.AutoSize = $true $Label3.location = New-Object System.Drawing.Point(100,200) $Label3.Font = 'segoe ui,9.5‘$Label4 =新对象system.Windows.Forms.Label $Label4.AutoSize = $True $Label4.text =“文件权限”$Label4.location =新对象System.Drawing.Point(100,200) $Label4.Font = 'Segoe,9.5‘################模块组合框################ $ComboBox1 = New-Object system.Windows.Forms.ComboBox $ComboBox1.text = "Select“$ComboBox1.width = 200 $ComboBox1.height = 20 $ComboBox1.location = New-Object System.Drawing.Point(310,45) $ComboBox1.Font = 'Microsoft,10‘$combobox1.items.Add("ControlSettings") $combobox1.items.Add("NextSettings") ########文件和文件夹权限############# $ComboBox2 = New-Object system.Windows.Forms.ComboBox $ComboBox2.text = "select a option“$ComboBox2.width = 200 $ComboBox2.height = 20 $ComboBox2.location = New-Object System.Drawing.Point(450,200) $ComboBox2.Font = 'Microsoft,10‘$ComboBox2.items.Add(“查看”)$ComboBox2.items.Add(“编辑”) $ComboBox3 =新对象system.Windows.Forms.ComboBox $ComboBox3.text =“选择选项”$ComboBox3.width = 200 $ComboBox3.height = 20 $ComboBox3.location =新对象System.Drawing.Point(450,200) $ComboBox3.Font = 'Microsoft,10‘$ComboBox3.items.Add(“查看”)$ComboBox3.items.Add(“编辑”)函数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 $Button1= "Start“$Button1= 150 = 30 =’F6CEE3‘$Button1.DialogResult = System.Windows.Forms.DialogResult::OK $Button1.location =新对象System.Drawing.Point(500,$Button1.Font = 'segoe,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,10‘$Button2.Add_Click({ Button2_Click;$global:Button2Clicked = 1;}) #########代码启动###########函数Button1_Click() { $link = $comboBox2.SelectedItem.ToString();$linktype = $comboBox3.SelectedItem.ToString();}代码void$Form.ShowDialog()

####### end of ###### $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($pictureBox1) $form.Controls.Add($label1) $label1({(()) })

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-03 17:01:51

问题是您的变量是在一个函数中设置的,所以它们的作用域是该函数。没有理由将其放入函数中,而是直接放在.add_click()中。或者,如果您认为需要将其保存在函数中,则可以像对$global:ButtonClicked那样对变量进行范围调整,并将它们设置为$global:Link$global:LinkType

编辑: Order确实在PowerShell中起了作用,所以我确实在脚本中移动了一些东西以使其正常工作,但是当我将它们放在全局范围内时,我能够得到很好的反映它们的值。

代码语言:javascript
复制
$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()

当我运行它时,设置两个组合框值,并单击Start,我在ISE中得到了文本,上面写着我将链接和LinkType变量设置为什么,并且能够回显$global:link$global:linktype以查看正确分配的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67824400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档