Page 1 of 1

how to stop code but keep form open?

Posted: Tue Oct 09, 2018 10:32 am
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		
	}

}

Re: how to stop code but keep form open?

Posted: Tue Oct 09, 2018 2:56 pm
by davidc
[TOPIC MOVED TO THE POWERSHELL GUI FORUM BY MODERATOR]

Re: how to stop code but keep form open?

Posted: Tue Oct 09, 2018 4:00 pm
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".