【问题标题】:Show a list when a choice is made做出选择时显示列表
【发布时间】:2019-05-05 05:09:41
【问题描述】:

我正在尝试解决 Windows.Form 下的脚本,但我有点卡住了。

我希望能够根据从第一个列表中做出的选择显示特定列表,这意味着在脚本开始时,只需要显示一个列表,并且根据所做的选择,还有许多其他列表可用。

这是完整的脚本供参考

#Open a Window.
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object Windows.Forms.Form
$form.text = "Contrôles"             
$form.Size = New-Object System.Drawing.Size(1000,700)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,150)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,150)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

#Create the Data table (DataTable).
$table1 = New-Object system.Data.DataTable
$table2 = New-Object system.Data.DataTable

#Define the 2 column (Name, Type).
$colonne1 = New-Object system.Data.DataColumn Choice,([string])
$colonne2 = New-Object system.Data.DataColumn Choice,([string])

#Create columns in the data table.
$table1.columns.add($colonne1)
$table2.columns.add($colonne2)

#Add the data line by line in the data table.
$ligne = $table1.NewRow()   #Creation of the new row.
$ligne.Choice = "Service"    #In the column Choice we put the value we want.
$table1.Rows.Add($ligne)    #Add a line in the data table.
$ligne = $table1.NewRow()
$ligne.Choice = "Software"
$table1.Rows.Add($ligne)
$ligne = $table1.NewRow()
$ligne.Choice = "Other"
$table1.Rows.Add($ligne)

#Add the data line by line in the data table.
$ligne = $table2.NewRow()   #Creation of the new row.
$ligne.Choice = "Service Enable"  #In the column Choice we put the value we want.   
$table2.Rows.Add($ligne)    #Add a line in the data table.
$ligne = $table2.NewRow()
$ligne.Choice = "Service Disable"
$table2.Rows.Add($ligne)
$ligne = $table2.NewRow()
$ligne.Choice = "Other"
$table2.Rows.Add($ligne)

#Create the View.
$vu1 = New-Object System.Data.DataView($table1)
$vu1.Sort="Choice ASC"   #Tri la colonne "Extension" par ordre croissant.

$vu2 = New-Object System.Data.DataView($table2)
$vu2.Sort="Choice ASC"

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(650,50)
$label.Size = New-Object System.Drawing.Size(280,35)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(650,100)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

#Create the Drop-down list (ComboBox).
$liste1 = New-Object System.Windows.Forms.Combobox
$liste1.Location = New-Object Drawing.Point 20,50
$liste1.Size = New-Object System.Drawing.Size(150, 50)
$liste1.DropDownStyle = "DropDownList"

$liste2 = New-Object System.Windows.Forms.Combobox
$liste2.Location = New-Object Drawing.Point 350,50
$liste2.Size = New-Object System.Drawing.Size(150, 50)
$liste2.DropDownStyle = "DropDownList"

#Associate the Data to the Drop-down list
#To do so, we create a "Binding Context".
$liste1.BindingContext = New-Object System.Windows.Forms.BindingContext
$liste1.DataSource = $vu1  #Assigne the view that contains the sorted Data.
$liste1.DisplayMember = "Choice"  #Column that will be displayed (Choice).  

$liste2.BindingContext = New-Object System.Windows.Forms.BindingContext
$liste2.DataSource = $vu2   #Assigne the view that contains the sorted Data.
$liste2.DisplayMember = "Choice"    #Column that will be displayed (Choice). 

#Attach the control to the window.
$form.controls.add($liste1)
$form.controls.add($liste2)

#Show everything.
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

#Work the code arround.
if ($liste1.DisplayMember= "Service Enable")
{set-service -name RemoteRegistry -ComputerName $textBox.Text -StartupType Automatic}

if ($liste1.DisplayMember = "Service Disable")
{set-service -name RemoteRegistry -ComputerName $textBox.Text -StartupType Automatic}
Write-Host "ComboBox = " $liste1.DisplayMember
Write-Host "ComboBox = " $liste2.selectedvalue

#Fin.

如果有人知道我可以在哪里看,那就太好了。

谢谢你 纳德

【问题讨论】:

  • 不幸的是,编程问题在这里是题外话,但这看起来非常适合我们的姐妹网站Stack Overflow。您应该请版主将您的问题迁移到那里。

标签: powershell list inbox


【解决方案1】:

1. You have no form / trigger events in your code.

2. You don't have the correct GUI objects in your code to hold a list / record result.

表单只是一个容纳元素的容器,直到您在后面添加代码以使其执行某些操作。您必须有一个适当的 GUI 对象才能将该结果发送到。

我不确定您是在 ISE 或 VSCode 或记事本或其他任何工具中手动完成这一切,但这是一个很好的第一次尝试。但是,您显示的内容似乎表明您在 GUI 开发/一般应用程序开发工作方面并没有真正赶上进度,因为您所做的并不是 PowerShell 真正独有的,而是任何应用程序开发客户端或 Web 都需要的。

所以,真的,花一些时间学习/审查一般的 WPF/Winforms 开发,并且将涵盖表单事件的内容。

至于你的用例,你需要:

  • 定义列表 GUI 对象(多行、列表框、列表视图、数据网格)以保存结果(同步组合框意味着在事件操作中添加和删除元素)
  • 定义该列表是什么(文本文件、数据库读取等)
  • 在单击、更改或其他表单事件中,从该列表中读取并填充 GUI 列表对象

这个网站和整个网络上有很多这样的例子。

Here a good video on GUI development with PowerShell:

powershell populate combobox basing on the selected item on another combobox

从上面的讨论(不是在不了解是什么和为什么的情况下添加到您的代码中的东西):

Use a ComboBox.SelectionChangeCommitted Event:

"Occurs when the user changes the selected item and that change is displayed in the ComboBox"
$combobox2_SelectionChangeCommitted={

  $Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
  foreach ($mailbox in $Mailboxes)
  {
      $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
      Load-ComboBox $combobox2 $CurrentMailbox -Append
  }

}

Use a button: 
$button1_Click={

$Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
  foreach ($mailbox in $Mailboxes)
  {
      $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
      Load-ComboBox $combobox2 $CurrentMailbox -Append
  }
}

最后,使用这个……

Write-Host "ComboBox = " $liste1.DisplayMember
Write-Host "ComboBox = " $liste2.selectedvalue

... 不是一个人会做的事情,因为没有打开控制台来查看这些结果,并且应该避免使用 Write-Host,除非在使用其他仅控制台格式化场景的仅控制台文本着色时,它也会清空显示缓冲区,所以它不能被发送到其他任何东西。此外,您在表单的任何位置都没有名为“ComboBox”的 GUI 对象,因此它对您的用例没有任何用途。

【讨论】:

    【解决方案2】:

    经过一段时间的研究,我设法找到了我真正需要的东西。

    这可能会对偶然发现该帖子的人有所帮助,所以这里只是我发现的一小部分

    function Service()
    {if ($ListBox1.SelectedItem -eq 'Enable Services')
    {
    $form.Controls.Add($Label3)
    $form.Controls.add($ListBox2)
    $form.Controls.Add($Label4)
    $form.Controls.Add($textBox)
    $form.Controls.Add($Button2)
    $form.Controls.Add($Button3)
    }
    

    我首先创建一个具有名称的函数,其中将计算在我的列表框“ComboBox”中做出选择时我希望发生的情况

    $button1.add_Click({ Service })
    

    然后我从我添加的按钮调用该函数,单击该按钮时将出现其他框。

    这与@Postanote 的回答没有太大区别,但这是我更放心的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2013-01-13
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多