Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
FAQ Title
Question: Why doesn't my Form Load code work?
 Join the Discussion
Add to the FAQ!
Discuss this at the
About.Com Forum
 

Jerry asked,

There are two Visual Basic 6 methods Line and Circle that do not seem to work. For example:

Line(20,20)-(100,150)
Circle(100,75),50

I'm using Form Load and I have tried all alternatives in the property ScaleMode but the line doesn't show. When I look at it using VB 6 Debug, it seems that the statements have been executed.

The problem is that when you execute these commands in the Form Load event, the Form doesn't really exist yet. (It's mid way through the process of being created.) So the commands execute, but when the Form is finally fully "there" these objects are not displayed as part of it.

To understand this point more fully, create an empty project and insert these events into it:

Private Sub Form_Activate()
   Debug.Print "Activate"
End Sub

Private Sub Form_Paint()
   Debug.Print "Paint"
End Sub

Private Sub Form_GotFocus()
   Debug.Print "GotFocus"
End Sub

Private Sub Form_Resize()
   Debug.Print "Resize"
End Sub

Private Sub Form_Initialize()
   Debug.Print "Initialize"
End Sub

Private Sub Form_Load()
   Debug.Print "Load"
End Sub

When you run this program you should see this result in the Immediate window.

Initialize
Load
Resize
Activate
GotFocus
Paint

This is the sequence that the events actually execute in. (Notice that their sequence in the program code is deliberately different. This is to emphasize that VB has a specific execution sequence that is not related in any way to the sequence of the Sub coding.)

In the Microsoft Help examples, Circle and Line are illustrated in the Form Click event to avoid this complication because the Form is completely loaded at this point. An alternative that would also work in your case, however, would be to place the Line and Circle statements in the Paint event.

Event sequence is one of the key things that you have to be aware of when writing event driven code. Using Debug is one of the best ways to be sure of the sequence that is actually taking place.

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.