无法使ComboBox文本正确显示

时间:2019-12-04 18:09:03

标签: powershell combobox

我有一个脚本可以执行以下操作:

创建带有2个组合框和一个文本框的表单以供用户输入 我根据来自ComboBox 1的选择填充ComboBox 1并填充ComboBox2。另一部分起作用的是,基于ComboBox 1的选择,我可以使Text出现在表单上。 我的挑战是我所面临的挑战,优先考虑的是:

挑战1: 如果更改了ComboBox 1中的选择,如何更改表单上的文本不会更新?

挑战2 :(与挑战1关联) 我正在寻找一种将ComboBox 1与ComboBox 2与文本框进行组合的方法 例如:Comp1-Tst-MyTst?

挑战3: 我想导入CSV文件或使用Get-AD ....导入变量,而不是在脚本中进行硬编码。

挑战4: 我正在努力向表单添加图标

挑战5: 我想阻止“确定按钮”出现,直到选中了复选框

这是我的代码:

# Below is one of the Array's I'm adding    
$ADSites=@("S01","S02","S03")

$ADSiteS01=@("AAA","BBB","CCC")
$ADSiteS02=@("DDD","EEE","FFF")
$ADSiteS03=@("GGG","HHH","JJJ")

####################################################################################################    
#####   Create Combo Boxes
####################################################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(625,625)
$Form.FormBorderStyle = "FixedToolWindow"
$Combobox1 = New-Object System.Windows.Forms.Combobox
$Combobox1.Location = New-Object System.Drawing.Size(26,25)  
$Combobox1.Size = New-Object System.Drawing.Size(105,20)
$Combobox1.items.AddRange($ADSites)
$combobox2 = New-Object System.Windows.Forms.Combobox
$combobox2.Location = New-Object System.Drawing.Size(143,25)  
$combobox2.Size = New-Object System.Drawing.Size(105,20)
$textBoxFPS = New-Object System.Windows.Forms.TextBox 
$textBoxFPS.Location = New-Object System.Drawing.Point(26,75) 
$textBoxFPS.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxFPS)
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)

################################################################################################
#####   Create Text Box
################################################################################################
$textBoxFPS = New-Object System.Windows.Forms.TextBox 
$textBoxFPS.Location = New-Object System.Drawing.Point(26,75) 
$textBoxFPS.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxFPS)





################################################################################################
#####   Add Labels for the Combo Boxes
###################################################################################################

$lbADSub = New-Object System.Windows.Forms.Label   
$lbADSub.Text = "Select AD Site"; $lbADSub.Top = 5; $lbADSub.Left = 26; $lbADSub.Autosize = $true  
$form.Controls.Add($lbADSub)

$lbDeptSub = New-Object System.Windows.Forms.Label   
$lbDeptSub.Text = "Select Department"; $lbDeptSub.Top = 5; $lbDeptSub.Left = 143; 
$lbDeptSub.Autosize = $true  
$form.Controls.Add($lbDeptSub)  

$lbFPSSub = New-Object System.Windows.Forms.Label   
$lbFPSSub.Text = "Type in the Asset Tag"; $lbFPSSub.Top = 55; $lbFPSSub.Left = 26; 
$lbFPSSub.Autosize = $true
$form.Controls.Add($lbFPSSub) 

## CheckBox 
$chkThis = New-Object Windows.Forms.checkbox 
$chkThis.Text = "Verify New Computer Name" ; $chkThis.Left = 26; $chkThis.Top = 105; 
$chkThis.AutoSize = $true
$chkThis.Checked = $false   # set a default value 
$form.Controls.Add($chkThis) 


<#
$lbCompSub = New-Object System.Windows.Forms.Label   
$lbCompSub.Text = "Verify Computer Name"; $lbCompSub.Top = 105; $lbCompSub.Left = 26; 
$lbCompSub.Autosize = $true  
$form.Controls.Add($lbCompSub) 
#>


############################################################################################
#####   Create Ok and Cancel Buttons
############################################################################################

$buttonPanel = New-Object Windows.Forms.Panel
    $buttonPanel.Size = New-Object Drawing.Size @(400,40)
    $buttonPanel.Dock = "Bottom"

## Creating the Ok Button

    $okButton = New-Object Windows.Forms.Button
        $okButton.Top = $cancelButton.Top ; $okButton.Left = $cancelButton.Left - $okButton.Width - 5
        $okButton.Text = "Ok" 
        $okButton.DialogResult = "Ok" 
        $okButton.Anchor = "Left" 

## Creating the Cancel Button
    $cancelButton = New-Object Windows.Forms.Button
        $cancelButton.Left = $buttonPanel.Height - $cancelButton.Height - 10; $cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
        $cancelButton.Text = "Cancel"
        $cancelButton.DialogResult = "Cancel"
        $cancelButton.Anchor = "Right"

## Add the buttons to the button panel 
    $buttonPanel.Controls.Add($okButton) 
    $buttonPanel.Controls.Add($cancelButton) 
## Add the button panel to the form 
    $form.Controls.Add($buttonPanel) 
## Set Default actions for the buttons 
    $form.AcceptButton = $okButton          # ENTER = Ok 
    $form.CancelButton = $cancelButton      # ESCAPE = Cancel



##################################################################################################
#####   Now we do stuff
##################################################################################################

# Populate Combobox 2 When Combobox 1 changes
$ComboBox1_SelectedIndexChanged= {
    $combobox2.Items.Clear() # Clear the list
    $combobox2.Text = $null  # Clear the current entry
    Switch ($ComboBox1.Text) {
            "S01"{
            $ADSiteS01 | ForEach { 
                $labelClub = New-Object System.Windows.Forms.Label
                $labelClub.Location = New-Object System.Drawing.Point(20,200) 
                $labelClub.Size = New-Object System.Drawing.Size(280,20) 
                $labelClub.Text = "$($combobox1.SelectedItem)-"
                $form.Controls.Add($labelClub)
                $combobox2.Items.Add($_)
                #$labelClub.Text = "$($combobox2.SelectedItem)-"
            }
        }
            "S02"{
            $ADSiteS02 | ForEach {
                $labelClub = New-Object System.Windows.Forms.Label
                $labelClub.Location = New-Object System.Drawing.Point(20,200) 
                $labelClub.Size = New-Object System.Drawing.Size(280,20) 
                $labelClub.Text = "$($combobox1.SelectedItem)-"
                $form.Controls.Add($labelClub)
                $combobox2.Items.Add($_)
             }
        }
        "S03"{
            $ADSiteS03 | ForEach {
                $labelClub = New-Object System.Windows.Forms.Label
                $labelClub.Location = New-Object System.Drawing.Point(20,200) 
                $labelClub.Size = New-Object System.Drawing.Size(280,20) 
                $labelClub.Text = "$($combobox1.SelectedItem)-"
                $form.Controls.Add($labelClub)
                $combobox2.Items.Add($_)
              }
        }

       }
}

$ComboBox1.add_SelectedIndexChanged($ComboBox1_SelectedIndexChanged)
$Form.ShowDialog()

1 个答案:

答案 0 :(得分:-1)

这应该可以帮助您解决组合框问题和“确定”按钮:

# Below is one of the Array's I'm adding    
$ADSites=@("S01","S02","S03")

$ADSiteS01=@("AAA","BBB","CCC")
$ADSiteS02=@("DDD","EEE","FFF")
$ADSiteS03=@("GGG","HHH","JJJ")


Add-Type -AssemblyName System.Windows.Forms
#
####################################################################################################    
#####   Create Combo Boxes
####################################################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.Size = '625,625'
$Form.FormBorderStyle = "FixedToolWindow"
#
$Combobox1 = New-Object System.Windows.Forms.Combobox
$Combobox1.Location = '26,25'
$Combobox1.Size = '105,20'
$Combobox1.items.AddRange($ADSites)
#
$combobox2 = New-Object System.Windows.Forms.Combobox
$combobox2.Location = '143,25'
$combobox2.Size = '105,20'
#
#
$Form.Controls.Add($combobox1)
$Form.Controls.Add($combobox2)

################################################################################################
#####   Create Text Box
################################################################################################
$textBoxFPS = New-Object System.Windows.Forms.TextBox 
$textBoxFPS.Location = '26,75'
$textBoxFPS.Size = '165,20'
$textBoxFPS.Text = 'xx'
$form.Controls.Add($textBoxFPS)





################################################################################################
#####   Add Labels for the Combo Boxes
###################################################################################################

$lbADSub = New-Object System.Windows.Forms.Label   
$lbADSub.Text = "Select AD Site"; $lbADSub.Top = 5; $lbADSub.Left = 26; $lbADSub.Autosize = $true  
$form.Controls.Add($lbADSub)

$lbDeptSub = New-Object System.Windows.Forms.Label   
$lbDeptSub.Text = "Select Department"; $lbDeptSub.Top = 5; $lbDeptSub.Left = 143; 
$lbDeptSub.Autosize = $true  
$form.Controls.Add($lbDeptSub)  

$lbFPSSub = New-Object System.Windows.Forms.Label   
$lbFPSSub.Text = "Type in the Asset Tag"; $lbFPSSub.Top = 55; $lbFPSSub.Left = 26; 
$lbFPSSub.Autosize = $true
$form.Controls.Add($lbFPSSub) 

## CheckBox 
$chkThis = New-Object Windows.Forms.checkbox 
$chkThis.Text = "Verify New Computer Name" ; $chkThis.Left = 26; $chkThis.Top = 105; 
$chkThis.AutoSize = $true
$chkThis.Checked = $false   # set a default value 
$form.Controls.Add($chkThis) 

############################
# Label for choice selection
############################
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = '20,200'
$labelClub.Size = '280,20'
$labelClub.Text = "-"
$form.Controls.Add($labelClub)



<#
$lbCompSub = New-Object System.Windows.Forms.Label   
$lbCompSub.Text = "Verify Computer Name"; $lbCompSub.Top = 105; $lbCompSub.Left = 26; 
$lbCompSub.Autosize = $true  
$form.Controls.Add($lbCompSub) 
#>


############################################################################################
#####   Create Ok and Cancel Buttons
############################################################################################

$buttonPanel = New-Object Windows.Forms.Panel
    $buttonPanel.Size = New-Object Drawing.Size @(400,40)
    $buttonPanel.Dock = "Bottom"

## Creating the Ok Button

    $okButton = New-Object Windows.Forms.Button
        $okButton.Top = $cancelButton.Top ; $okButton.Left = $cancelButton.Left - $okButton.Width - 5
        $okButton.Text = "Ok" 
        $okButton.DialogResult = "Ok" 
        $okButton.Anchor = "Left" 
        $okButton.Enabled = $false

## Creating the Cancel Button
    $cancelButton = New-Object Windows.Forms.Button
        $cancelButton.Left = $buttonPanel.Height - $cancelButton.Height - 10; $cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
        $cancelButton.Text = "Cancel"
        $cancelButton.DialogResult = "Cancel"
        $cancelButton.Anchor = "Right"

## Add the buttons to the button panel 
    $buttonPanel.Controls.Add($okButton) 
    $buttonPanel.Controls.Add($cancelButton) 
## Add the button panel to the form 
    $form.Controls.Add($buttonPanel) 
## Set Default actions for the buttons 
    $form.AcceptButton = $okButton          # ENTER = Ok 
    $form.CancelButton = $cancelButton      # ESCAPE = Cancel



##################################################################################################
#####   Now we do stuff
##################################################################################################

# Populate Combobox 2 When Combobox 1 changes
$ComboBox1.add_SelectedIndexChanged({
    $combobox2.Items.Clear() # Clear the list
    $combobox2.Text = $null  # Clear the current entry
    Switch ($ComboBox1.Text) {
            "S01"{

            $ADSiteS01 | ForEach { 
                $combobox2.Items.Add($_)
            }
        }
            "S02"{
            $ADSiteS02 | ForEach {
                $combobox2.Items.Add($_)
             }
        }
        "S03"{
            $ADSiteS03 | ForEach {
                $combobox2.Items.Add($_)
              }
        }

    }
    $labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})

$ComboBox2.add_SelectedIndexChanged({
    $labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})

$textBoxFPS.add_TextChanged({
    $labelClub.Text = $combobox1.Text + "-" + $combobox2.Text + "-" + $textBoxFPS.Text
})

$chkThis.Add_CheckStateChanged({
   If ($chkThis.Checked) {
       $okButton.enabled = $true
   }
   Else {
    $okButton.enabled = $false
   }
})

$Form.ShowDialog()
  • 如果使用AD cmdlet将站点详细信息检索到对象中,则应该可以轻松地使用与现在相同的方法从该对象中填充列表。