All of the objects displayed on the Controls window are objects of a special type called “controls”. Xojo 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 Label 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 Label 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 Label has events such as “MouseDown”, “MouseDrag”, and “MouseUp” that it can respond to. The Label 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 Label Class.
Creating a Custom Class
We are going to create a clickable object based on the Label 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 click on the Insert icon on the toolbar and select “Class” from menu that comes up. 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 Label. At this point our new URLtext Class has all of the properties, events, and methods of any other Label.
The custom class “URLtext” has been defined as a subclass of the Label class.
By adding some code to several of URLtext’s events and by adding two properties to URLtext we transform a Label 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 add a Label control. Select the Label control. In the Super field enter “URLText.
Select the URLtext object you just added to the window and look at its properties in the Inspector. In Behavior section at the bottom and you should see the two properties you added (theURL and theCaption). 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.