winformspowershellps2exe-gui

Possible to constrain form data powershell?


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?


Solution

  • 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.