Gui.cs: MessageBox not closing in PowerShell

Created on 28 Aug 2020  路  13Comments  路  Source: migueldeicaza/gui.cs

First time user. I have a simple class created to display a Message box.

public class Msg : IDisposable
    {
        public Msg(string Message)
        {
            Application.Init();
            var msg = MessageBox.Query(20, 10, "Test", Message, "Yes", "No");
        }

        public void Dispose()
        {
            Console.Write("\u001b[?1h");
        }
    }

When I use the class from within PowerShell, the MessageBox opens but does not disappear once a option is chosen. What am I missing?

Most helpful comment

Since this is a gui.cs<->Powershell issue/discussion....

I spiked a possible powershell integration for gui.cs at https://github.com/wgross/gui.ps1.
I did this out of curiosity and don't have a real-world application currently to drive further implementation but maybe it can serve as a stimulus or starting point for other people?

All 13 comments

How you are calling the Msg class? You don't need to use Application.Init() because MessageBoxclass already open a Dialog. You also need to return the right response from it which is the button used from 0 to number of used buttons and the constructor isn't an ideal place to get it.

This is not meant to be useful yet, just a test 馃槉

This is how I call it:

PS> import-module .\fwTextGUI.dll
PS> [fwTextGUI.Msg]::new("Test Message")

Alternately, you can call it directly with the same result:

PS > import-module .\Terminal.Gui.dll
PS > import-module .\NStack.dll
PS > [Terminal.Gui.Application]::Init()
PS > [Terminal.Gui.MessageBox]::Query(20, 10, "Test", "Hello World", "Yes", "No")

I don't know what you have in fwTextGUI.dll module. Try putting Application.RequestStop (); after the call to the var msg = MessageBox.Query (20, 10, "Test", message, "Yes", "No");

Since you call Application.Init Terminal.Gui has control over it and you have to call Application.RequestStop to return to the PowerShellconsole. So you must control it on your Msg class or on your fwTextGUI class. Without seeing the code I can't provide more information.

A little sample:

static class Program {
    public static void Main ()
    {
        Application.Init ();
        var msg = new Msg ("Test Message!");
        msg.Response = (e) => {
            if (e == 0) {
                Console.Write ("Yes");
            } else {
                Console.Write ("No");
            }
            // Exit
            Application.RequestStop ();
        };
        Application.Top.Ready = () => msg.DisplayMsg ();

        Application.Run ();
        msg.Dispose ();
    }
}
public class Msg : IDisposable {
    string message;
    public Action<int> Response;

    public Msg (string message)
    {
        this.message = message;
    }
    public void DisplayMsg ()
    {
        var msg = MessageBox.Query (20, 10, "Test", message, "Yes", "No");
        Response?.Invoke (msg);
    }

    public void Dispose ()
    {
        Console.Write ("\u001b[?1h");
    }
}

Yes, I've gotten it to work with a console application, but it won't work from within PowerShell. End goal is to write a PowerShell CmdLet that uses the Terminal.Gui API. Calling the MessageBox class directly from PowerShell using the alternative method outlined above fails (as well as using the simple single class DLL I had in my original post).

@domhnal you may want to look at our ConsoleGuiTools project which exposes an Out-ConsoleGridView:
https://github.com/PowerShell/GraphicalTools/tree/master/src/Microsoft.PowerShell.ConsoleGuiTools

Yes, I was looking at that yesterday, and that is ultimately where I'm headed, but my first example program left me scratching my head. I've done some debugging and it's not that the application hangs, output will continue to be printed to the screen, it's just that any new output is obscured by the modal message box. I can type 'exit' and the PowerShell window will close, you will just never see anything but the Message Box up until the window closes, at which point you get a very brief glimpse of what was printed to the display.

@domhnal try this please:

public class Msg : IDisposable {
    public Msg (string Message)
    {
        Application.Init ();
        var msg = MessageBox.Query (20, 10, "Test", Message, "Yes", "No");
        Application.Shutdown ();
    }

    public void Dispose ()
    {
        Console.Write ("\u001b[?1h");
    }
}

OK, That worked. Mouse support doesn't work in the new windows terminal app, but works fine in the standard powershell 7 console. Thank you for your help.

I still don't understand how that gap in mouse functionality still persists in the new Windows Terminal.

Since this is a gui.cs<->Powershell issue/discussion....

I spiked a possible powershell integration for gui.cs at https://github.com/wgross/gui.ps1.
I did this out of curiosity and don't have a real-world application currently to drive further implementation but maybe it can serve as a stimulus or starting point for other people?

@wgross I only managed to get it to work by changing the netstandardto version 2.0. I think that is because the Terminal.Gui is configured to run that version. See my PR at https://github.com/wgross/gui.ps1/pull/1

I still don't understand how that gap in mouse functionality still persists in the new Windows Terminal.

Me either: I guess they are working on it. This is tracked here: https://github.com/migueldeicaza/gui.cs/issues/332

Was this page helpful?
0 / 5 - 0 ratings