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
Is it possible to open second form?
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.
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.
Re: Is it possible to open second form?
Found it on an other post
- $f = get-item Function:\Show-TestForm_psf
- $ps = [powershell]::Create()
- $ps.AddScript($f.Definition)
- $ps.BeginInvoke()
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Is it possible to open second form?
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.
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.
Re: Is it possible to open second form?
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
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
Re: Is it possible to open second form?
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.
If we add a "synchash" then we can pass data between forms dynamically.
- Attachments
-
- Test-ChildPosition (3).zip
- (142.34 KiB) Downloaded 181 times
Re: Is it possible to open second form?
I tried the code:
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.
- $f = get-item Function:\Show-ChildForm_psf
- $ps = [powershell]::Create()
- $ps.AddScript($f.Definition)
- $ps.BeginInvoke()
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 (7.79 KiB) Viewed 4377 times
-
- filled.PNG (20.96 KiB) Viewed 4377 times
Re: Is it possible to open second form?
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?
And this is not showing either, so could this be a webbrowser issue?
Re: Is it possible to open second form?
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.
Re: Is it possible to open second form?
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.
Not that explicitly setting "STA" allows the browser to work as expected.
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()