【问题标题】:ComboBox.SelectedItem giving Null valueComboBox.SelectedItem 给出 Null 值
【发布时间】:2021-08-21 18:00:48
【问题描述】:

我整天都被困在这个问题上。我正在使用 PowerShell 表单创建 UI。其中:

  • 用户从第一个组合框中选择一个选项。点击按钮前往
  • 根据选择,将出现一个面板,其中包含另一个组合框。
  • 如果用户在第一个组合框中选择另一个选项,则会出现另一个面板和另一个组合框
  • 从面板组合框中选择选项后,用户单击开始按钮。
  • 这会导致一个函数将选定的选项存储到一个变量中。

问题

  • 现在,当用户从面板的组合框中选择选项时,我使用 $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()

【问题讨论】:

    标签: powershell combobox panel


    【解决方案1】:

    问题是您的变量是在函数中设置的,因此它们的作用域是该函数。真的没有理由把它放在一个函数中,而是直接把它放在.add_click() 中。或者,如果您觉得需要将其保留在函数中,您可以像使用 $global:ButtonClicked 一样设置变量的范围,并将它们设置为 $global:Link$global:LinkType

    编辑:顺序确实对 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 以查看值正确分配。

    【讨论】:

    • 好的尝试像这样将它放入 .add_click 但仍然相同的 Null 值 $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({ $linktype = $comboBox3.SelectedItem.ToString(); $link = $comboBox2.SelectedItem.ToString(); Button1_Click; $global:Button1Clicked = 1; } )
    • 嗯,我想点击可能有一个我不知道的范围。尝试像使用 $global:Button1Clicked 一样确定变量的范围。
    • 你的意思是在同一个 .add_click $global:Link = $comboBox2.SelectedItem.ToString(); $global:Linktype = $comboBox3.SelectedItem.ToString(); }) 这也让我为 NULL
    • 它对我有用。不过,我确实重组了你的脚本。这是 PowerShell,而不是 C# 或某种顺序无关紧要的语言。在其他地方引用它们之前,您需要定义函数和对象。我将您的函数移到顶部,并在创建面板之前将组合框定义移动到它们的正下方,因为您将组合框添加到面板中。我会更新我的答案以反映我的所作所为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2011-09-06
    • 1970-01-01
    • 2016-01-29
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多