Function PS帮助使用函数和哈希表键入并单击
我有这样的想法:Function PS帮助使用函数和哈希表键入并单击,function,powershell,listbox,click,add,Function,Powershell,Listbox,Click,Add,我有这样的想法: #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_K
#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()
因此,我想点击一个按钮,填写我的列表,选择项目,当按enter键时,它会向我写入我声明要输入的键的值。
因此,如果我单击btn1,从列表中选择一个项目并按Enter键,它将写入主机值$hashT1。'KEY'例如:$hashT1=@{“文件1”=“Path\File1.bin”;“文件2”=“Path\File2.cue”}
单击btn1,用两行填写列表框,“文件1”和“文件2”。
选择其中任何一个,暂时选择文件2。
按enter键,它将写入主机:Path\File2.cue 我得到的结果:
“空行”后跟所选项目值 如果我将Func写入主机$currentHashT'File 2',它确实可以工作
如果我将Func写入主机$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
否则你就必须完全重新设计它。是的,我昨天意识到了这一点,并且已经重新设计为一个更少的noob脚本哈,ty。