PS 使用函数和哈希表帮助 KeyDown 和 Click
PS Help to KeyDown and Click with functions and hash tables
我有这样的东西:
#initialize System.Drawing and System.Windows.Forms
#$form = New-Object System.Windows.Forms.Form , $form.text, $form.Size etc
#$listBox = New-Object System.Windows.Forms.ListBox , $listBox.Location etc
$form.KeyPreview = $True
$form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){
#call func passing $item value to the currentItemSelected parameter of func
$item=$listBox.SelectedItem;(Func -currentItemSelected $item)}})
$form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$form.Close()}})
$hashT1 = Get-Content -raw "path\hashT.txt" | ConvertFrom-StringData
$hashT2 = Get-Content -raw "path\hashT2.txt" | ConvertFrom-StringData
#arg is recieved from Btn.Add_Click(), btn1 clicked arg1 = $hashT1, btn2 = $hashT2 ...
function PopList($arg1){
$listBox.Items.Clear()
foreach ($game in $arg1.GetEnumerator() | sort Name){
$listBox.Items.Add("{0}" -f $game.Key)}
##Function recieve $hashT1 or any table I state when I click a Button
##and recieve the listbox.SelectedItem value when I press enter
##Write-Host is just to check if the variables recieved the correct info
function Func($currentHashT, $currentItemSelected ){
Write-Host $currentHashT.$currentItemSelected}
$btn1 = New-Object System.Windows.Forms.Button
#{.location, .size, .text...}
#PopList fill list with $hashT1 keys and Func $currentHashT should be $hashT1 'cause I'm
#passing $hashT1 as argument
$btn1.Add_Click({PopList $hashT1; Func -currentHashT $hashT1})
$form.Controls.Add($bt1)
$btn2 = New-Object System.Windows.Forms.Button
#{.location, .size, .text...}
#PopList fill list with $hashT2 keys and Func $currentHashT should be $hashT2 'cause I'm
#passing $hashT2 as argument
$btn2.Add_Click({PopList $hashT2; Func -currentHashT $hashT2})
$form.Controls.Add($btn2)
$form.Controls.Add($listBox)
$form.Add_Shown({$form.Activate()})
[void] $form.ShowDialog()
因此,我想单击一个按钮,填写我的列表,select 项,当按下回车键时,它会向我写入我声明我想要的 KEY 的 VALUE。
因此,如果我单击 btn1,select 列表中的一个项目并按 Enter,它将写入主机 $hashT1 的值。'KEY' 例如:
$hashT1 = @{"File 1" = "Path\File1.bin"; "File 2" = "Path\File2.cue"}
单击 btn1,用 2 行填充列表框,"File 1" 和 "File 2".
select其中任何一个,现在是文件 2。
按下回车,它会写主机:Path\File2.cue
我得到的结果:
"blank line" 后跟所选项目值
如果我将 Func 写入 Write-Host $currentHashT。'File 2' 它确实有效
如果我将我的 Func 写到 Write-Host $hashT1.$currentItemSelected 它确实有效
但是如果我这样做,我将不得不为我想添加的每个按钮创建一个功能,我可以按照我想要的方式来做吗?使用根据我的需要而变化的变量...
感谢阅读我的问题。
问题不在于变量更改值,问题在于您正试图调用带有两个参数的函数,例如
function add ($a, $b) {
write-host ($a + $b)
}
add -a 4
add -b 5
并期望以某种方式只调用函数一次,然后打印 9。这永远行不通。
您可以通过将 $currentHashT
保留在父作用域中来实现,函数只是引用它而不是通过参数传入它:
function add($b) {
write-host ($firstNUm + $b)
}
$n = 4
$firstNUm = $n
add -b 5
$n = 5
$firstNUm = $n
add -b 5
否则你必须完全重新设计它。
我有这样的东西:
#initialize System.Drawing and System.Windows.Forms
#$form = New-Object System.Windows.Forms.Form , $form.text, $form.Size etc
#$listBox = New-Object System.Windows.Forms.ListBox , $listBox.Location etc
$form.KeyPreview = $True
$form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){
#call func passing $item value to the currentItemSelected parameter of func
$item=$listBox.SelectedItem;(Func -currentItemSelected $item)}})
$form.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$form.Close()}})
$hashT1 = Get-Content -raw "path\hashT.txt" | ConvertFrom-StringData
$hashT2 = Get-Content -raw "path\hashT2.txt" | ConvertFrom-StringData
#arg is recieved from Btn.Add_Click(), btn1 clicked arg1 = $hashT1, btn2 = $hashT2 ...
function PopList($arg1){
$listBox.Items.Clear()
foreach ($game in $arg1.GetEnumerator() | sort Name){
$listBox.Items.Add("{0}" -f $game.Key)}
##Function recieve $hashT1 or any table I state when I click a Button
##and recieve the listbox.SelectedItem value when I press enter
##Write-Host is just to check if the variables recieved the correct info
function Func($currentHashT, $currentItemSelected ){
Write-Host $currentHashT.$currentItemSelected}
$btn1 = New-Object System.Windows.Forms.Button
#{.location, .size, .text...}
#PopList fill list with $hashT1 keys and Func $currentHashT should be $hashT1 'cause I'm
#passing $hashT1 as argument
$btn1.Add_Click({PopList $hashT1; Func -currentHashT $hashT1})
$form.Controls.Add($bt1)
$btn2 = New-Object System.Windows.Forms.Button
#{.location, .size, .text...}
#PopList fill list with $hashT2 keys and Func $currentHashT should be $hashT2 'cause I'm
#passing $hashT2 as argument
$btn2.Add_Click({PopList $hashT2; Func -currentHashT $hashT2})
$form.Controls.Add($btn2)
$form.Controls.Add($listBox)
$form.Add_Shown({$form.Activate()})
[void] $form.ShowDialog()
因此,我想单击一个按钮,填写我的列表,select 项,当按下回车键时,它会向我写入我声明我想要的 KEY 的 VALUE。
因此,如果我单击 btn1,select 列表中的一个项目并按 Enter,它将写入主机 $hashT1 的值。'KEY' 例如:
$hashT1 = @{"File 1" = "Path\File1.bin"; "File 2" = "Path\File2.cue"}
单击 btn1,用 2 行填充列表框,"File 1" 和 "File 2".
select其中任何一个,现在是文件 2。
按下回车,它会写主机:Path\File2.cue
我得到的结果:
"blank line" 后跟所选项目值
如果我将 Func 写入 Write-Host $currentHashT。'File 2' 它确实有效
如果我将我的 Func 写到 Write-Host $hashT1.$currentItemSelected 它确实有效
但是如果我这样做,我将不得不为我想添加的每个按钮创建一个功能,我可以按照我想要的方式来做吗?使用根据我的需要而变化的变量...
感谢阅读我的问题。
问题不在于变量更改值,问题在于您正试图调用带有两个参数的函数,例如
function add ($a, $b) {
write-host ($a + $b)
}
add -a 4
add -b 5
并期望以某种方式只调用函数一次,然后打印 9。这永远行不通。
您可以通过将 $currentHashT
保留在父作用域中来实现,函数只是引用它而不是通过参数传入它:
function add($b) {
write-host ($firstNUm + $b)
}
$n = 4
$firstNUm = $n
add -b 5
$n = 5
$firstNUm = $n
add -b 5
否则你必须完全重新设计它。