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
Solved! Go to Solution.
Solved by khoa.ho. Go to Solution.
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 } }
Huh. That is strange.
This did solve my problem. I guess I will avoid ShowModalDialog in the future.
Thanks.
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.