how to stop code but keep form open?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 237
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Tue Feb 25, 2014 1:01 pm

how to stop code but keep form open?

Post by mqh777 »

PowerShell Studio 2018 v5.5.153
OS is windows 10 x64

We have a form that has a pcname text box. you can enter a machine name and then press any number of buttons to get and do stuff. But if the tech did not enter a name in the pcname text but I want a message to display in the richtextbox. I wrote this function which works with one issue. it throws up an error "Unhandled exception has occurred in a component in your application." But if I use RETURN or END the $error1 message is displayed but it still executes the rest of the code under the "button click" and displays that in the richtextbox and we don't want that. If the PCNameBox is emply I want the code to stop but I want the form to stay open.

Code: Select all

Function PC-FieldEmpty
	if ($PCNameBox.TextLength -eq 0 -OR $PCNameBox.TextLength -Like " ")
		$Error1 = "You did not specify a computer name!"
		$richtextbox_Output.SelectionColor = 'Red'


User avatar
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: how to stop code but keep form open?

Post by davidc »

SAPIEN Technologies, Inc.

Posts: 14437
Joined: Tue May 22, 2007 9:57 am

Re: how to stop code but keep form open?

Post by jvierra »

Please explain what you men by "stop code"?

Windows forms are event driven. When an event exits the code is stopped (no longer being executed) the form will not execute code until another event is received.

If you are in a loop that you want to end just exit the loop with a "break" statement.

To be sure a value has been entered for a pc name use the "validating" and "validated" events. Disable all buttons when the textbox is blank and enable all when it is "validated".