2012-05-08 51 views
1

我有「事件處理」這個功能,但我無法在$事件來定義動作區間值,窗口形式按鈕功能的PowerShell

$event=Register-ObjectEvent $Button click -SourceIdentifier $clickV -action {write-host $buttonV } 

我有按鈕,但沒有輸出請參閱下面的功能PARAM,

如果我改變符合:

$event=Register-ObjectEvent $Button click -SourceIdentifier $clickV -action {write-host "OK"} 

我有按鈕和輸出

任何幫助將受到歡迎,


Function Button 
{ 
    param(
     [int]$EmplXButton, 
     [int]$EmplYButton, 
     [innt]$LongButton, 
     [int]$LargButton, 
     [string]$ButtonName, 
     [string]$clickV, 
     [int]$ButtonV 
    ) 
    $Button = New-Object System.Windows.Forms.Button 
    $Button.Location = New-Object System.Drawing.Size($EmplXButton,$EmplYButton) 
    $Button.Size = New-Object System.Drawing.Size($LongButton,$LargButton) 
    $Button.Text = $ButtonName 
    $event=Register-ObjectEvent $Button click -SourceIdentifier $clickV -action { 
     write-host $buttonV ; Unregister-Event $clickV 
    } 
    $Button.Add_Click({$objForm.Close()}) 
    $objForm.Controls.Add($Button) 
} 

Button 20 60 90 23 zero clickV0 0 
Button 115 60 90 23 one clickV1 1 
Button 210 60 90 23 two clickV2 2 
Button 305 60 90 23 three clickV3 3 
#> 

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

回答

1

您好我有同樣的問題與我的論壇。這就是我所做的可能是你可以改變我的例子。讓我知道你是否需要我解釋任何事情。

帶有按鈕的論壇工作論壇。

#Generated Form Function 
function GenerateForm { 

#region Import the Assemblies 
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null 
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null 
#endregion 

#region Generated Form Objects 
$MainForum = New-Object System.Windows.Forms.Form 
$GetServices = New-Object System.Windows.Forms.Button 
$richTextBox1 = New-Object System.Windows.Forms.RichTextBox 
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState 
#endregion Generated Form Objects 

#---------------------------------------------- 
#Generated Event Script Blocks 
#---------------------------------------------- 
#Provide Custom Code for events specified in PrimalForms. 


$OnLoadForm_StateCorrection= 
{#Correct the initial state of the form to prevent the .Net maximized form issue 
    $MainForum.WindowState = $InitialFormWindowState 
} 

#---------------------------------------------- 
#region Generated Form Code 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 262 
$System_Drawing_Size.Width = 461 
$MainForum.ClientSize = $System_Drawing_Size 
$MainForum.DataBindings.DefaultDataSourceUpdateMode = 0 
$MainForum.Name = "MainForum" 
$MainForum.Text = "Demo GUI" 


$GetServices.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 13 
$System_Drawing_Point.Y = 26 
$GetServices.Location = $System_Drawing_Point 
$GetServices.Name = "GetServices" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 23 
$System_Drawing_Size.Width = 75 
$GetServices.Size = $System_Drawing_Size 
$GetServices.TabIndex = 1 
$GetServices.Text = "GetServices" 
$GetServices.UseVisualStyleBackColor = $True 
$GetServices.add_Click({ GetServices($GetServices) }) 
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 12 
$System_Drawing_Point.Y = 132 
$richTextBox1.Location = $System_Drawing_Point 
$richTextBox1.Name = "richTextBox1" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 118 
$System_Drawing_Size.Width = 437 
$richTextBox1.Size = $System_Drawing_Size 
$richTextBox1.TabIndex = 0 
$richTextBox1.Text = "Results Out Put To This Box" 

$MainForum.Controls.Add($richTextBox1) 
$MainForum.Controls.Add($GetServices) 
#endregion 
function GetServices($object) 
{ 
OutputText 
} 


function OutputText($object) 
{ 
$service = (Get-Service s*) 
foreach ($_ in $service) 
{ 
$name = $_.Name 
$status = $_.Status 
$richTextBox1.Text = $richTextBox1.Text + $name + " " +$status + "`r" 
} 
} 

#Save the initial state of the form 
$InitialFormWindowState = $MainForum.WindowState 
#Init the OnLoad event to correct the initial state of the form 
$MainForum.add_Load($OnLoadForm_StateCorrection) 
#Show the Form 
$MainForum.ShowDialog()| Out-Null 

} #End Function 
#Call the Function 
GenerateForm