Question on event function displaying data

Hello

I am not able to write in console from event attached function.
is this normal behavior?
Thanks

Your question doesn’t make much sense, how about posting code you’re working on

Thanks

I apologize, below a script, in the keyDown event I try to show something that not appears in the console.

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

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown(
	{if ($_.KeyCode -eq "Enter") {
		"Enter pressed " + $TBox.Text
		$objForm.Close()}
	}
)

$TBox = New-Object System.Windows.Forms.TextBox 
$TBox.Location = New-Object System.Drawing.Size(10,40) 
$TBox.Size = New-Object System.Drawing.Size(260,20)
$TBox.text = "Hit enter key"
$objForm.Controls.Add($TBox) 

$objForm.Topmost = $True

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

You’re not assigning anything to the Text property.

This:

"Enter pressed " + $TBox.Text

Should be:

$TBox.Text = "Enter pressed "

If you want to keep the text in the field, then use this:

$TBox.Text = "Enter pressed " + $TBox.Text

Also, your script closes the form when the key is pressed so you won’t see the text field update :slight_smile:

Hi matt
I simply woud know why a statement like "Enter pressed " + $TBox.Text shows Enter pressed Hit enter key when in the main program part and don’t show anithing when in event function.
( I use statemts like the above for debug purpose).

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

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown(
	{if ($_.KeyCode -eq "Enter") {
		"Enter pressed " + $TBox.Text
		$objForm.Close()}
	}
)

$TBox = New-Object System.Windows.Forms.TextBox 
$TBox.Location = New-Object System.Drawing.Size(10,40) 
$TBox.Size = New-Object System.Drawing.Size(260,20)
$TBox.text = "Hit enter key"
$objForm.Controls.Add($TBox) 

$objForm.Topmost = $True
"Enter pressed " + $TBox.Text
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Consol shows:

PS C:\Sviluppo\PowerShell> .\event
Enter pressed Hit enter key
PS C:\Sviluppo\PowerShell>

Sorry, I misunderstood, I thought you wanted to update the text box.

To write to the console you should use [Console]::WriteLine():

[Console]::WriteLine("Enter pressed " + $TBox.Text)

Thanks matt

Giovanni

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.