【问题标题】:ComboBox.SelectedItem is nullComboBox.SelectedItem 为空
【发布时间】:2019-05-24 12:53:51
【问题描述】:

我用 AD SamAccountName 项填充了一个组合框。选择其中一个项目时,可以按一个按钮以便从该帐户中检索信息。但是,单击按钮时,我收到以下 错误

Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.

错误所指的命令是:

$Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName

代码的关键部分是:

        $ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
        $ComboBox.Width = 300
        $ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)

        $Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
        ForEach ($User in $Users){
            $ComboBox.Items.Add($User.SamAccountName)
        }
        $ComboBox.SelectedIndex = 1

            $MainWindow.Controls.Add($ComboBox)

        $Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
        $Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
        $Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
        $Button_Check_TEST.Text = 'Check'

        $MainWindow.Controls.Add($Button_Check_TEST)

        $Button_Check_TEST.Add_Click({
            Try{
                $Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
            }
            Catch{
                Write-Verbose -Verbose $_.Exception.Message
            }
        })

问题是,我需要两层。基本上,应该有一个包含四个不同选项的菜单,其中之一是“用户”。单击“用户”时,将出现组合框和“单击”按钮。 使用上面没有“用户”按钮的代码可以正常工作。

问题:为什么当我使用按钮“创建”ComboBox 时,ComboBox.SelectedItem 不起作用?

完整代码如下:

    $font = New-Object -TypeName System.Drawing.Font("Times New Roman", 18, [System.Drawing.FontStyle]::Bold)

    $MainWindow = New-Object -TypeName System.Windows.Forms.Form
    $MainWindow.Text = 'PIM v10 Administrator Window'
    $MainWindow.Width = 600
    $MainWindow.Height = 555
    $MainWindow.AutoSize = $true

    $Button_User = New-Object -TypeName System.Windows.Forms.Button
    $Button_User.Location = New-Object -TypeName System.Drawing.Size(25, 25)
    $Button_User.Size = New-Object -TypeName System.Drawing.Size(200, 75)
    $Button_User.Text = 'User'
    $Button_User.Font = $font

        $MainWindow.Controls.Add($Button_User)

    $Button_User.Add_Click({
    $Label_User = New-Object -TypeName System.Windows.Forms.Label
    $Label_User.Text = 'Given Name:'
    $Label_User.Location = New-Object -TypeName System.Drawing.Point(250, 50)
    $Label_User.AutoSize = $true

        $MainWindow.Controls.Add($Label_User)

    $Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
    $Label_User_ItemContent.Text = ''
    $Label_User_ItemContent.Location = New-Object -TypeName System.Drawing.Point(250, 100)

        $MainWindow.Controls.Add($Label_User_ItemContent)

    $ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
    $ComboBox.Width = 300
    $ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)

    $Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
     ForEach ($User in $Users){
         $ComboBox.Items.Add($User.SamAccountName)
        }
     $ComboBox.SelectedIndex = 1

        $MainWindow.Controls.Add($ComboBox)

     $Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
     $Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
     $Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
     $Button_Check_TEST.Text = 'Check'

     $MainWindow.Controls.Add($Button_Check_TEST)

     $Button_Check_TEST.Add_Click({
            Try{
                $Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
            }
            Catch{
                Write-Verbose -Verbose $_.Exception.Message
            }
        })

     if (-not ($ComboBox.SelectedItem -eq $null)){
         $Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
     }
     else {
        Write-Host -Object "Object is null"
     }
    })
    $MainWindow.ShowDialog()

【问题讨论】:

  • 我看到了错误。您正在 Add_Click 事件中创建组合变量。这意味着变量在错误的范围内。在您下次单击时调用它之前,它的内存已被清除

标签: powershell combobox selecteditem


【解决方案1】:

所以发生的事情是您在错误的范围内创建变量

$Button_User.Add_Click({
    $ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
    $Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
    $MainWindow.Controls.Add($ComboBox)
    $Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
    $Button_Check_TEST.Add_Click({
        $Label_User_ItemContent.Text = (Get-ADUser -Identity 
        $ComboBox.SelectedItem).SamAccountName
    )}
})

因为您是在 Add_click 操作中创建 Combo 和 Lable。这些值仅在执行操作时存在并且在$MainWindows.Controls 中。然后从内存中清除这些项目

当您运行下一个操作 $Button_Check_TEST.Add_Click() 时,因为变量已被清除,所以 $ComboBox$Label_User_ItemContent 不相等。

解决方法是将它们放在$Button_User.Add_Click() 事件之外

$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
$Button_User.Add_Click({
    $MainWindow.Controls.Add($ComboBox)
    $Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label
    $Button_Check_TEST.Add_Click({
        $Label_User_ItemContent.Text = (Get-ADUser -Identity 
        $ComboBox.SelectedItem).SamAccountName
    )}
})

这是整个脚本的工作状态

$font = New-Object -TypeName System.Drawing.Font("Times New Roman", 18, [System.Drawing.FontStyle]::Bold)

$MainWindow = New-Object -TypeName System.Windows.Forms.Form
$MainWindow.Text = 'PIM v10 Administrator Window'
$MainWindow.Width = 600
$MainWindow.Height = 555
$MainWindow.AutoSize = $true

$Button_User = New-Object -TypeName System.Windows.Forms.Button
$Button_User.Location = New-Object -TypeName System.Drawing.Size(25, 25)
$Button_User.Size = New-Object -TypeName System.Drawing.Size(200, 75)
$Button_User.Text = 'User'
$Button_User.Font = $font

$MainWindow.Controls.Add($Button_User)

$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$Label_User_ItemContent = New-Object -TypeName System.Windows.Forms.Label

$Button_User.Add_Click({
    $Label_User = New-Object -TypeName System.Windows.Forms.Label
    $Label_User.Text = 'Given Name:'
    $Label_User.Location = New-Object -TypeName System.Drawing.Point(250, 50)
    $Label_User.AutoSize = $true

    $MainWindow.Controls.Add($Label_User)


    $Label_User_ItemContent.Text = ''
    $Label_User_ItemContent.Location = New-Object -TypeName System.Drawing.Point(250, 100)

    $MainWindow.Controls.Add($Label_User_ItemContent)


    $ComboBox.Width = 300
    $ComboBox.Location = New-Object -TypeName System.Drawing.Point(250, 25)

    $Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^adminA'}
    $MainWindow.Controls.Add($ComboBox)
    ForEach ($User in $Users){
        $ComboBox.Items.Add($User.SamAccountName)
    }
    $MainWindow.Controls.Add($ComboBox)
    $ComboBox.SelectedIndex = 0   
    $Button_Check_TEST = New-Object -TypeName System.Windows.Forms.Button
    $Button_Check_TEST.Location = New-Object -TypeName System.Drawing.Size(350, 150)
    $Button_Check_TEST.Size = New-Object -TypeName System.Drawing.Size(150, 50)
    $Button_Check_TEST.Text = 'Check'

    $MainWindow.Controls.Add($Button_Check_TEST)

    $Button_Check_TEST.Add_Click({
        Try{
            $Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
        }
        Catch{
            Write-Verbose -Verbose $_.Exception.Message
        }
    })

    if (-not ($ComboBox.SelectedItem -eq $null)){
        $Label_User_ItemContent.Text = (Get-ADUser -Identity $ComboBox.SelectedItem).SamAccountName
    }
    else {
        Write-Host -Object "Object is null"
    }
})
$MainWindow.ShowDialog()

【讨论】:

    猜你喜欢
    • 2011-10-14
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 2017-02-10
    • 2021-08-21
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多