Logo

Controls Messages and Events

 

Controls Events

 

Introduction

Because your computer is made of various objects, these are under the control of three entities:

  • The operating system is first in charge of all the basic operations that must be performed in order for the computer to be usable. Some of these include checking that the various parts (hardware) are ready to be used, displaying the time, checking that a printer is working, etc
  • The person who creates a program, that is you, is in charge of a particular application. For example, as we will learn on this site, when you create a program, you decide what it can do and what it shouldn't do. You also decide if and when it should do something
  • The user, that is, a person who uses the computer or a program that you have written also controls such aspects as when to open a program, when to perform a certain processing

Every time you do something on the computer, the object you interact with composes a message like an email and sends that message to another entity, normally the operating system, that can process that message. This means that, whether you click an object, type something using the keyboard, move the mouse on the screen, or press and click at the same time, a message is composed. Once a message has been composed, it is sent to the OS. Once a message is received, it is analyzed and interpreted. Then a result may be sent to the application from where the message was sent.

The action of sending a message is called an event. There are so many objects you can use on a computer, and/or on an application, that Microsoft Windows is referred to as an event-driven operating system.

After the computer has been launched, it becomes "static" and displays a blank desktop to the user. For example, the computer cannot start a program on its own and it cannot just start typing words on the desktop... (Even if this happens as a result of a script, it doesn't mean that the computer did it on its own, it means that somebody asked it to do it; it is important to understand that the computer is a dumb object that doesn't think. For the computer to do something automatically, something has to ask to do it, whether it is a script you wrote (Windows Script Host(WSH)) or a virus somebody sent you; the computer can't just decide to do something on its own.).

Because there are so many actions that can be performed, the computer cannot predict what should be done next. Therefore, it leaves it up to the user to initiate an action. Again, because there are so many things that could happen at any time, for the computer to do what is needed, it expects good and precise directives. Based on this, Microsoft Windows uses a mechanism like a mail you send to somebody through the post office.

When a message is sent, some conditions must be met for the message to be processed. If a message is not well defined, either the computer would ignore it (best case scenario) or it would crash. For this reason, as a programmer, you need to know what messages you can send (there are are so many messages for different reasons), when you can send a certain message (you cannot just send any message at any time), why (it is important that you know the reason for sending a message, otherwise you may send the right message at the right time but the message cannot be processed because either it is not needed, not necessary, or not efficient).

After a message has been formulated, it must be sent. The action of sending a message is called an event.

In order for a message to be processed, it must provide at least three pieces of information.

 

1. The Sender of a Message

The first piece of information necessary for each message is:

  • WHO sent the message? The computer contains many applications and each application is made of various internal objects. Any application can create a message anytime and send it. Since there can be so many messages sent to the operating system at any time, the application or the object that sent the message must be identified. In some cases the operating system may need to send a response when it has finished processing the message, in some other cases, it needs to identify the object so it would know how the message must be processed.

    Once the sender of a message has been identified, its event is considered as a private matter because only that object is concerned with the message. If this action is not private, it will be qualified as Public.

    Therefore, the coding of each event starts with the Private keyword:

    Private

    An event is really an assignment you ask the application, the form, or the control to perform in response to a particular action happening. You can even ask an object to perform an action based on the behavior of another object or based on the computer doing something (such as singing when the clock displays 12:00 PM). Since there are so many assignments you will give to different components to perform, these actions are called procedures. There are two kinds of procedures: Functions and Sub procedures. Both are written in Visual Basic.

    A Function is a general assignment you write in Visual Basic. This assignment is a resource for other events or actions to get results. For example, if many controls on a form would require a particular value or the result of a particular calculation, you can write a function that all desired events can refer to and get the appropriate result. Since other events and functions would expect a particular result from it, a function is expected to Return a value. We will learn what kind of value a function can return.

    A Sub procedure is a form of assignment that applies to an event associated with particular application, form, or control. It is used to "enclose" the coded assignment you want an event to carry. Since each event is a procedure, now we have:

    Private Sub

    As mentioned already, the object that sent a message must be identified. Therefore, the Sub keyword would be followed by that object:

    Private Sub MessageSender

 

2. The Type of Message

After the sender of a message has been identified, the operating system would need to know:

  • WHAT message was sent? There are various objects in the computer and applications. Some objects can send the same type of message. Some other objects have particular messages that only they can send. Because one type of object can send different types of messages, even if the operating system has been able to identify the sender, it needs to know the type of message that was sent. By convention, and as we will see later on, the name of a message produces the name of the event

By convention, the name of the event is written after the name of the object that sent the message. To distinguish between a control's name and its event, Visual Basic uses a convention of displaying an underscore between them, like this:

Private Sub MessageSender_Event

 

3. The Message Accessories

Once the operating system knows what object sent the message and what that message is, depending on the message, it may need to know:

  • the accessories needed to process the message

While one message may appear easy, such as clicking an object, another message would need additional information such as where (the coordinates of the mouse cursor) the clicking occurred. Therefore, some events will need some values from you. In some situations it will be one value; in this case the accessory is called an argument. Another type of event may need more than one accessory, thus many arguments. Again, depending on the event, this could be one argument, or it could be as many arguments as necessary. When we move on, we will see what events need what argument(s)

The argument or group of arguments that the event may need is listed in parentheses on the right side of the event name, like this:

Private Sub MessageSender_Event(Argument1, Argument2, Argument_n)

Even if an event doesn't need an argument, you must provide empty parentheses, like this:

Private Sub MessageSender_Event()
 

The Body of an Event

The subject of the assignment is called the body. It starts with the Private Sub line and ends with a line identified as End Sub. Between these two lines, you specify what the event is supposed to accomplish. Some events just need to know what you want them to do, for example, you can ask a button to close a form when that button is clicked. On the other hand, when the user clicks somewhere on an object, you could ask the object to display something depending on the button the user clicked. In this case, the event code would like to know what button was clicked.

The above introduction was meant to show you what a coded event looks like. Microsoft Visual Basic will do a lot of work for you behind the scenes. For example, it will always set a beginning and end event for you. It will also specify the names (and types) of arguments for you. This means that Visual Basic always writes a skeleton code for your event; you can then customize it as you see fit.

 

Practical Learning: Starting an Event

  1. Start Microsoft Visual Basic 6.0
  2. On the opening dialog box, make sure Standard EXE is selected and click Open
  3. On the main menu, click View -> Code
  4. In the empty window, type Private Sub Form_Click
     
  5. Press Enter
  6. Notice that Visual Basic added the parentheses and the End Sub line
  7. In the Code window, notice that the Object combo box is displaying Form
  8. Click the arrow of the Object combo box to display its list: 
     
  9. Click the arrow of the Procedure combo box and, in the list, click MouseDown 
     
  10. Notice the section of the MouseDown event. Also notice that this event has a few arguments
  11. We will eventually learn what the writings of these arguments mean.
    In the Object combo box, notice that Text1 is still displaying.
  12. In the Code window, click on one of the form events.
  13. Notice that the Object combo box has automatically changed.

Categories of Events

 

The Keyboard Events

Word processing consists of manipulating text and characters on your computer until you get the fantastic result you long for. To display these characters, you press some keys on your keyboard. If the application is configured to receive text, your pressing actions will display characters on the screen. 

The keyboard is also used to perform various other actions such as accepting what a dialog box displays or dismissing one.

When you press the keys on your keyboard, you are sending keyboard events.
 

The Click Event 

The mouse has become a very important object of computer use. It is used by pressing one of its buttons.

The Double-Click Event

When you press the left mouse button once, the event is called the Click event. Another action you can perform is to click the button twice but very fast. This is referred to as double-clicking.

The Right-Click Event

Since Microsoft Windows 95, the mouse buttons are intensely used and both buttons have become important object of the computer daily use. By default, the users click the left mouse button for all routine work. The other button, the right one, is used in various circumstances, such as displaying a context menu.

The Right-Click action is performed by clicking the right mouse button. The actions that the right-clicking produce completely depend on the programmer.

When writing code for the right-click button, you will have to find out what button was clicked, and then write code accordingly.

The Focus Events

Microsoft Windows operating systems allow you to work on more that one application at the same time. They also allow you to work on many forms as the computer can handle. But only one application can receive instructions at a given time. For example, although you can edit text on a word processor while a spreadsheet is running in the background, you can only perform one action at a time. You have the ability to display the desired application when needed. This applies to applications.

Many dialog boxes have more than one input control, such as the Font dialog box we used earlier. Although all these controls are available, you can work from only one control at a time.

If many applications are running on your computer while you are working, the program that is currently being edited or receiving input from you is said to have focus. If you have two forms, you can open both of them but at a given time, you can work on only one of them. On a form that is equipped with many controls, only one control can be changed at a time; such a control is said to have focus.

The application or the form that has focus usually has its title bar with the active window color as set in Control Panel. In a form with many controls, the one that has focus will usually have a cursor or a dotted line around its selection.

When an application, a form, or a control has focus, Microsoft Visual Basic applies the GotFocus event. If the focus shifts to another application, form, or control, Microsoft applies the LostFocus to the same component.

 

Launching and Loading A Program

Your computer is filled with a lot of programs, some of which you use all the time, some of which you use some time to time, and some of which you probably never or rarely use. Since the computer can't predict what you want to do, it keeps all these programs in a storage area called the hard drive. They simply stay there and wait. When you want to use one of these programs, you ask the computer to bring it to you. There is another, temporary, storage area in your computer called the memory (RAM). This is where the computer puts the programs you are using currently. When, you decide to use a program, the computer brings it up. When you have finished using the program, the computer puts it back into the hard drive. Of course, the computer can put as many programs as possible into the memory (or as many as the capacities of the computer allow it).

To use a program, you have to "Load" it into memory (the computer will do it for you). And to load a program you have to select and start it. That's why you need to find it and...

When a program starts, it is said to be launched. Visual Basic considers that the program is Opening. It takes just a few seconds for a program to launch or open. Some of them display a "Splash Screen" while they are launching. After the program has been launched, it is said to be Loaded. Once a program is loaded, it is said to be running. Actually, loaded and running would mean the same thing, especially in Visual Basic.

Practical Learning: Loading a Program

  1. To start WordPad, click Start -> (All) Programs -> Accessories -> WordPad.

  2. While the program is Opening, you should see a brief splash screen (since WordPad is a small application (not by programming standard, but as far as the users are concerned), the splash screen might not appear, or it would be very brief):
     

  3. Notice that after launching, WordPad is opened. Visual Basic says that WordPad is Loaded:
     

     

 

Previous Copyright © 2002-2005 FunctionX, Inc. Next