【问题标题】:Powershell: creating dynamic formsPowershell:创建动态表单
【发布时间】:2015-12-11 09:43:31
【问题描述】:

我尝试使用具有目录名称的按钮创建一个动态表单。单击该按钮应获取目录名称并对其进行处理。

代码被删除:

{function convert_it($arg)

    $dir = "$source\$arg"
    $dir_liste =  Get-ChildItem $dir | Where-Object {$_.mode -match "d"} 
    $dir_count_total = $dir_liste.count

...

...

...

}



foreach ($dir in $dir_list)

{   
# Button
if ($split_count -eq 25){
    $x=250
    $y=50
}
elseif ($split_count -eq 50){
    $x=500
    $y=50
}

$dir_numbers = (get-childitem -Path $source\$dir -recurse | where-object { 
$_.PSIsContainer }).Count

$run = New-Object System.Windows.Forms.Button
$run.Location = New-Object System.Drawing.Size($x,$y)
$run.Size = New-Object System.Drawing.Size(100,20)
if ($dir_numbers -eq 0) {
    $run.Enabled = $false
}
$run.Text = "#$dir_count -> $dir -> $dir_numbers"

$run.Add_Click({ convert_it($dir) }.GetNewClosure())

$testForm.Controls.Add($run)
$Font = New-Object System.Drawing.Font("Times New Roman",14,
[System.Drawing.FontStyle]::Regular)
$run.font = $Font
$run.AutoSize = $True
$y+=30
$split_count+=1
$dir_count+=1

}







$testForm.ShowDialog()

当我运行脚本时,我收到以下错误:

convert_it :术语“convert_it”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者路径是否 包含,请验证路径是否正确,然后重试。

  • $run.Add_Click({ convert_it($dir) }.GetNewClosure())
  • ~~~~~~~~~~
    • CategoryInfo : ObjectNotFound: (convert_it:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException

我在这里做错了什么?:

$run.Add_Click({ convert_it $dir }.GetNewClosure())

【问题讨论】:

    标签: forms powershell dynamic


    【解决方案1】:

    看起来你的函数的左括号位置错误(应该在行尾而不是开头(这使它成为一个脚本块)。

    应该是这样的:

    function convert_it($arg) {
    
        $dir = "$source\$arg"
        $dir_liste =  Get-ChildItem $dir | Where-Object {$_.mode -match "d"} 
        $dir_count_total = $dir_liste.count
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 2016-11-11
      相关资源
      最近更新 更多