Force Message Box focus

Here is where you can request new features or special features.

Force Message Box focus

Postby ger21 » Mon Jan 02, 2017 3:11 pm

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?
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Force Message Box focus

Postby cncdrive » Mon Jan 02, 2017 4:45 pm

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.
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm

Re: Force Message Box focus

Postby ger21 » Mon Jan 02, 2017 5:29 pm

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();
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Force Message Box focus

Postby ger21 » Mon Jan 02, 2017 6:18 pm

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.
Gerry
UCCNC 2022 Screenset - http://www.thecncwoodworker.com/2022.html
ger21
 
Posts: 2714
Joined: Sat Sep 03, 2016 2:17 am

Re: Force Message Box focus

Postby cncdrive » Mon Jan 02, 2017 8:18 pm

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...
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm


Return to Feature Request

Who is online

Users browsing this forum: No registered users and 8 guests