Common Events

As with properties and methods, there are some events that will be responded to most often. Components cover a wide variety of possible Windows controls, so each component will have individual needs. Events specific to forms are not covered here because I covered that information on Day 6. The most commonly used events are listed in Table 8.3.

Table 8.3. Commonly handled component events.

Event

Description

OnChange This event is triggered when a control changes in one way or another.

Exact implementation depends on the component.

OnClick Sent when the component is clicked with either mouse button.

OnDblClick This event occurs when the user double-clicks the component. OnEnter This event occurs when a windowed component receives focus (is activated).

OnExit This event occurs when a windowed component loses focus as the result of the user switching to a different control. It does not occur,

Table 8.3. continued

Event

Description

OnKeyDown

OnKeyPress

OnKeyUp OnMouseDown however, when the user switches forms or switches to another application.

This event is triggered when the user presses a key while the control has focus. Keys include all alphanumeric keys as well as keys such as the arrow keys, Home, End, Ctrl, and so on.

This event is also triggered when the user presses a key, but only when alphanumeric keys or the Tab, backspace, Enter, or Esc keys are pressed.

This event occurs whenever a key is released.

This event is triggered when the mouse button is pressed while over the component. The parameters passed to the event handler give you information on which mouse button was clicked, special keys that were pressed (Alt, Shift, Ctrl), and the x,y coordinate of the mouse pointer when the event occurred.

This event occurs any time the mouse is moved over the control.

This event is triggered when the mouse button is released while over a control. The mouse button must first have been clicked while on the control.

This event is sent any time a component needs repainting. You can respond to this event to do any custom painting a component requires.

OnMouseMove OnMouseUp

OnPaint

Dealing with Mouse Events

Mouse events have a couple peculiarities that you should be aware of. If you are responding just to a mouse click on a component, you will want to keep it simple and only respond to the OnClick event. If you must use OnMouseDown and

OnMouseUp, you should be aware that the OnClick event will be sent as well as the

OnMouseDown and OnMouseUp events. For example, a single click will result in these events occurring (and in this order):

OnMouseDown

OnClick

OnMouseUp

Similarly, when the user double-clicks with the mouse, it could result in the application getting more events than you might think. When a component is double-clicked, the following events occur:

OnMouseDown

OnClick

OnDblClick

OnMouseUp

The point I am trying to make is that you need to take care when responding to both double-click and single-click events for a component. Be aware that you will get four events for a double-click event.

Multiple events will occur when a key is pressed, too. A keypress in an edit control, for instance, will result in OnKeyDown, OnKeyPress, OnChange, and OnKeyUp events occurring.

You can obtain a program called EventTst from http://www.mcp.com/sams/ codecenter.html . This program illustrates the fact that multiple events occur on mouse clicks and keypresses. Run this program and you will see how multiple events can be triggered based on certain user actions.

In just a moment we're going to look at some of the VCL components in more detail. First, however, I want to introduce you to a class that is used by certain VCL components.

TStrings

The TStrings class is a VCL class that manages lists of strings. Several VCL components use instances of TStrings to manage their data (usually text). For example, on Day 7 you used TStrings when you built the ScratchPad application. "Hmmm...I don't recall using a TStrings class," you say. Well, you did, but you just weren't aware of it. Remember when we saved and loaded files? You used something like this:

Memo->Lines->SaveToFile(SaveDialog->FileName);

The Lines property of TMemo is an instance of the TStrings class. The SaveToFile() method of TStrings takes the strings and saves them to a file on disk. You can do the same thing to load a list box from a file on disk or save the contents of a list box to disk. In the case of the

TListBox class, the property that holds the list box items is called Items. For example, do this exercise:

  1. Create a new application and place a ListBox component on the form. Size the list box as desired.
  2. Change the Name property of the list box to ListBox.
  3. Double-click on the background of the form (not on the list box). The Code Editor displays the FormCreate() function.
  4. Type the following code in the FormCreate() function:

char winDir[256], fileName[256]; GetWindowsDirectory(winDir, sizeof(winDir)); sprintf(fileName, "%s\\win.ini", winDir); ListBox->Items->LoadFromFile(fileName);

5. Click the Run button to compile and run the program.

When the program runs, the list box will contain the contents of your WIN.INI file. Using this method, it's easy to load a list box from any ASCII text data file. The ComboBox component also has an Items property, and it works in exactly the same way.

You can add, delete, insert, and move items in a list box, combo box, or memo by calling the Add(), Append(), Delete(), Insert(), and Move() methods of the TStrings class.

Note

How Add() performs depends on the value of the Sorted property. If the Sorted property is set to true, Add() will insert the string where it needs to be in the list of items. If Sorted is false, the new string will be added at the end of the list.

You can clear a component of its contents by calling the Clear() method. You can access an individual string by using the Strings property of TStrings and the array subscript operator. For example, to retrieve the first string in a list of strings, you would use

Each string in a TStrings array contains the string itself and 4 bytes of extra storage. This extra storage can be accessed through the Objects property. You can use the extra storage any way you like. Let's say, for example, that you were creating an owner-drawn list box that displayed bitmaps. You could store the string in the usual way, plus store a pointer to the TBitmap object in the Objects array.

There may be a time when you need to manage a list of strings unrelated to a component. The TStringList class is provided for exactly that purpose. This class works just like TStrings but can be used outside components.

Note

In reality TStrings is what is called an abstract base class. An abstract base class is never used directly, but only serves as a base class from which to derive other classes. Technically, the Lines property of the Memo component is an instance of the TMemoStrings class and not an instance of the TStrings class as I said in this section. I didn't mean to lead you astray, but I thought it was best to make this distinction after the discussion on TStrings rather than confuse you with this information during that discussion.

+1 0

Post a comment