1

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
4
  • What do you mean validated? What are you trying to validate it for? Do you want them to only type character keys, only numbers or both? Or when you hit "Send" it checks that the text is of a certain format? Commented Jul 24, 2018 at 8:21
  • For example if an input is given, it should be a mandatory field. Imagine if I have two or more textboxes. Commented Jul 24, 2018 at 8:24
  • And when did you want it validated? On any key down? Commented Jul 24, 2018 at 8:25
  • After the Send Button is clicked or after the Input of some textboxes by hiting Input and than Enter. Do I Need a Function for that purposes? Commented Jul 24, 2018 at 8:25

1 Answer 1

2

Knocked something up quickly in PrimalForms to make your GUI code look not terrible. Highly recommend if you want to smash out a few GUIs quickly and consistently. The features you are looking for in a text box are $txtb.add_KeyDown($handler_txtb_KeyDown) and then call the $_.KeyCode to check if it is Enter

Full working code for you to mess around with here.

#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 Form Objects
$form = New-Object System.Windows.Forms.Form
$sbutn = New-Object System.Windows.Forms.Button
$pr = New-Object System.Windows.Forms.Label
$txtb = New-Object System.Windows.Forms.TextBox
$lbl = New-Object System.Windows.Forms.Label
$itext = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$InvalidInput = "Invalid Input, try again"
#endregion Form Objects

#Provide Code for events
$handler_sbutn_Click= 
{
    If(($txtb.Text -eq '') -or ($txtb.Text -eq $InvalidInput)){
        $txtb.Text = $InvalidInput
    } Else {
        $txtb.Text = "Congratulations, you didn't break it"
    }

}

$handler_txtb_KeyDown= 
{
if ($_.KeyCode -eq 'Enter'){
    &$handler_sbutn_Click
}

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 265
$System_Drawing_Size.Width = 495
$form.ClientSize = $System_Drawing_Size
$form.DataBindings.DefaultDataSourceUpdateMode = 0
$form.Name = "form"
$form.Text = "Fernwartungssoftware"


$sbutn.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 190
$sbutn.Location = $System_Drawing_Point
$sbutn.Name = "sbutn"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 46
$System_Drawing_Size.Width = 125
$sbutn.Size = $System_Drawing_Size
$sbutn.TabIndex = 4
$sbutn.Text = "Send"
$sbutn.UseVisualStyleBackColor = $True
$sbutn.add_Click($handler_sbutn_Click)

$form.Controls.Add($sbutn)

$pr.AutoSize = $True
$pr.DataBindings.DefaultDataSourceUpdateMode = 0
$pr.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9,0,3,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 117
$pr.Location = $System_Drawing_Point
$pr.Name = "pr"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 15
$System_Drawing_Size.Width = 57
$pr.Size = $System_Drawing_Size
$pr.TabIndex = 3
$pr.Text = "Problem:"

$form.Controls.Add($pr)

$txtb.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 75
$System_Drawing_Point.Y = 117
$txtb.Location = $System_Drawing_Point
$txtb.Name = "txtb"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 381
$txtb.Size = $System_Drawing_Size
$txtb.TabIndex = 2
$txtb.add_KeyDown($handler_txtb_KeyDown)

$form.Controls.Add($txtb)

$lbl.AutoSize = $True
$lbl.DataBindings.DefaultDataSourceUpdateMode = 0
$lbl.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12.75,1,3,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 73
$lbl.Location = $System_Drawing_Point
$lbl.Name = "lbl"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 137
$lbl.Size = $System_Drawing_Size
$lbl.TabIndex = 1
$lbl.Text = "Bitte ausfüllen:"

$form.Controls.Add($lbl)

$itext.AutoSize = $True
$itext.DataBindings.DefaultDataSourceUpdateMode = 0
$itext.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",27.75,1,3,0)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$itext.Location = $System_Drawing_Point
$itext.Name = "itext"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 42
$System_Drawing_Size.Width = 239
$itext.Size = $System_Drawing_Size
$itext.TabIndex = 0
$itext.Text = "Fernwartung"

$form.Controls.Add($itext)

#endregion Form Code

#Save the initial state of the form
$InitialFormWindowState = $form.WindowState
#Init the OnLoad event to correct the initial state of the form
$form.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you sir, that was exactly what I was looking for.
do you can explain what DataBindings.DefaultDataSourceUpdateMode do and why I need it?
It pretty much binds your properties and their values together, allowing you to define a form in a database and auto generate different datasets when needed. For your super simple application, you wont need it. But it is a great thing to have when the GUI is generated.

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.