KnowDotNet NetRefactor

Modeless Dialogs in an Add-in

Control Characters Consumed by the IDE

by Les Smith
Print this Article Discuss in Forums

Why do control characters not enter a text box in a modeless dialog in an add-in?  Visual Studio .NET consumes control characters destined for modeless dialogs in an add-in.

Sometimes you need to create a modeless form for display in an add-in. Obviously, you can do this by creating an instance of the form and showing it by using "o.Show()". It will work fine unless you have one or more text boxes on the dialog that you use for input. The text boxes will accept input of alphanumeric characters, but if you try to enter a "control character" such as backspace, del, or an arrow key, etc., those keys will not be entered into the text box. Rather, they will be captured by the IDE, and if you have a code window that was active at the time the modeless dialog is being used, the key, backspace for instance, will actually wipe out code in your code window and leave you wondering what happened.

There are a couple of ways of handling this problem. Neither are straightforward. Microsoft's solution would be to create a Tool Window. This is a good solution if the dialog is not too big and busy, but it is not a simple solution. In order to create a tool window, if you have not done it, you need to go to Microsoft's web site and search for automation samples or for CreateToolWindow. Creating a tool window is fairly complex because you have to have a hosting object for the tool window. The sample from Microsoft contains a C++ project that you can build to host a user control in a tool window plus a sample VB, C#, and C++ project to actually create a tool window with a simple control in it.

A second way to solve the problem, albeit not real pretty, is to create the modeless dialog, and when it needs input, display a modeless dialog via .ShowDialog() to get the input for the modeless dialog. I have done this when I want to show a results dialog such as a grid or listview containing data from a datatable. It's probably not pleasing to the purist, but it works and gets around what I believe to be a shortcoming in the automation model.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing