Create and manage modal dialog boxes
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
When you create a modal dialog box inside Visual Studio, you must make sure that the parent window of the dialog box is disabled while the dialog box is displayed, then re-enable the parent window after the dialog box is closed. If you do not do so, you may receive the error: Microsoft Visual Studio cannot shut down because a modal dialog is active. Close the active dialog and try again.
There are two ways of doing this. The recommended way, if you have a WPF dialog box, is to derive it from DialogWindow, and then call ShowModal to display the dialog box. If you do this, you do not need to manage the modal state of the parent window.
If your dialog box is not WPF, or for some other reason you cannot derive your dialog box class from DialogWindow, then you must get the parent of the dialog box by calling GetDialogOwnerHwnd and manage the modal state yourself, by calling the EnableModeless method with a parameter of 0 (false) before displaying the dialog box and calling the method again with a parameter of 1 (true) after closing the dialog box.
Create a dialog box derived from DialogWindow
Create a VSIX project named OpenDialogTest and add a menu command named OpenDialog. For more information about how to do this, see Create an extension with a menu command.
To use the DialogWindow class, you must add references to the following assemblies (in the Framework tab of the Add Reference dialog box):
PresentationCore
PresentationFramework
WindowsBase
System.Xaml
In OpenDialog.cs, add the following
using
statement:using Microsoft.VisualStudio.PlatformUI;
Declare a class named
TestDialogWindow
that derives from DialogWindow:class TestDialogWindow : DialogWindow {. . .}
To be able to minimize and maximize the dialog box, set HasMaximizeButton and HasMinimizeButton to true:
internal TestDialogWindow() { this.HasMaximizeButton = true; this.HasMinimizeButton = true; }
In the
OpenDialog.ShowMessageBox
method, replace the existing code with the following:TestDialogWindow testDialog = new TestDialogWindow(); testDialog.ShowModal();
Build and run the application. The experimental instance of Visual Studio should appear. On the Tools menu of the experimental instance you should see a command named Invoke OpenDialog. When you click this command, you should see the dialog window. You should be able to minimize and maximize the window.
Create and manage a dialog box not derived from DialogWindow
For this procedure, you can use the OpenDialogTest solution you created in the previous procedure, with the same assembly references.
Add the following
using
declarations:using System.Windows; using Microsoft.Internal.VisualStudio.PlatformUI;
Create a class named
TestDialogWindow2
that derives from Window:class TestDialogWindow2 : Window {. . .}
Add a private reference to IVsUIShell:
private IVsUIShell shell;
Add a constructor that sets the reference to IVsUIShell:
public TestDialogWindow2(IVsUIShell uiShell) { shell = uiShell; }
In the
OpenDialog.ShowMessageBox
method, replace the existing code with the following:IVsUIShell uiShell = (IVsUIShell)ServiceProvider.GetService(typeof(SVsUIShell)); TestDialogWindow2 testDialog2 = new TestDialogWindow2(uiShell); //get the owner of this dialog IntPtr hwnd; uiShell.GetDialogOwnerHwnd(out hwnd); testDialog2.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; uiShell.EnableModeless(0); try { WindowHelper.ShowModal(testDialog2, hwnd); } finally { // This will take place after the window is closed. uiShell.EnableModeless(1); }
Build and run the application. On the Tools menu you should see a command named Invoke OpenDialog. When you click this command, you should see the dialog window.