2014-09-09 3 views
0

두 개의 스크립트가 있습니다. 첫 번째 함수는 내 함수를 포함합니다. 두 번째 스크립트는 첫 번째 스크립트의 함수를 사용합니다.powershell 버튼을 클릭하여 새 단추 시작

버튼 클릭을 제외한 모든 기능이 원활하게 작동합니다. 그러면 다음 오류가 발생합니다. 이 설정되지 않았기 때문에

  • 변수 '$ ClickButton'

    검색 할 수 없습니다. 에서 D : \ 테스트 스크립트 \ \을 MyFunctions \ MyFunctions.ps1 31 문자 : 24
  • $ Button.Add_Click ($ {} ClickButton)
  • ~~~~~~~~~~~~ CategoryInfo : InvalidOperation (ClickButton : 문자열)] RuntimeExceptio
  • FullyQualifiedErrorId : VariableIsUndefined *

모든 파라미터 함수에 전달되지만, $clickButton 파라미터가 허용되는 것은 아니다. 이 작업을 수행 할 수 없습니까, 아니면 변경해야합니까?

먼저 스크립트 (MyFunctions.ps1) :

Function Add-Button { 
param(
    [int]$ButtonX, 
    [int]$ButtonY, 
    [int]$ButtonWidth, 
    [int]$ButtonHeight, 
    [string]$ButtonName, 
    [string]$ClickButton 
    ) 

$Button = New-Object System.Windows.Forms.Button 
$Button.Location = New-Object System.Drawing.Size($ButtonX,$ButtonY) 
$Button.Size = New-Object System.Drawing.Size($ButtonWidth,$ButtonHeight) 
$Button.Text = $ButtonName 
$Button.Add_Click({$ClickButton}) 
$objForm.Controls.Add($Button) 
} 

두 번째 스크립트

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 


. .\MyFunctions\MyFunctions.ps1 




Function MainForm { 

$MainForm = New-Object System.Windows.Forms.Form  
$MainForm.Size = New-Object System.Drawing.Size(700,500) 
$MainForm.Text= "Apply RetentionPolicy" 
$objform=$MainForm 

Add-Button -ButtonX 20 -ButtonY 20 -ButtonWidth 250 -ButtonHeight 40 -ButtonName "Apply to User" -ClickButton ApplyToUser 


$MainForm.Add_Shown({$MainForm.Activate()}) 
[void] $MainForm.ShowDialog() 
} 

Function ApplyToUser { 

$testform = New-Object System.Windows.Forms.Form  
$testform.Size = New-Object System.Drawing.Size(700,500) 
$testform.Text= "test" 
Add-TextLabel -LabelX 330 -LabelY 30 -LabelWidth 450 -LabelHeigth 40 -Text "test test test" 
$testform.Add_Shown({$testform.Activate()}) 
[void] $testform.ShowDialog() 
} 


Mainform 

답변

0

변경 한 후 대신 문자열은 ScriptBlock과로 $의 ClickButton의 매개 변수는 추가를 호출하는 방법을 변경 - 버튼 절차.

Function Add-Button { 
param(
    [int]$ButtonX, 
    [int]$ButtonY, 
    [int]$ButtonWidth, 
    [int]$ButtonHeight, 
    [string]$ButtonName, 
    [ScriptBlock]$ClickButton #<-- Change from [String] To [ScriptBlock] 
    ) 

$Button = New-Object System.Windows.Forms.Button 
$Button.Location = New-Object System.Drawing.Size($ButtonX,$ButtonY) 
$Button.Size = New-Object System.Drawing.Size($ButtonWidth,$ButtonHeight) 
$Button.Text = $ButtonName 
$Button.Add_Click($ClickButton) #<-- Remove the curly braces {} around variable 
$objForm.Controls.Add($Button) 
} 


Add-Button -ButtonX 20 -ButtonY 20 -ButtonWidth 250 -ButtonHeight 40 ` 
    -ButtonName "Apply to User" ` 
    -ClickButton {ApplyToUser} #<-- Add curly braces around your function reference 
+0

예, 그랬습니다. 고맙습니다. 하루 종일 작동하도록 노력하고 $ Button.Add_Click ({$ ClickButton}) 함수를 $ Button.Add_Click ({ApplyToUser})으로 수정하면 모든 것이 잘 작동하므로 이해하지 못했습니다. –

관련 문제