I am implementing a GUI for Remote Access Control. I have my textboxes with labels and now I want to add some KeyEvent into to the textboxes, so that that the user Input can be validated. For testing purposes I want to know how can I add some KeyEvents for example printing "HelloWorld" in a MessageBox. I tried with KeyEventHandler, KeyCode and KeyPressEventHandler with no success. Can someone please help. Here is my sample Code:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Function MakenewForm {
$form.Close()
$form.Dispose()
MakeForm
}
Function MakeForm {
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Fernwartungssoftware '
$form.Size = New-Object System.Drawing.Size(1600,800)
$form.StartPosition = 'CenterScreen'
$itext = New-Object System.Windows.Forms.Label
$itext.Location = New-Object System.Drawing.Point(4,15)
$itext.Size = New-Object System.Drawing.Size(500,42)
$itext.Font = New-Object System.Drawing.Font($itext.Font.Name, 28);
$itext.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 28, [System.Drawing.FontStyle]::Bold);
$itext.Text = 'Fernwartung'
$form.Controls.Add($itext)
#sbutn
$sbutn = New-Object System.Windows.Forms.Button
$sbutn.Location = New-Object System.Drawing.Point(09,180)
$sbutn.Size = New-Object System.Drawing.Size(110,30)
$sbutn.Text = 'Send'
$form.AcceptButton = $sbutn
$form.Controls.Add($sbutn)
#Headline
$lbl = New-Object System.Windows.Forms.Label
$lbl.Location = New-Object System.Drawing.Point(08,73)
$lbl.Size = New-Object System.Drawing.Size(390,28)
$lbl.Text = 'Bitte ausfüllen:'
$lbl.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 13, [System.Drawing.FontStyle]::Regular);
$form.Controls.Add($lbl)
#Problem
$pr = New-Object System.Windows.Forms.Label
$pr.Location = New-Object System.Drawing.Point(08,105)
$pr.Size = New-Object System.Drawing.Size(90,20)
$pr.Text = 'Problem:'
#$label.Font = New-Object System.Drawing.Font($pr.Font.Name, 13);
$pr.Font = New-Object System.Drawing.Font($pr.Font.Name, 9);
$form.Controls.Add($pr)
#Textbox
$txtb = New-Object System.Windows.Forms.TextBox
$txtb.Location = New-Object System.Drawing.Point(105,105)
$txtb.Size = New-Object System.Drawing.Size(320,20)
#$form.Add_Shown({$form.Activate(); $txtb.focus()})
$form.Controls.Add($txtb)
#$textBox.Add_KeyDown({
#if ($_.KeyCode -eq "Enter") {
#logic
# $textBox.Text | Out-Host
# }
#})
$form.ShowDialog()
}
MakeForm