Custom Classes

All of the objects displayed on the Controls window are objects of a special type called "controls". REALbasic objects are also known as classes or subclasses. Classes and subclasses are defined by the particular properties, events, and methods that they have.

The most basic class is the Object class which has no properties, events, or methods of it own. The Control class is a subclass of the Object class and it has a few properties such as "Name", a couple of events; "Open" and "Close" and one method (Close). The StaticText Class is a subclass of the Object Class and, as such, it inherits all of the Object Class' properties, events, and methods. Besides its inherited properties, the StaticText Class has some properties of its own such as "Caption", "TextColor", "TextSize", and "Underline". In addition to the "Open" and "Close" events inherited from its parent or super class, the Object Class, a StaticText has events such as "MouseDown", "MouseDrag", and "MouseUp" that it can respond to. The StaticText Class does not have any methods other than "Close" method that it inherits from the Object Class.

The idea of subclasses inheriting characteristics from their parent or super classes is very important. Because of this inheritance we can create custom classes of our own based on any existing class.

In this tutorial we are going to create a custom class based on the StaticText Class.

Creating a Custom Class

We are going to create a clickable object based on the StaticText Class called "URLtext". When we are done we will have an object that we can add to windows that, when clicked, will open a web address in a browser or open a pre-addressed email message in the current default email program.

To create a custom class you select "New Class" from the REALbasic File menu. A new class called "Class1" appears in the project window. Using Class1's Properties window we change its name to URLtext. Also in the Properties window we enter its Super as StaticText. At this point our new URLtext Class has all of the properties, events, and methods of any other StaticText.

The custom class "URLtext" has been defined as a subclass of the StaticText class.

HandCursor is a cursor resource that has also been added to the project.

By adding some code to several of URLtext's events and by adding two properties to URLtext we transform a StaticText Class into something unique.

Above you can see that two properties have been add to URLtext. "theCaption" will store the caption to be displayed when the project is run and "theURL" will store the web address to be opened when a user clicks on the URLtext object.

To add an URLtext to a window drag it from the Project Window to any of the project's windows. Select the URLtext object you just added to the window and look at its properties in the Properties Window

The added properties show up in the Behavior section of the Properties Window.

Enter values for theURL and theCaption as seen here.

Two URLtexts have been added to this window. When the project is running their captions are displayed. If a user clicks on one of these URLtexts the code in the URLtext's MouseUp event will open the web address.

To examine the code in URLtext's MouseDown, MouseEnter, and MouseExit event download the project and double click on the URLtext object in the Project Window.

Download the project