Calling Code ?

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 6 years and 3 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

Calling Code ?

Post by mqh77777 »

Product, version and build: 5.4.145
32 or 64 bit version of product: 64
Operating system: Windows 10, Windows 7
32 or 64 bit OS: Both

I have a PowerShell Studio tool that has a field for "computer name" once you enter a computer name you can then press many different buttons on different forms that will execute code against that computer name. I have this code that works as expected but I have to copy/paste it into many many different parts of my overall script as part of any button that runs code against the computer.

if ($PC -eq "" -OR $PC -like " ")
{
$Error1 = "You did not enter a machine in the computer field."
$richtextbox_Output.Clear()
$richtextbox_Output.SelectionColor = 'Red'
$richtextbox_Output.appendtext($Error1)

}


Is there a way to use this code over and over when you want? Can I declare it in some way within PowerShell studio? Or.....can it only work if it is a Function?

I was trying to envision something like this:

$ERRORCODE = "my code from above"

Then I can just call $ERRORCODE when I want and it will execute.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Calling Code ?

Post by davidc »

[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]

A function is probably the best bet. You can also share the same event over multiple button controls. You can use the $this variable to determine what control triggered the event.
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: Calling Code ?

Post by jvierra »

Use the "Validating" event to enforce the rules for the input control. All controls can be ted tot the same event. When you define a button event use the "events" tab of the properties panel and just select an existing event.
This topic is 6 years and 3 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