1. Technology

Using Dialog Forms

There's a lot of power there if you know how to tap into it!


Updated May 28, 2014

In the Visual Studio 2010 documentation, Microsoft defines a dialog form as, "a form with its FormBorderStyle enumeration property set to FixedDialog." That wasn't the definition I was expecting to see.

Windows has dialog boxes everywhere, from the humble MsgBox to complex forms like the ColorDialog. One suspects that the variety and differences in forms that are called "dialog boxes" make this the only thing that is common among all of them, and that's why Microsoft's documentation reads like that. But our experience tells us that there is a lot more to them than just the FormBorderStyle property. In fact, a much better definition of a "dialog" is the Dialog form that you can Add to a project in Visual Studio Solution Explorer.

Click Here to display the illustration

The properties that are usually different in most dialogs are bolded in the Properties window for the Dialog form and include not just FormBorderStyle, but AcceptButton, CancelButton, MinimizeBox, MaximizeBox, ShownInTaskbar and StartPosition. A panel container control with two buttons, OK and Cancel, are included along with Click event code to handle them.

Click Here to display the illustration

The value returned from a dialog is the DialogResult property.

Dim theResult As Integer = Dialog2.ShowDialog()

This is a property that is defined for all Windows Forms, but it's not useful unless the form is displayed using the ShowDialog method that displays it as a modal form.

DialogResult is an interesting property because it is part of both the Form and Button controls. You can set the design time property of a Button to determine what is returned when it's clicked. If the Button is contained in a Form that has been displayed using ShowDialog, then the Button DialogResult property is assigned to the Form automatically. When you Add a Dialog form to your project in Visual Studio, the two pre-configured buttons in the form work that way.

Click Here to display the illustration

The real definition of a dialog: A Modal Form

A "dialog" is better defined, not by what it is, but by how it's called. In order for a dialog to behave the way you expect, it has to be called using the ShowDialog method. If you would like to prove this to yourself, don't use Microsoft's Dialog template from Visual Studio; build your own using a standard Windows Form and see if you can see a difference with the Dialog template. As Microsoft's documentation notes, perhaps too concisely, "You can construct your own custom dialog boxes by using the Windows Forms Designer in Visual Studio."

Any form can be a "dialog" if it's called with the ShowDialog method because this makes the form "modal" and that's the most clearly distinguishing feature of dialogs. (See the Note at the end of the article.) It results in several changes in the way your code runs:

  • The code following "<formname>.ShowModal" isn't executed until the dialog is closed.
  • Any form that is opened using "<formname>.ShowModal" can be closed by setting the DialogResult property to something other than None (the default value).

If this code using Show is in the calling (parent) form:

' More code in the calling (parent) form

"More code" is executed immediately after the Dialog1 form is displayed without waiting for any event (such as a button being clicked). It doesn't matter what properties are in Dialog1. It's just a standard form if it's called with the Show() method.

If this code using ShowDialog is in the calling (parent) form:

' More code in the calling (parent) form

"More code" isn't executed until after Form2 is closed. If you add code in a Form2 event that sets the DialogResults property to something other than None ...

Public Class Form2
    Private Sub Button1_Click(ByVal ...
        Me.DialogResult = DialogResult.Yes
    End Sub

... then Form2 will close.

On the next page, we look at what happens when a dialog is closed in more detail.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Tapping Into the Power of Dialog Forms in VB.NET

©2014 About.com. All rights reserved.