Author Topic: ShowModelessDialog + ModalDialog = focus problems  (Read 5339 times)

0 Members and 1 Guest are viewing this topic.

simor

  • Guest
ShowModelessDialog + ModalDialog = focus problems
« on: February 02, 2009, 08:03:03 AM »
"When I use ShowModelessDialog() to display MyForm, and MyForm in turn displays a modal dialog (eg. MessageBox.Show...), then, after closing the MessageBox and when MyForm closes, AutoCAD loses focus and whatever other application running at that time becomes the foreground application."

I managed to find a thread about this with a solution to use Hide in the closing event. Apparently it works, haven't tried because I don't understand why does it have to be done like that.. I mean, if we keep loading many different forms and they all get hidden instead of being closed properly, won't that slow acad down and make it use more resources?  :oops:

http://discussion.autodesk.com/forums/thread.jspa?messageID=5755989&#5755989

TonyT

  • Guest
Re: ShowModelessDialog + ModalDialog = focus problems
« Reply #1 on: February 02, 2009, 05:34:01 PM »
When you show a .NET form modelessly, the form will dispose itself
when it is closed, which means you can't show it again.

Since a form may have state that you may wish to retain for the
life of the AutoCAD session, you can do that by not allowing the
form to dispose itself when it's closed, so that each time you need
to show it, you can use that same instance with its state intact.

To prevent a modeless form from disposing itself when the user
closes it, override the OnFormClosing() method, and in the override,
set the Cancel property of the event args to true, and then set the
form's Visible property to false.

Or, you can derive your Form from this specialization, rather
than directly from the System.Windows.Forms.Form class:

Code: [Select]

   public class AcadModelessForm : Form
   {
      protected override void OnFormClosing( FormClosingEventArgs e )
      {
         base.OnFormClosing( e );
         if( !e.Cancel )
         {
            e.Cancel = true;
            this.Visible = false;
         }
      }

      public void ShowModeless
      {
          Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(this);
      }

   }


 


"When I use ShowModelessDialog() to display MyForm, and MyForm in turn displays a modal dialog (eg. MessageBox.Show...), then, after closing the MessageBox and when MyForm closes, AutoCAD loses focus and whatever other application running at that time becomes the foreground application."

I managed to find a thread about this with a solution to use Hide in the closing event. Apparently it works, haven't tried because I don't understand why does it have to be done like that.. I mean, if we keep loading many different forms and they all get hidden instead of being closed properly, won't that slow acad down and make it use more resources?  :oops:

http://discussion.autodesk.com/forums/thread.jspa?messageID=5755989&#5755989

simor

  • Guest
Re: ShowModelessDialog + ModalDialog = focus problems
« Reply #2 on: February 02, 2009, 06:46:43 PM »
Yes, you are giving me the same solution you gave in the mentioned thread and I'm here asking if this is the only way (to get rid of the focus problem) and isn't this affecting autocad's speed when a users starts opening forms which instead of being properly closed when their time is over they hung around being invisible. Also, I want the user to be given a fresh form, without any changes he might have done last time he used it so your way is just adding extra work...

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8741
  • AKA Daniel
Re: ShowModelessDialog + ModalDialog = focus problems
« Reply #3 on: February 02, 2009, 08:10:46 PM »
Maybe you can p/invoke SetFocus in the event

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8741
  • AKA Daniel
Re: ShowModelessDialog + ModalDialog = focus problems
« Reply #4 on: February 02, 2009, 08:16:37 PM »
untested example

Code: [Select]

  public class AcadModelessForm : Form
   {
    [DllImport("user32.dll")]
     static extern IntPtr SetFocus(IntPtr hWnd);

     protected override void OnFormClosing( FormClosingEventArgs e )
     {
        base.OnFormClosing( e );
        if( !e.Cancel )
        {
          e.Cancel = true;
          this.Visible = false;
          SetFocus(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
        }
     }

      public void ShowModeless
      {
          Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(this);
      }

   }


TonyT

  • Guest
Re: ShowModelessDialog + ModalDialog = focus problems
« Reply #5 on: February 03, 2009, 01:39:15 AM »
Yes, you are giving me the same solution you gave in the mentioned thread and I'm here asking if this is the only way (to get rid of the focus problem) and isn't this affecting autocad's speed when a users starts opening forms which instead of being properly closed when their time is over they hung around being invisible.

No, what makes you think that a hidden form affects AutoCAD's speed?

Only if there are hundreds of them, which is not likely.

Most of the palette-based tools you use in AutoCAD do exactly the
same thing - they only hide when you close them, rather than destroy
themselves.

Only in cases where a form is handling some event(s) that fire often,
can there be unnecessary overhead, and of course, if the form is well
designed, it will stop/start handling events when it is hidden/shown.

Quote
Also, I want the user to be given a fresh form, without any changes
he might have done last time he used it so your way is just adding
extra work...

It depends on the particulars. In most cases, I want a form to be in
the exact same state it was in when it was last visible. If that's not
what you want, there's no point to using the code I show, and you
can just let the form dispose itself when closed, and create another
instance when you need to show it again.

Regarding the focus problem, you're playing a bit of a guessing game
with us, because it could depend on how certain Form properties are
set (TopMost and ShowInTaskBar being the usual suspects), as well
as what your call to MessageBox.Show() looks like.

So, if someone here can guess exactly what's going on in your code
that may be causing the problem, do they win a free, all-expense-paid
trip to DisneyLand, or something like that?

« Last Edit: February 03, 2009, 02:31:26 AM by TonyT »

simor

  • Guest
Re: ShowModelessDialog + ModalDialog = focus problems
« Reply #6 on: February 03, 2009, 03:43:33 AM »
Yeap, manually setting the focus (after the MessageBox returns) stops AutoCAD from loosing it..

Code: [Select]
SetFocus(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle);
or calling

Code: [Select]
MessageBox.Show(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow, text, caption, buttons, icon)
also fixes the problem.

Thanks a lot!

hericson

  • Guest
Re: ShowModelessDialog + ModalDialog = focus problems
« Reply #7 on: July 03, 2009, 11:29:02 AM »
I have a similar problem:

I start a dialog with dialogName.ShowDialog(). When I close the dialog I would like to display a message box before closing the dialog but I want to hide the dialog so the user don't see it under the message box. I use Me.Hide() for hiding the dialog. The dialog hides but switches to another open program and shows my message box on top of that application instead of AutoCAD! When I close the message box, AutoCAD will switch back.

The difference (I think) is that in my case AutoCAD loses focus while hiding the dialog and showing the message box, instead of losing focus after closing the message box in simors case.