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.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

how to stop code but keep form open?

Post by mqh77777 »

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: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: how to stop code but keep form open?

Post by davidc »

[TOPIC MOVED TO THE POWERSHELL GUI FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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".
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked