Is it possible to open second 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 6 years and 1 month 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
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Is it possible to open second form?

Post by SvanGool »

Hello,

I've got an application which has multiple forms under the mainForm (on the mainform the user clicks a button).
I've got one form which needs to be opend (screen with output of many jobs) and the user can open a second (diverent form, where he can put data in from the first form) from the mainForm.
Is this even possible or is this by design not possible.

Thanks,

Sander
User avatar
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Re: Is it possible to open second form?

Post by SvanGool »

Found it on an other post
  1. $f = get-item Function:\Show-TestForm_psf
  2. $ps = [powershell]::Create()
  3. $ps.AddScript($f.Definition)
  4. $ps.BeginInvoke()
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Is it possible to open second form?

Post by mxtrinidad »

If you're using PowerShell Studio, trying to open and work with two forms at same time then No!
It's possible to accomplish this on a high-level programming language such like C#, or even C++.

Your best resolution would be, on the main-form to either have button to display a form with the output jobs, or creating dashboard for the output job. Then, you can have the other button to open the form to enter information.

By the way, If you use the Form Wizard in PowerShell Studio, you'll have the ability to create Multi-form projects.

Also, although you could experiment with the code below, PowerShell Studio will provide with a better programming code & template.

:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Is it possible to open second form?

Post by jvierra »

Runspaces work just fine in PSS multi-form projects. The code sample I posted will launch and form that is part of the project since the forms are stored as functions. I extracted the code from the function and used it as the script for the PowerShell object.

PSS works well like this but there is no specific support for runspaces and forms. Not all things available in a multi-form project will be available with the runspace forms.

Runspaces can be used for many things but require some decent understanding of programming in a multi-threaded environment.

The reason this works is because a runspace is part of the current PowerShell session and the form has its own "Windows Message Loop". It is still a modal dialog but, because of the separate message loop it does not block the calling from

https://learn-powershell.net/2016/02/14 ... l-runspace
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Is it possible to open second form?

Post by jvierra »

Here is a working example. I didn't post it before because I had to clean it up.

If we add a "synchash" then we can pass data between forms dynamically.
Attachments
Test-ChildPosition (3).zip
(142.34 KiB) Downloaded 181 times
User avatar
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Re: Is it possible to open second form?

Post by SvanGool »

I tried the code:
  1.  $f = get-item Function:\Show-ChildForm_psf
  2.     $ps = [powershell]::Create()
  3.     $ps.AddScript($f.Definition)
  4.     $ps.BeginInvoke()
in my project. It opens the second form and I can still access the mainform. But the second form is empty in my project.
Its a form which shows output (through tabs and webbrowser) from nightly jobs.
When I do a normal call (like the muliple-form project template) it is filled.
See the attachements.
Attachments
empty.PNG
empty.PNG (7.79 KiB) Viewed 4375 times
filled.PNG
filled.PNG (20.96 KiB) Viewed 4375 times
User avatar
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Re: Is it possible to open second form?

Post by SvanGool »

Just added a webbrowser to you test-childpostion project on the second form.
And this is not showing either, so could this be a webbrowser issue?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Is it possible to open second form?

Post by jvierra »

Al variables used in the second form have to be defined in the form. You cannot share variables between the two forms. If you want to share variables you would have to add a synchash containing the shared variables.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Is it possible to open second form?

Post by jvierra »

Note also that the runspace is default and may need to be adjusted for some controls.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Is it possible to open second form?

Post by jvierra »

To run many controls in a runspace we must take on building our own custom runspace. The following will work better with most controls including the WebBrowser control.

Code: Select all

    $f = get-item Function:\Show-ChildForm_psf
    $rs = [runspacefactory]::CreateRunspace()
    $rs.ApartmentState = 'STA'
    $ps = [powershell]::Create()
    $ps.Runspace = $rs
    $rs.Open()
    $ps.AddScript($f.Definition)
    $ps.BeginInvoke()
Not that explicitly setting "STA" allows the browser to work as expected.
This topic is 6 years and 1 month 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