我在 gui powershell 中有按钮功能的问题 [英] Im having promlen with button functions in gui powershell

查看:58
本文介绍了我在 gui powershell 中有按钮功能的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里挣扎.我不知道如何让按钮功能读取 $listbox1.selectedItems 和 $listbox2.selectedItem这里有什么问题吗?如果我在按下 OK 按钮后尝试查看 $listbox 项目,它会显示我,但如果我调用功能按钮则不会显示,我删除了一些不必要的代码部分.

I am struggling here. i cannot figure out how to make the button functions read the $listbox1.selectedItems and $listbox2.selectedItem whats wrong here? if i try to look at the $listbox items after the OK button been pressed it will show me but not if i call the function button, ive deleted some unneccesery code parts.

$form = New-Object System.Windows.Forms.Form
$form.StartPosition = 'CenterScreen'

$button1 = New-Object System.Windows.Forms.Button
$button1.Text = 'Link'

$button2 = New-Object System.Windows.Forms.Button
$button2.Text = 'UnLink'

$button3 = New-Object System.Windows.Forms.Button
$button3.Text = 'ShowGPOlink'

$button4 = New-Object System.Windows.Forms.Button
$button4.Text = 'ShowOUlink'

#OK Button
$button5 = New-Object System.Windows.Forms.Button
$button5.Text = 'Done'
$button5.DialogResult = [System.Windows.Forms.DialogResult]::OK

$listBox1 = New-Object System.Windows.Forms.ListBox
$listbox1.SelectionMode = 'MultiExtended'

$listBox2 = New-Object System.Windows.Forms.ListBox

[void] $listBox1.Items.addRange($GPOLIST)
[void] $listBox2.Items.Addrange($OUHOLDER.CanonicalName)

$form.Controls.Add(...)
$form.AcceptButton = $button5

$button1.Add_Click({ LinkFn })
$button2.Add_Click({ UnLinkFn })
$button3.Add_Click({ ShowGPO })
$button4.Add_Click({ ShowOU })

function LinkFn {
#for some reason it returns nothing
$listBox1.selecteditems
$listbox2.SelectedItem 
Write-Host "function link"
}
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK){ 
$listBox1.selecteditems
$listbox2.SelectedItem  }

推荐答案

为什么不利用提供的 PowerShell 文档示例并根据需要进行调整:

Why are you not leveraging the provided PowerShell docs examples and tweaking as needed:

从列表框中选择项目

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select a Computer'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$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,120)
$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)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select a computer:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80

[void] $listBox.Items.Add('atl-dc-001')
[void] $listBox.Items.Add('atl-dc-002')
[void] $listBox.Items.Add('atl-dc-003')
[void] $listBox.Items.Add('atl-dc-004')
[void] $listBox.Items.Add('atl-dc-005')
[void] $listBox.Items.Add('atl-dc-006')
[void] $listBox.Items.Add('atl-dc-007')

$form.Controls.Add($listBox)

$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $listBox.SelectedItem
    $x
}

如果您定义了多选,则将块更改为此...

If you have the Multiselect defined, then change the block to this...

$listBox.SelectionMode = 'MultiExtended'  


if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    ForEach ($ListItem in $listBox.SelectedItems)
    {$ListItem}
}

所以,正如我所说的,这要求我重新编写您在此处的内容以进行解释并向您展示您将要查看的内容.这将显示格式化的 GUI 和一个预填充的列表,当您从 listbox1 中单选或多选项目并单击链接按钮时,会触发函数 LinkFn,它将将该选择复制到 Listbox2 并且不会关闭表单.

So, as I said, this required me to physically re-write what you have here to explain and show you what you would be looking at. This will show the formatted GUI, with a prepopulated list, that when you single or multi-select and item from listbox1, and click the Link button, which fires the function LinkFn, it will copy that selection to Listbox2 and will not close the form.

除了将结果发送到另一个 GUI 元素以及不通过默认的 OK 返回关闭表单之外,下面的代码与 PS 帮助中显示的代码没有任何不同.所有这些都是 GUI 设计中的普遍现象.

The below code is not doing anything differently than what is shown in the PS help other than sending the results to another GUI element, and not closing the form via the default OK return. All of which is a common thing in GUI design.

#region Begin environment initialization

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#endregion Begin environment initialization


#region Begin functions and code behind

function DoneFn { }
function ShowModuleFn { }
function ShowProcessFn { }
function UnLinkFn { }
function LinkFn 
{ 
    $listBox1.selecteditems
    # $listbox2.SelectedItems
    [void] $listBox2.Items.Addrange($listBox1.selecteditems)
}

$List1 = (Get-Process).Name
# $List2 = (Get-Module).Name

#endregion End functions and code behind


#region Begin  GUI code
$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '634,339'
$Form.text                       = 'Form'
$Form.TopMost                    = $false

$ListBox1                        = New-Object system.Windows.Forms.ListBox
$ListBox1.text                   = 'listBox1'
$listBox1.SelectionMode          = 'MultiExtended'
$ListBox1.width                  = 269
$ListBox1.height                 = 176
$ListBox1.location               = New-Object System.Drawing.Point(17,21)

$ListBox2                        = New-Object system.Windows.Forms.ListBox
$ListBox2.text                   = 'listBox2'
$listBox2.SelectionMode          = 'MultiExtended'
$ListBox2.width                  = 300
$ListBox2.height                 = 175
$ListBox2.location               = New-Object System.Drawing.Point(318,21)

$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = 'Link'
$Button1.width                   = 60
$Button1.height                  = 30
$Button1.location                = New-Object System.Drawing.Point(20,238)
$Button1.Font                    = 'Microsoft Sans Serif,10'

$Button2                         = New-Object system.Windows.Forms.Button
$Button2.text                    = 'Unlink'
$Button2.width                   = 60
$Button2.height                  = 30
$Button2.location                = New-Object System.Drawing.Point(127,241)
$Button2.Font                    = 'Microsoft Sans Serif,10'

$Button3                         = New-Object system.Windows.Forms.Button
$Button3.text                    = 'ShowProcess'
$Button3.width                   = 88
$Button3.height                  = 30
$Button3.location                = New-Object System.Drawing.Point(20,297)
$Button3.Font                    = 'Microsoft Sans Serif,10'

$Button4                         = New-Object system.Windows.Forms.Button
$Button4.text                    = 'ShowModules'
$Button4.width                   = 105
$Button4.height                  = 30
$Button4.location                = New-Object System.Drawing.Point(127,297)
$Button4.Font                    = 'Microsoft Sans Serif,10'

$Button5                         = New-Object system.Windows.Forms.Button
$Button5.text                    = 'Done'
$Button5.width                   = 60
$Button5.height                  = 30
$Button5.location                = New-Object System.Drawing.Point(556,298)
$Button5.Font                    = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@(
    $ListBox1,
    $ListBox2,
    $Button1,
    $Button2,
    $Button3,
    $Button4,
    $Button5
))

$Button1.Add_Click({ LinkFn })
$Button2.Add_Click({ UnLinkFn })
$Button3.Add_Click({ ShowProcessFn })
$Button4.Add_Click({ ShowModuleFn })
$Button5.Add_Click({ DondFn })

[void] $listBox1.Items.addRange($List1)
# [void] $listBox2.Items.Addrange($List2)

#endregion End GUI code

# Call the GUI
[void]$Form.ShowDialog()

这篇关于我在 gui powershell 中有按钮功能的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆