Basic Dialog boxes
Written by Mike James   
Wednesday, 01 December 2010
Article Index
Basic Dialog boxes
Getting data

Dialog boxes are everywhere – but what is the best way to implement them in .NET? We take a look at how to organise things to create a custom dialog box in Visual Basic .NET


Dialog boxes may not be exciting but they are still the most common way of getting information from the user and showing exactly how things are configured.  In many cases you can simply use a standard dialog box but to get custom information you need a custom dialog box.

The good news is that creating custom dialog boxes in .NET Windows forms is particularly easy. The main problem is that while the documentation is fairly clear on many of the topics relating to creating and using dialog boxes, it is scattered.

So let’s take a look at how to create and use a .NET custom dialog box. The language used is Visual Basic but as the key features are provided by the Framework only the superficial details change in other languages.

The very first thing to say is that there isn’t a special dialog box class.

In .NET forms a dialog box is just a customised Windows form. In this sense any window can be regarded as a potential dialog box.

The look of it

What marks out a window as a dialog box as far as the user is concerned is the look of it. To make a form look like a dialog box you need set the form’s properties as:

  • FormBorderStyle to FixedDialog,
  • ControlBox to false
  • MinimizeBox to false
  • MaximizeBox to false

You can do this either in the Forms designer or programmatically at run time.

For example, add a new form called Dialog. To customise it add:

Private Sub Dialog_Load(
ByVal sender As System.Object,
ByVal e As System.EventArgs)
     Handles MyBase.Load
 FormBorderStyle =
 ControlBox = False
 MinimizeBox = False
 MaximizeBox = False
End Sub
Now that we have something that looks like a dialog box the next problem is displaying it modally.


A modal window is one that blocks the current thread until the user has finished with it.  In .NET there are two methods that show a window

  • Show which displays a form non-modally


  • ShowDialog which displays a form modally.

If you use ShowDialog then the form also behaves a little differently in that it returns a DialogResult enumeration. The value of the DialogResult indicates whether or not the user has clicked the OK or Cancel button.

So to show a modal dialog box you would use something like:

Dim D As New Dialog
Dim result As DialogResult=D.ShowDialog()

Notice that  you don’t need to create an instance of Dialog as you can use the Dialog class as if it was an object and leave it to the compiler to create an instance for you.

Dim result As DialogResult=

Dialog results

Of course we still have the problem of how to set the DialogResult.

The first thing to say is that DialogResult is a property of the window class and you can get and set it in the usual way. The value when the dialog box is dismissed is returned as the result.

So how does the user dismiss the dialog box?

The answer is that if the value of DialogResult is set to anything other than None then the dialog box returns control to the calling program and uses the value of DialogResult as its result.

For example, if you place button on the dialog box, set its text to “OK” and code its click event handler as:

Private Sub Button1_Click(
   ByVal sender As System.Object,
   ByVal e As System.EventArgs)
                   Handles Button1.Click
 DialogResult = DialogResult.OK
End Sub

then when the user clicks the button the dialog box returns the value OK to the calling program.

Notice that this method works with any control you care to put on a dialog box.

For example if you want to provide a checkbox that ends the dialog you could code its change event handler as:

Private Sub CheckBox1_CheckedChanged(
   ByVal sender As System.Object,
   ByVal e As System.EventArgs)
         Handles CheckBox1.CheckedChanged
 If (CheckBox1.Checked) Then
          DialogResult = DialogResult.OK
End Sub

However in nearly all cases dialog boxes are terminated by the user clicking on a button and the Framework has made all our lives easier by giving the button control a DialogResult property.

This can be set at design time or run time to the value that you want to return when the button is clicked. Any button that has a DialogResult set to anything other than “none” and will automatically terminate the dialog box when it is clicked without you having to setup an explicit event handler.

So, if you add two buttons to the dialog box, one labelled OK and one labelled Cancel, and set the OK button’s DialogResult to OK and the Cancel button’s DialogResult to Cancel using the property window, you have a working dialog box. If you also set the Form’s AcceptButton property to the OK button and its CancelButton to the Cancel button the dialog box will return OK when the user presses the enter key and the Cancel when they press escape.

All very easy but what about the data that the user enters?









Last Updated ( Wednesday, 01 December 2010 )