Showing custom modal dialogs in managed code - David Airapetyan

InfoPath Dev

David Airapetyan

Showing custom modal dialogs in managed code

A common question is to how to show custom dialogs using the InfoPath 2007 OM. In InfoPath 2003 one could use the ShowModalDialog OM function that would take an arbitrary HTML and show it. Although you can still use the old IP2003 OM in InfoPath 2007, there is a better option if you are writing in managed code, namely WinForms!

If you are writing using IP2007 OM, you don't need to do anything special, System.Windows.Forms has already been referenced for you and the using statement has been included.

If you are writing using IP2003 OM, make sure to select "Add Reference" for your project and add System.Windows.Forms. Now you can start showing message boxes or create your custom winforms and show them when needed.

Important: you cannot use WinForms when developing for InfoPath Forms Server. This is because no .NET code can run on the client and running WinForms on the server makes little sense. If you cannot use WinForms, make sure your compatibility is not set to "Browser" in Design Checker.

Scenario 1 - Show a simple confirmation box

if (MessageBox.Show(
   "Are you sure",
MessageBoxButtons.OKCancel) == DialogResult.OK)
{      // do something

Note: in IP2003 OM, you will have to prefix each WinForms type with "System.Windows.Forms". You cannot simply add a "using" statement because WinForms types will clash with the IP2003 ones. However you could use aliases in your using, for example:

using WF=System.Windows.Forms;

Scenario 2 – Show a custom dialog box


Showing a custom dialog box is as simple as adding a new WinForm class to your project. Simply right-click the project in SolutionExplorer, select "Add" and then "Windows Form".

Here is source code example for a Message Box that can show/hide additional details:

To invoke it, call the following: new MessageBoxWithDetails(caption, message, details).ShowDialog();

MessageBoxWithDetails is good to show exception data with the stack trace going into details, for example.



Dilip Singh Dodiya said:

Its really good one.

August 30, 2007 11:21 PM

ultragc said:

Does this work for browser based Infopath form?

September 27, 2007 1:57 PM

Infopath Custom Dialogs Using Winforms « Sladescross's Blog said:

Pingback from  Infopath Custom Dialogs Using Winforms « Sladescross's Blog

September 27, 2009 3:30 AM

About davidair

I am a Computer Science graduate from McGill University in Montreal, Canada. I have over seven years of industry experience including almost five of them spent with the Microsoft Corporation where I shipped several versions of Microsoft Office InfoPath. Originally from Russia, I am fluent in several languages, including Russian, English, French, C#, C++ and Perl. I currently reside in Montreal and work for Qdabra Software.
Copyright © 2003-2018 Qdabra Software. All rights reserved.
View our Terms of Use.