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.
Locked
User avatar
mqh777
Posts: 229
Joined: Tue Feb 25, 2014 1:01 pm

how to stop code but keep form open?

Post by mqh777 » Tue Oct 09, 2018 10:32 am

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.Clear()
		$richtextbox_Output.SelectionColor = 'Red'
		$richtextbox_Output.appendtext($Error1)
		EXIT		
	}

}

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

Re: how to stop code but keep form open?

Post by davidc » Tue Oct 09, 2018 2:56 pm

[TOPIC MOVED TO THE POWERSHELL GUI FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: how to stop code but keep form open?

Post by jvierra » Tue Oct 09, 2018 4:00 pm

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

Locked