0
votes

I try to create a dynamic form with buttons which have directory names. Clicking in the button should take the directory name and process it.

code sinpped:

{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()

When I run the script, I get the following error:

convert_it : The term 'convert_it' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

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

What do I wrong here?:

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

1 Answers

0
votes

It looks like you have the opening bracket in the wrong location for your function (should be at the end of the line instead of at the beginning (this makes it a scriptblock instead).

Should be this:

function convert_it($arg) {

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