Building Windows Applications

Creating and Using a Form as a Dialog Box

Technically speaking, every window/form in an application could be called a dialog box. When I use the term, however, I am referring specifically to a window that is displayed to request some information from the users and return that information back to the application that displayed the dialog box. For the most part, the calling application does not care what happens between displaying the form and the user clicking OK or Cancel to close it; all it is concerned with is the information gathered by the dialog box. To illustrate how you can create a standard dialog box using Visual Basic .NET, this section walks you through the creation of a dialog box that is designed to allow the users to enter in an address (see Figure 3.18 ).

Figure 3.18
An Address entry dialog box.

Setting Up Your Form

First, you create the sample application and form. Open a new project, a Visual Basic Windows Application, and add a new form named GetAddress . There should now be two forms in your project, which is exactly what you want because we will launch the GetAddress dialog box from Form1 . Now, you need to set up the look of GetAddress to match the expected appearance of a dialog box. Set up four text boxes named txtStreet , txtCity , txtPostalCode , and txtCountry on your form and arrange them somewhat like the form shown in Figure 3.18. Now, add two buttons to your form, saveAddress and cancelAddress , with captions of OK and Cancel, respectively. The two buttons should be positioned in the lower-right corner. If you are planning to make your dialog box resizable, you will want to anchor the two buttons to bottom and right. Select the form itself (click any empty area of the design surface) and set its AcceptButton and CancelButton properties to the saveAddress and cancelAddress buttons. Setting these properties allows the users to use the Enter and Escape keys as the equivalent of OK and Cancel. The AcceptButton property also makes the saveAddress button into the default for the form, which causes it to be highlighted. So that you can tell what button was pressed to exit the form, you should also set the DialogResult property of both buttons. Set the DialogResult property for saveAddress to OK , and set it to Cancel for cancelAddress .

If you want your dialog box to be resizable, select the Sizable option for the FormBorderStyle property. For a fixed sized dialog box, you select FixedDialog for FormBorderStyle and set MinimizeBox and MaximizeBox both to False .

Once you have created your dialog box, the key to using it is to determine a method for putting starting data into the dialog box and for pulling out the information the user entered. You could access the various controls (the text boxes) directly, but I strongly advise against it. If you work directly with the controls, you will have to change that code if you ever modify the dialog box. Instead, I suggest one of two approaches. Either create a property procedure for each of the values you are exchanging (street, city, postal code, and country in this example) or create a new class that holds all of these values and then create a single property for that object.

This section shows you both methods; you can use whichever one you prefer. For the first case, using multiple properties, create a property for each of the four values you are dealing with, as shown in Listing 3.23.

Listing 3.23 You Can Insert and Remove Values from Your Dialog Box Using Properties

Public Property Street() As String
  Get
    Return Me.txtStreet.Text
  End Get
  Set(ByVal Value As String)
    Me.txtStreet.Text = Value
  End Set
End Property
Public Property City() As String
  Get
    Return Me.txtCity.Text
  End Get
  Set(ByVal Value As String)
    Me.txtCity.Text = Value
  End Set
End Property
Public Property PostalCode() As String
  Get
    Return Me.txtPostalCode.Text
  End Get
  Set(ByVal Value As String)
    Me.txtPostalCode.Text = Value
  End Set
End Property
Public Property Country() As String
  Get
    Return Me.txtCountry.Text
  End Get
  Set(ByVal Value As String)
    Me.txtCountry.Text = Value
  End Set
End Property

For now, these property procedures are just working with the text boxes directly. The value in using property procedures instead of direct control access is that you can change these procedures later, without affecting the code that calls this dialog box. The properties are all set up now, but to make the dialog box work correctly, you also need some code (shown in Listing 3.24) in the OK and Cancel buttons.

Listing 3.24 Don't Forget to Provide a Way to Close Your Forms

Private Sub saveAddress_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles saveAddress.Click
  Me.Close()
End Sub
Private Sub cancelAddress_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles cancelAddress.Click
  Me.Close()
End Sub

Although both button click events do the same thing, close the dialog box, the DialogResult property of each button is set appropriately so it will result in the correct result value being returned to the calling code. In the calling procedure, a button click event on the first form, you populate the dialog box using the property procedures, display it using ShowDialog() , and then retrieve the property settings back into the local variables. ShowDialog is the equivalent of showing a form in VB6 passing in vbModal for the modal parameter, but with the added benefit of a return value. ShowDialog returns a DialogResult value to indicate how the user exited the dialog box. The example in Listing 3.25 checks for the OK result code before retrieving the properties from the dialog box.

Listing 3.25 If Users Click OK, You Must Copy the Values Back, Otherwise You Don't Want to Change Anything Because They Must Have Clicked Cancel

Private Sub Button1_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
  Dim address As New addressDialog
  Dim Street As String = "124 First Street"
  Dim City As String = "Redmond"
  Dim Country As String = "USA"
  Dim PostalCode As String = "98052"
  address.Street = Street
  address.City = City
  address.Country = Country
  address.PostalCode = PostalCode
  If address.ShowDialog = DialogResult.OK Then
    Street = address.Street
    City = address.City
    Country = address.Country
    PostalCode = address.PostalCode
  End If
End Sub

The other method I mentioned, using a class to hold a set of values instead of passing each value individually, requires just a few modifications to the code. First, you need to create the class. Add a new class to your project, named Address , and enter the class definition shown in Listing 3.26.

Listing 3.26 With a Class being Used to Hold Multiple Values, You Can Add New Properties to it without Having to Change the Code to Pass it Around

Public Class address
  Dim m_Street As String
  Dim m_City As String
  Dim m_PostalCode As String
  Dim m_Country As String
  Public Property Street() As String
    Get
      Return m_Street
    End Get
    Set(ByVal Value As String)
      m_Street = Value
    End Set
  End Property
  Public Property City() As String
    Get
      Return m_City
    End Get
    Set(ByVal Value As String)
      m_City = Value
    End Set
  End Property
  Public Property PostalCode() As String
    Get
      Return m_PostalCode
    End Get
    Set(ByVal Value As String)
      m_PostalCode = Value
    End Set
  End Property
  Public Property Country() As String
    Get
      Return m_Country
    End Get
    Set(ByVal Value As String)
      m_Country = Value
    End Set
  End Property
End Class

This class is nothing more than a convenient way to package data into a single object, but it allows you to have only a single property procedure (Listing 3.27) in the dialog box and to simplify the calling code in Form1 (see Listing 3.28).

Listing 3.27 The Number of Properties Is Reduced to One if You Switch to Using a Class Versus Individual Properties on Your Form

Public Property Address() As address
  Get
    Dim newAddress As New address
    newAddress.City = txtCity.Text
    newAddress.Country = txtCountry.Text
    newAddress.PostalCode = txtPostalCode.Text
    newAddress.Street = txtStreet.Text
    Return newAddress
  End Get
  Set(ByVal Value As address)
    txtCity.Text = Value.City
    txtCountry.Text = Value.Country
    txtPostalCode.Text = Value.PostalCode
    txtStreet.Text = Value.Street
  End Set
End Property
ty

Listing 3.28 A Single Property to Exchange Data Simplifies the Calling Code as well as the Form

Private Sub Button1_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
  Dim addr As New address
  Dim address As New addressDialog
  Dim Street As String = "124 First Street"
  Dim City As String = "Redmond"
  Dim Country As String = "USA"
  Dim PostalCode As String = "98052"
  addr.Street = Street
  addr.City = City
  addr.Country = Country
  addr.PostalCode = PostalCode
  address.Address = addr
  If address.ShowDialog = DialogResult.OK Then
    addr = address.Address
  End If
End Sub

The simple addition of a return code when displaying a modal form makes it easier to implement dialog boxes within Visual Basic .NET, but the lack of default form instances can make all multiple form applications difficult.

In Brief

This chapter presented a detailed overview of the new Windows client development model in Visual Basic .NET. Here are the important points of this chapter:

  • Windows forms is the new forms model for Visual Basic. Although it is similar to the forms model for Visual Basic 6.0, it supports many new features.
  • In .NET, you can see and sometimes edit all of the code that creates and configures your user interface.
  • Event handling in .NET is no longer based on the name of the event-handler procedure, instead a new Handles keyword has been added.
  • Form layout is more powerful in .NET than in VB6 due to the addition of new features for docking and anchoring.
  • There are no default instances of forms in VB .NET, which means you cannot work with the form's name as if it were always available.

This is a sample chapter from Microsoft Visual Basic .NET 2003 Kick Start.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”