.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ShowModalDialog dialog closes when I switch windows

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
mikael.bjork
853 Views, 3 Replies

ShowModalDialog dialog closes when I switch windows

I open a regular windows form using ShowModalDialog like this:

 

Dialogs.OptionsForm options = new Dialogs.OptionsForm(Configuration.Read());
if (Application.ShowModalDialog(Application.MainWindow.Handle, options, false) == Forms.DialogResult.OK)
{
// Do stuff }

 

But when I have the form open and switch windows (for example by reaching a break-point in visual studio, but just clicking in outlook or notepad works) the form closes, apparantly with the Cancel return value.

 

Does anyone know what is causing this problem. In my office we have various applications that have this problem with some forms, but not all, even in this specific addin there are several forms that do not close like this.

 

regards

3 REPLIES 3
Message 2 of 4
khoa.ho
in reply to: mikael.bjork

The following is an alternative method of the built-in AutoCAD method Application.ShowModalDialog(). I am not sure it will work for your problem. You can give it a try:

 

[CommandMethod("ShowDialogTest")]
public static void ShowDialogTest()
{
    var testForm = new System.Windows.Forms.Form();

    var nativeWindow = new System.Windows.Forms.NativeWindow();
    nativeWindow.AssignHandle(Application.MainWindow.Handle);

    // Show the modal form inside AutoCAD
    if (testForm.ShowDialog(nativeWindow) == System.Windows.Forms.DialogResult.OK)
    {
        // Do stuff
    }
}

 

Message 3 of 4
mikael.bjork
in reply to: khoa.ho

Huh. That is strange.

 

This did solve my problem. I guess I will avoid ShowModalDialog in the future.

 

Thanks.

Message 4 of 4
khoa.ho
in reply to: mikael.bjork

The difference is Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog() uses AutoCAD native call to hookup the windows form handle with the parent AutoCAD main window handle pointer. While nativeWindow.AssignWindow() and form.ShowDialog(nativeWindow) use all .NET based methods.

 

Both two solutions use form.ShowDialog() or form.Show() to display the modal/modeless form in the host application. But they are different in the way connecting parent window with its child window. I guess .NET may do the job a bit better.

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost