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 に答える