1

我有这个带有“事件处理程序”的功能,但我无法在$event中定义操作部分的值,

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

我有按钮但没有输出,请参见下面的函数参数,

如果我改变我的行:

$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()
4

1 回答 1

2

嗨,我的论坛也有同样的问题。这就是我所做的,也许你可以改变我的例子。如果您需要我解释任何事情,请告诉我。

带有按钮的工作论坛并发布到论坛。

#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
于 2012-07-04T13:17:07.437 回答