Page 1 of 1

Force Message Box focus

PostPosted: Mon Jan 02, 2017 3:11 pm
by ger21
Quite often, when Using a MessageBox in a macro, it can lose focus and end up behind the UCCNC screen. When this happens, the user gets the impression that UCCNC is frozen, if he doesn't know what's happening.
I'm calling some macros from the Jog screen, and if they call a MessageBox, moving the mouse off of the JogScreen automatically sends the MessageBox behind UCCNC.
Is it possible to force Messageboxes to keep focus, or always be on top? Not sure if there are any downsides to this?

Re: Force Message Box focus

PostPosted: Mon Jan 02, 2017 4:45 pm
by cncdrive
Hi Gerry,

One way is to make the main window the parent of the MessageBox, so the MessageBox will appear above the main form, like this:

MessageBox.Show(exec.mainform, "text", "title");

However if there is a child window which is already above the mainform then it may still appear below that,
so the other way is to create a new Form and make it TopMost and TopLevel and make the MessageBox it's child, like this:

System.Windows.Forms.Form dummy = new System.Windows.Forms.Form();
dummy.TopMost = true;
dummy.TopLevel = true;
MessageBox.Show(dummy, "text", "title");
dummy.Dispose();

This way the Messagebox will always appear on the top.

Re: Force Message Box focus

PostPosted: Mon Jan 02, 2017 5:29 pm
by ger21
That works, but there's a problem.
If the following runs twice, the Messagebox doesn't appear the second time. It's almost like the macro is hanging somewhere.
When this happens, the jog screen and MDI no longer works, and UCCNC will not close, even after it asks if you want to close.


Code: Select all
System.Windows.Forms.Form topform = new System.Windows.Forms.Form();
topform.TopMost = true;
topform.TopLevel = true;

MessageBox.Show (topform, "Testing", "Test Box", MessageBoxButtons.OK, MessageBoxIcon.Error);
topform.Dispose();

Re: Force Message Box focus

PostPosted: Mon Jan 02, 2017 6:18 pm
by ger21
cncdrive wrote:One way is to make the main window the parent of the MessageBox, so the MessageBox will appear above the main form, like this:
MessageBox.Show(exec.mainform, "text", "title");


This method will work fine for me, but the other method is definitely broken.
Also be aware that with the other method, you can still run g-code while the MDI and jog screen are frozen.

Re: Force Message Box focus

PostPosted: Mon Jan 02, 2017 8:18 pm
by cncdrive
Yes, my bad, it was a long first day of the year for me and I did not think about that the macro is running in it's own thread, so yes, that method will not work as I typed it.
It does not work, because the macro is seated not in the main thread with it's GUI Window message loop, so showing it from the background worker thread of the macro will really not work and can randomly freezy the GUI, because it makes an illegal cross-thread call.

It could be made work with delegating and invoking on the mainform's thread with placing the delegate function in the #events, but then the whole thing will be kind of overcomplicated,
so I think it is then better to use the other approach with makes the mainform the parent, that is a single line of code...