![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
View/ControllersIn many programs the distinction between views and controllers blurs. In AppointmentBook the month area acts as a view when it displays a calendar showing which dates contain appointments. It acts as a controller when the user clicks on it to select a new date. The month area acts as both a view and a controller. View/controllers often arise from the direct-manipulation nature of Windows programs. The month area allows a user to view and manipulate the data so it is both a view and a controller. Many other Visual Basic controls work similarly. Text boxes, option buttons, check boxes, combo boxes, scroll bars, lists, and others can all be used both to display and manipulate information. When a class is tied closely to one of these controls, chances are good the class will be both a view and a controller. When working with a view/controller class, a program should keep the view and controller functions as separate as possible. Programs that use one routine to play the roles of both view and controller can be very confusing. In particular, whenever possible the program should not make controller routines update the views display. Instead, the model should tell the view/controller, acting as a view, to update itself. For example, when the user clicks on AppointmentBooks month area, the area must select a new date and update its display to show the new date. Rather than performing both of these actions in the single subroutine, the CalVC class uses the models subroutines only to set the new date. The model then notifies the view of the change so it can update its display. This technique does not work with Visual Basic controls that automatically update their own appearances. For instance, suppose a program has a view/controller object that manages a text box. Then suppose program code changes the value of the text. This generates a Changed event for the text box. Acting as a controller, the object detects the change and updates the model. The model then notifies the object that a change to the text has occurred. Now acting as a view, the object updates the text box. This generates another Changed event, which causes the object to act as a controller again and notify the model of the change. This causes another event notification, which causes a text update, which causes yet another event notification, and so forth. In cases such as this one, the program needs a way to break out of the infinite cascade of changes and event notifications. AppointmentBooks view/controller objects do this with a Boolean variable IgnoreChanges. Just before the object (acting as a view) modifies the display, it sets IgnoreChanges to true. When the object detects a change, it checks the value of IgnoreChanges. If the value is true, it ignores the change. Otherwise, it reports the change (acting as a controller) to the model. After the object (acting as a view) finishes modifying the display, it sets IgnoreChanges back to false so changes made by the user are reported to the model in the future. The following code fragment shows how AppointmentBook prevents event cascades for the month areas scroll bar:
Private IgnoreChanges As Boolean
:
The model is notifying us that the selected date has changed.
Public Sub Notify(event_mask As Long)
:
Verify that the selected date has changed.
If event_mask And CAL_SelectedDateChanged Then
IgnoreChanges = True Prevent an event cascade.
Change the scroll bar value to reflect the new date.
TheScrollBar.Value = _
DateDiff(ww, TheModel.FirstDate, _
TheModel.SelectedDate)
IgnoreChanges = False Report future events to the model.
End If
End Sub
Someone (the user or the program) has changed the scroll bar value.
Public Sub ValueChanged(Value As Integer)
If IgnoreChanges is true, the program made the change.
Do not start an event cascade.
If IgnoreChanges Then Exit Sub
The user made the change. Tell the model about it.
TheModel.SelectedWeek = Value
End Sub
MVC in AppointmentBookThis section lists the data events used by AppointmentBook. It then explains the duties of the applications model, views, and controllers. This section focuses mainly on how the objects interact using the MVC paradigm. It does not describe class details such as how the CalVC object draws the month area. Although these implementation details require quite a bit of Visual Basic code, they are relatively straightforward and are not important for the discussion of the more advanced topic of MVC. You can see all of the code on the compact disk. To make it a bit easier to remember the purposes of the classes, the names of view classes end with the letter V, the names of controller classes end with a C, and view/controller classes have names ending with VC. Events AppointmentBook defines the following data events in the module CalStuff.BAS:
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|