【发布时间】: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