1

I have this function with "event handler" but I am unable in $event to define value in action section,

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

I have the button but no output, see below with the function param,

if I change my line with:

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

I have the button and the output

Any help will be welcome,


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 Answer 1

2

Hi I had the same problem with my forum. This is what I did may be you could just change my example. Let me know if you need me to explain anything.

Working forum with a button and out putting to the forum.

#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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.