Modeless Dialogs

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 12.  Advanced Windows Forms


VB.NET supports modeless dialogs as well as modal dialogs. You can typically identify modeless dialogs from their design: modal dialogs have OK and Cancel buttons, while modeless dialogs have Apply and Close buttons . A few programming differences must be noted:

  • The Show method is used to display the modeless dialog (instead of the ShowDialog method).

  • A DialogResult is not returned from the Show method; programmers must implement their own strategy for providing behavior for the Apply and Close buttons in the modeless dialog.

graphics/codeexample.gif

In the program ModelessDialogExample , we use a modeless dialog to add items to a list box. Figure 12-1 illustrates the program as it is executed.

Figure 12-1. Using modeless dialogs.

graphics/12fig01.jpg

Step 1: Designing the GUI

To build this application, we have added two forms to our project. The startup form's properties are summarized in Table 12-1, while the modeless dialog's properties are summarized in Table 12-2.

Table 12-1. Property Values for MainWindow
Control Type Name Other Properties
Listbox lstItems  
Button btnNew Text: New Data
Form MainWindow Text: Main Window
Table 12-2. Property Values for NewDataForm
Control Type Name Other Properties
Label lblItem Text: Item:
Textbox txtItem Text: (blank)
Button btnApply

Enabled: False

Text: Apply

Button btnClose Text: Close
Form NewDataForm

MaximizeBox: False

MinimizeBox: False

ShowInTaskbar: False

Text: New Data

Step 2: Managing the Relationship between Forms

A modeless dialog must know about the form that displayed it so the code in the Click event of its Apply button can copy data back to its parent form. We must add a variable to the modeless dialog to manage this relationship. We must also modify the form's constructor to accept a value for this variable.

 Class NewDataForm     Inherits System.Windows.Forms.Form  Private m_myParent As MainWindow  Public Sub New(  ByVal parent As MainWindow  )       MyBase.New()  m_myparent = parent  ...    End Sub    ... End Class 

The Click event handler for MainWindow's btnShowData will pass a reference to itself when it instantiates the NewDataForm .

 Private Sub btnNew_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnNew.Click  Dim newData As New NewDataForm(Me)   newData.Show()  End Sub 

Step 3: Programming the Apply and Close Buttons

Now that the modeless dialog can be displayed, we must provide code for the Apply and Close buttons. The Apply button must copy the data in its controls back to controls on the parent form. The Apply button's Click event handler is coded as follows :

 Private Sub btnApply_Click(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles btnApply.Click  If Trim(txtItem.Text) <> "" Then   m_myParent.lstItems.Items.Add(txtItem.Text)   txtItem.Text = ""   End If  End Sub 

The Close button's Click event handler should call the form's Close method to close the form.

 Private Sub btnClose_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnClose.Click  Me.Close()  End Sub 

Step 4: Enabling and Disabling the Apply Button

Many modeless dialogs enable and disable the Apply button as a user interacts with them. Only when the Apply button is enabled is the user able to move data back to the parent form.

We set btnApply's Enabled property to False at design time. When the user changes the data in txtItem , we will monitor it to determine whether the Apply button should be enabled or disabled. This can be done in the Change event of the textbox.

 Private Sub txtItem_TextChanged(ByVal sender As _   System.Object, ByVal e As System.EventArgs) _  Handles txtItem.TextChanged  If Trim(txtItem.Text) <> "" Then   btnApply.Enabled = True   Else   btnApply.Enabled = False   End If  End Sub 

Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net