Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
136
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

216 Chapter 5 WORKING WITH FORMS

VB.NET at Work: The MultipleForms Project

It’s time to write an application that puts together the most important topics discussed in this section. There’s quite a bit to learn about projects with multiple forms, and this is the topic of the following sections. Most of the aspects discussed here are demonstrated in the MultipleForms project, which you will find on the CD. I suggest you follow the steps outlined in the text to build the project on your own.

The MultipleForms project consists of a main form, an auxiliary form, and a dialog box. All three components of the application’s interface are shown in Figure 5.17. The buttons on the main form display both the auxiliary form and the dialog box.

Figure 5.17

The MultipleForms project’s interface

Let’s review the various operations we want to perform—they’re typical for many situations, not specific to this application. At first, we must be able to invoke both the auxiliary form and the dialog box from within the main form; the Show Auxiliary Form and Show Age Form buttons do this. The main form contains a variable declaration, strProperty. This variable is, in effect, a property of the main form and is declared with the following statement:

Public Shared strProperty As String = “Mastering VB.NET”

The main form’s code declares a variable that represents the auxiliary form and then calls its Show method to display the auxiliary form. The declaration must appear in the form’s declarations section:

Dim FRM As New AuxiliaryForm()

The Show Auxiliary Form button contains a single statement, which invokes the auxiliary form by calling the Show method of the FRM variable.

The auxiliary-form button named Read Shared Variable In Main Form reads the strProperty variable of the main form with the following statement:

Private Sub bttnReadShared_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnReadShared.Click

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

LOADING AND SHOWING FORMS 217

MsgBox(MainForm.strProperty, MsgBoxStyle.OKOnly, “Public Variable Value”)

End Sub

Using the same notation, you can set this variable from within the auxiliary form. The following event handler prompts the user for a new value and assigns it to the shared variable of the main form. You can confirm that the value has changed by reading it again.

Private Sub bttnSetShared_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnSetShared.Click Dim str As String

str = InputBox(“Enter a new value for strProperty”) MainForm.strProperty = str

End Sub

The two forms communicate with each other through public variables. Let’s make this communication a little more elaborate by adding an event. Every time the auxiliary form sets the value of the strProperty variable, it will raise an event to notify the main form. The main form, in turn, will use this event to display the new value of the string on the TextBox control as soon as the code in the auxiliary form changes the value of the variable and before it’s closed.

To raise an event, you must declare the event’s name in the form’s declaration section. Insert the following statement in the auxiliary form’s declarations section:

Event strPropertyChanged()

Now add a statement that fires the event. To raise an event, we call the RaiseEvent statement passing the name of the event as argument. This statement must appear in the Click event handler of the Set Shared Variable In Main Form button, right after setting the value of the shared variable. Listing 5.6 shows the revised event handler.

Listing 5.6: Raising an Event

Private Sub bttnSetShared_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles bttnSetShared.Click Dim str As String

str = InputBox(“Enter a new value for strProperty”) MainForm.strProperty = str

RaiseEvent strPropertyChanged End Sub

The event will be raised, but it will go unnoticed if we don’t handle it from within the main form’s code. To handle the event, you must change the declaration of the FRM variable from

Dim FRM As New AuxiliaryForm()

to

Dim WithEvents FRM As New AuxiliaryForm()

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

218 Chapter 5 WORKING WITH FORMS

The WithEvents keyword tells VB that the variable is capable of raising events. If you expand the drop-down list with the objects in the code editor, you will see the name of the FRM variable, along with the other controls you can program. Select FRM in the list and then expand the list of events for the selected item. In this list, you will see the strPropertyChanged event. Select it, and the definition of an event handler will appear. Enter these statements in this event’s handler:

Private Sub FRM_strPropertyChanged() Handles FRM.strPropertyChanged

TextBox1.Text = strProperty

Beep()

End Sub

It’s a very simple handler, but it’s adequate for demonstrating how to raise and handle custom events. If you run the application now, you’ll see that the value of the TextBox control changes as soon as you change the value in the auxiliary form.

Of course, you can update the TextBox control on the main form directly from within the auxiliary form’s code. Use the expression MainForm.TextBox1 to access the control and then manipulate it as usual. Events are used when we want to perform some actions on a form when an action takes place in one of the other forms of the application.

Let’s see now how the main form interacts with the dialog box. What goes on between a form and a dialog box is not exactly “interaction”—it’s a more timid type of behavior. The form displays the dialog box and then waits until the user closes the dialog box. Then, it looks at the value of the DialogResult property to find out whether it should even examine the values passed back by the dialog box. If the user has closed the dialog box with the Cancel (or an equivalent) button, the application ignores the dialog box settings. If the user closed the dialog box with the OK button, the application reads the values and proceeds accordingly.

Before showing the dialog box, the code of the Show Dialog Box button sets the values of certain controls on it. In the course of the application, it usually makes sense to suggest a few values on the dialog box, so that the user can accept the default values. The main form selects a date on the controls that display the date, and then displays the dialog box with the statements given in Listing 5.7.

Listing 5.7: Displaying a Dialog Box and Reading Its Values

Protected Sub Button3_Click(ByVal sender As Object, _

ByVal e As System.EventArgs)

Preselects the date 4/11/1980 DLG.cmbMonth.Text = “4” DLG.cmbDay.Text = “11” DLG.CmbYear.Text = “1980” DLG.ShowDialog()

If DLG.DialogResult = DialogResult.OK Then MsgBox(DLG.cmbMonth.Text & “ “ & DLG.cmbDay.Text & “, “ & _

DLG.cmbYear.Text)

Else

MsgBox(“OK, we’ll protect your vital personal data”)

End If

End Sub

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com