0

I created a Windows form in Powershell which I am then using PS2EXE-GUI to make an executable...I am struggling with this part, as it relies a LOT on user input to be correct:

$form = New-Object System.Windows.Forms.Form 
$form.Text = "'Enter ID number for the Machine"
$form.Size = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = "CenterScreen"

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox)

And I would like to constrain what data can be input (must be 8 character length, start with specific characters, etc.). Is this a possibility?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260

2 Answers2

0

You can set $textBox.MaxLength to limit a text box to a certain number of characters. You can also use the events tied to the text box such as KeyUp to create logic to check for the start of entries and what is contained and inform the user at that point what is wrong with their entry.

Jason Snell
  • 1,425
  • 12
  • 22
0

Add an event handler for the TextChanged event on the TextBox and evaluate the input whenever it changes:

$textBox.add_TextChanged({
    param($sender)

    if($sender.Text -match '^\d{6}\D{2}$'){
        # Input looks good, allow submission
        $submitButton.Enabled = $true
    }
    else {
        $submitButton.Enabled = $false
    }
})

In the above, we test that the input string consists of 6 digits and then 2 non-digits, but you can of course do any kind of testing you want in the event handler block

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206