February 25, 2021
Hot Topics:

Book Review: Beginning Visual Basic 6 Objects

  • By James Limm
  • Send Email »
  • More Articles »

Ok, let's get some forms up so we can actually use our clsTitles class.

Try It Out – Using Our clsTitles Class

1 Our application is going to be a traditional MDI (Multiple Document Interface) application.

Go ahead and add an MDI form to your project. Once again, if any of this form/module adding stuff seems a little strange then you'd do well to check out my last book, Beginning Visual Basic 6.

Since this is an MDI form, make it big enough to be able to hold the other windows that it's going to contain. If you make it too small, its child windows won't be completely visible within it - which isn't great for our users.

2 Set the name of this form to mdiMain. Then select Properties from the Project menu and use the General tab dialog to set the start-up form to be this MDI form, as in the screenshot here:

For the sake of completeness, you should also set up the project name to BiblioTech, just as I have done here.

3 Okay, so we have an MDI form in the application, and we have the clsTitles class that neatly wraps up the Titles table. We now need to add a menu to our MDI form that will allow us to access the other forms that we'll be adding to the application.

Add the following menu headings to the MDI form: File, Titles and Authors. Remember, you can do this in Visual Basic by selecting the MDI form in design mode, and then pressing Ctrl and E to bring up the menu editor. There's no need to worry about giving these things neat names since we aren't going to add any code to respond to events on them - it's the submenu items that we're more concerned with. I called mine mnuFile, mnuTitles, and mnuAuthors, respectively:

4 Use the menu editor again to add a menu item under the File menu. Set this new menu option caption to Exit and then name it mnuFExit. Underneath the Titles heading, put a Browse item in there and call it mnuTBrowse. Finally, underneath the Authors heading, put a Maintain item on there and call it mnuAMaintain:

5 The next step is to get the actual Title Browsing form up and ready, with code to start it up from the MDI.

As you've probably noticed, there's a lot here for us to do in this phase of our application development - such are the toils of creating a user interface. I'm not an unsympathetic guy! Take a break for this step, and just read what I have to say about user interfaces and object interfaces.

User Interfaces and Object Interfaces

While we're in the middle of building our user interface for DataDamage Inc, I'd like to point out to you how closely related user interfaces and object interfaces are to one another. This is really interesting.

Object-oriented programming does, in fact, fit in really well with the way Windows works. At the root of any programming problem there's a hideous mess of data and an equally unnerving set of code to manipulate that data. Dump that code in a class, though, and we can design a nice interface to that code and data - something more meaningful and more in tune with the real-world problem. Now in terms of the user interface of a Windows application, we usually provide graphical controls on a form that hook into some or all of the interfaces in our classes and objects. This is a case of a user interface marrying an object interface, if you like.

Our Title Browsing form is going to provide users of the application with much the same interface as we can deal with in our objects. For Title Browsing, the interface will of course be in the form of buttons and text boxes, while our classes (such as clsTitles) and then our objects will interface through properties and methods.

6 Let's get that Title Browsing form up and running. Add a standard form to the project, call it frmTitle, and set the MDIChild property on the form to True. By default, at runtime Windows will create this form in a default size, which can totally screw up our user interface.

Rather than write a lot of resize code to make the controls and the form appear as we intended at runtime, simply set the Borderstyle property to Fixed Dialog.

7 When you've done these things, go ahead and drop some controls on the form so that it looks like this:

Set the names of the text boxes to txtTitle, txtYear_Published, txtISBN, txtPubID, txtDescription, txtNotes, txtSubject, txtComments - names that fit quite nicely with the actual properties on the class. Likewise, set the names of the buttons to cmdFirst, cmdPrevious, cmdNext and cmdLast. Notice that I've also set the Caption property of the form to Title Browser - it would be rude not to, after all.

8 In order to get this working, we need to add a little code. Now the important thing to note here is that the hard work is done. We have a class with a very neat interface, and all we now need to do is to literally link the user interface elements to the class interface elements.

Click on the new form to select it, then hit F7 on the keyboard to open up its code window. When it appears, key this code in:

Option Explicit
Private WithEvents Titles As clsTitles

Private Sub cmdFirst_Click()
End Sub

Private Sub cmdLast_Click()
End Sub

Private Sub cmdNext_Click()
End Sub

Private Sub cmdPrevious_Click()
End Sub

Private Sub Form_Load()
Set Titles = New clsTitles
End Sub

Private Sub Titles_DataChanged()

' When the data changes, we need to load up the text
' boxes on the form

With Titles
txtTitle = .strTitle
txtYear_Published = .intYear_Published
txtISBN = .strISBN
txtPubID = .lngPubID
txtDescription = .strDescription
txtNotes = .strNotes
txtSubject = .strSubject
txtComments = .strComments
End With

End Sub

Incredibly simple huh? We just call single methods from the click events on those buttons. Take the First button, for example:

Private Sub cmdFirst_Click()
End Sub

The important part of this code, though, is the event handling.

It is most fortunate that Visual Basic 6 lets us create our own custom events. In the early days of VB, we could write an application that utilized classes, but we would still need to write nasty blocks of code to wait for conditions on our objects to occur. In Visual Basic 6 we can just raise a custom event.

We declared the DataChanged event, if you recall, right at the beginning of our clsTitles class like this:

Public Event DataChanged()

which is just a little note telling VB that there's a new event that can occur, and that it's called DataChanged. This event is triggered by the programmer with the RaiseEvent command; in our case, in the Reload_Members method within the clsTitles class:

RaiseEvent DataChanged

No great problems there I hope. However, dealing with the events in code elsewhere in the project does require a little thought. We need to declare the object WithEvents; this means that when we declare an instance of our Titles object, we just do the following (as you'll see right at the top of the code we just added to our Title Browsing form):

Private WithEvents Titles As clsTitles

At this point, the Titles object (which is derived from the clsTitles class) is available in the object drop down at the top of the code window, just as if it were a control on the form.

Using events like this greatly reduces the amount of code in the application. No more endless loops waiting for a specific condition to occur! There is a catch though, as we can see in the form's Form_Load event: when we declare an object WithEvents, we can't create the object at that point in time! So, where we would normally say :

Dim MyObject As New clsMyClass

we actually have to drop the New keyword when we use WithEvents. This isn't a big problem, but it can cause us problems if we forget to actually create the object! That's where the Form_Load event comes in:

Private Sub Form_Load()
Set Titles = New clsTitles
End Sub

Here the object is actually created, and stored in our Titles object variable, which we declared earlier.

The beauty of objects and events is that they let us deal with the flow of our program in the same way that we might deal with the flow of events in everyday life. Very few people wait specifically for the phone to ring day in, day out: they prefer instead to respond to the phone's ring event.

9 Finally, we need to add some code to get the MDI form to load up the Titles browsing form. From the Project explorer window, double click on the MDI form, mdiMain, and select the Browse option from underneath the Titles menu. The code window will appear. Now code the event so that it looks like this:

Private Sub mnuTBrowse_Click()
Load frmTitle
End Sub

10 At this stage, our program works just as we would expect it to. Run it up and have a play with the Title Browsing form to get a feel for what it's doing. There's a lot going on behind the scenes, however, that demonstrates some of the benefits of the object way of working. Just to recap:

The application is now independent of its data

Black-box development simplifies GUI code

Custom events reduce code size at front end

But there's more good news: Visual Basic's support for object-orientation goes far beyond writing custom events, properties and methods. Let's take a look...

Page 6 of 9

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date