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.
User avatar
SvanGool
Posts: 33
Joined: Wed Jan 17, 2018 5:41 am

Is it possible to open second form?

Post by SvanGool » Mon Mar 12, 2018 7:38 am

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: 33
Joined: Wed Jan 17, 2018 5:41 am

Re: Is it possible to open second form?

Post by SvanGool » Mon Mar 12, 2018 9:09 am

Found it on an other post
  1. $f = get-item Function:\Show-TestForm_psf
  2.  
  3. $ps = [powershell]::Create()
  4.  
  5. $ps.AddScript($f.Definition)
  6.  
  7. $ps.BeginInvoke()

User avatar
mxtrinidad
Site Admin
Posts: 199
Joined: Sun Mar 03, 2013 12:42 pm

Re: Is it possible to open second form?

Post by mxtrinidad » Mon Mar 12, 2018 3:56 pm

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.

:)

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Is it possible to open second form?

Post by jvierra » Mon Mar 12, 2018 4:31 pm

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

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Is it possible to open second form?

Post by jvierra » Mon Mar 12, 2018 4:50 pm

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 21 times

User avatar
SvanGool
Posts: 33
Joined: Wed Jan 17, 2018 5:41 am

Re: Is it possible to open second form?

Post by SvanGool » Mon Mar 12, 2018 11:01 pm

I tried the code:
  1.  $f = get-item Function:\Show-ChildForm_psf
  2.  
  3.     $ps = [powershell]::Create()
  4.  
  5.     $ps.AddScript($f.Definition)
  6.  
  7.     $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 566 times
filled.PNG
filled.PNG (20.96 KiB) Viewed 566 times

User avatar
SvanGool
Posts: 33
Joined: Wed Jan 17, 2018 5:41 am

Re: Is it possible to open second form?

Post by SvanGool » Mon Mar 12, 2018 11:29 pm

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?

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Is it possible to open second form?

Post by jvierra » Tue Mar 13, 2018 8:38 am

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.

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Is it possible to open second form?

Post by jvierra » Tue Mar 13, 2018 8:47 am

Note also that the runspace is default and may need to be adjusted for some controls.

User avatar
jvierra
Posts: 12663
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Is it possible to open second form?

Post by jvierra » Tue Mar 13, 2018 9:27 am

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.

Locked