Call a method from a different form

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 7 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
wgknowles
Posts: 13
Last visit: Sat Apr 08, 2017 10:37 pm

Call a method from a different form

Post by wgknowles »

I feel like I'm going about this the wrong way, so feel free to suggest a better approach...

I have a method on one form that I'd like to be able to call from a different form.
I know about $MethodName_EventType.Invoke() but that seems to be limited to a single form?
This approach probably has all sorts of variable scope issues...

Maybe what I really should do is create this as a function in another file and include it with command extension = true...
Thoughts?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Call a method from a different form

Post by jvierra »

Functions in another form cannot be reached as the form is not available until it is displayed.

Use a project and add the functions to the "globals" file . Loading functions into a form from a file will only make the functions available in that form due to scoping restrictions.

You can also place functions in a module and load it as a referenced module from the module path.
User avatar
wgknowles
Posts: 13
Last visit: Sat Apr 08, 2017 10:37 pm

Re: Call a method from a different form

Post by wgknowles »

Apologies in advance...
Can you clarify for me the difference b/t globals.ps1 and "startup.pss" which are both created with a new project?
When is it appropriate to put things in one vs the other?
What order are they executed in?

Did I miss a key bit of documentation re: how the SAPIEN Powershell Studio framework is organized?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Call a method from a different form

Post by jvierra »

Two suggestions will help with this:

1. Read the text in the two provided files.
2. Read the "Project" section of the product manual. It will give you detailed info on how t use a project.
This topic is 7 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