Minimize Form auto closes Child

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 4 years and 2 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
daniel kusnir
Posts: 75
Last visit: Tue May 23, 2023 10:50 am
Answers: 1

Minimize Form auto closes Child

Post by daniel kusnir »

hello

i have stock Multi project form. What i want is to auto - minimize the main form when i minimize child.

but when i do that, child is auto closed. Is there any way to achieve that ?

Problem is that when child is minimized, main form is still shown and its frozen - cannot be used until child is closed.
So at least i want it out of screen
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Minimize Form auto closes Child

Post by jvierra »

Yes. That is a limitation of modal dialogs. Both the main form and the child are modal dialogs. If a form and child are shown and they have a parent child relation then minimizing the main form will minimize the child but minimizing the child will never minimize the parent.

This is by design. The only way to avoid this is to use runspaces or the forms or to create a true main form which cannot easily be done in PowerShell. It is also extremely hard to do in PowerShell Studio as PSS does not provide any mechanism for creating forms that can be launched with the application run command. Even if it could the PowerShell single threaded environment can cause issues with forms run in this way.
User avatar
daniel kusnir
Posts: 75
Last visit: Tue May 23, 2023 10:50 am
Answers: 1

Re: Minimize Form auto closes Child

Post by daniel kusnir »

thanks,

i didnt know that - i am wiser now :)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Minimize Form auto closes Child

Post by jvierra »

Here is an example of how to accomplish you goal. It works and can be used but requires some careful thought to extend.
Attachments
ChildTest.zip
(110.92 KiB) Downloaded 124 times
User avatar
daniel kusnir
Posts: 75
Last visit: Tue May 23, 2023 10:50 am
Answers: 1

Re: Minimize Form auto closes Child

Post by daniel kusnir »

Wow, thanks a lot

so you basically figured it out. And i can now also work with both windows concurrently. You are genius

And if you add this code, you can minimize form without closing the child - simply amazing
  1. $formChildForm_SizeChanged={
  2.  
  3.     if ($this.WindowState -eq 'Minimized'){ $parent.WindowState = 'Minimized' }
  4.     elseif ($this.WindowState -eq 'Normal') { $parent.WindowState = 'Normal' }
  5. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Minimize Form auto closes Child

Post by jvierra »

No, I knew this and just created a quick demo. It iwll present many new challenges when used like a normal form. First it has no access to the environment of the parent for. That means it cannot see or use any variables that the parent can see.
User avatar
daniel kusnir
Posts: 75
Last visit: Tue May 23, 2023 10:50 am
Answers: 1

Re: Minimize Form auto closes Child

Post by daniel kusnir »

i see.

what about the global properties ? i see e.g OpenFileDialog() is not working just by simply adding it to the child. Is that part of the challenge ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Minimize Form auto closes Child

Post by jvierra »

There ae no shared global properties.

To use a runspace it is necessary to understand how to work with runspaces. This can be a challenge even for trained programmers. It is much better to design solutions that do not require advanced programming skills and that allow you to leverage PowerShell Studio and forms without pain.
\Most of my experience with those new to PowerShell and WinForms is that they have no training in WinForms and how to design a form. WinForms was designed to solve specific problems in certain ways which can make it very challenging to do things that were not part of the original design intent of MS.

The attached file shows how to get the file dialogs to work in a runspace.
Attachments
ChildTest_1.zip
(115.14 KiB) Downloaded 125 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Minimize Form auto closes Child

Post by jvierra »

Here is an example of one way to use arguments and to manage a child form in a separate runspace. It also adds exception handling and a return value.

Code: Select all

$buttonCallChildForm_Click={
    Try{
    
        $ps = [powershell]::Create()
        $ps.Runspace = [runspacefactory]::CreateRunspace()
        $ps.Runspace.ApartmentState = 'sta'
        $ps.Runspace.Open()  
        $t = Get-Item function:'Show-ChildForm_psf'
        $ps.AddScript($t.Definition)
        $startPosition = [System.Drawing.Point]::New($this.Parent.Right,$this.Parent.Top)    
        $ps.AddArgument($MainForm)
        $ps.AddArgument($startPosition)

        $results = $ps.Invoke()
        Write-Host $results[0]
    }
    Catch{
        Throw $_
    }
    Finally{
        $ps.Runspace.Dispose()
        $ps.Dispose()
    }
}
User avatar
daniel kusnir
Posts: 75
Last visit: Tue May 23, 2023 10:50 am
Answers: 1

Re: Minimize Form auto closes Child

Post by daniel kusnir »

thanks a lot, i will take a look at that
This topic is 4 years and 2 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