UserGuide

Subclassing Examples

From Xojo Documentation

These are some examples of control subclasses.

SelectablePopupMenu

As an example, you can create a PopupMenu subclass that adds a method that can be used to select an existing item in the PopupMenu. Create a new class, called SelectablePopupMenu, and set its Super to PopupMenu. On this class, add a new public method and call it “SelectRow” with the parameter “value As String”.

This method will loop through all the rows in the PopupMenu and check to see if the text for a row matches the supplied value. If the text matches, it will select the row and return. If no matches are found, then nothing is selected.

This is the code for the SelectRow method:

For i As Integer = 0 To Self.ListCount - 1
If Self.List(i) = value Then
// Found a row with matching
// text, so select it
Self.ListIndex = i
Return
End If
Next

// Select nothing
Self.ListIndex = -1

MonthPopup

Here is another example. Suppose you want to create a PopupMenu that, by default, displays the names of the months of the year, with the current month selected. To do this, create a new class, call it MonthPopup, and choose PopupMenu as its super class.

Now you can add an Open event handler to MonthPopup and add the month names, the heading, and code that selects the appropriate month in the list. In this case, the Open event handler is:

Self.AddRow("January")
Self.AddRow("February")
Self.AddRow("March")
Self.AddRow("April")
Self.AddRow("May")
Self.AddRow("June")
Self.AddRow("July")
Self.AddRow("August")
Self.AddRow("September")
Self.AddRow("October")
Self.AddRow("November")
Self.AddRow("December")

// Select the current month
Dim d As New Date
Self.ListIndex = d.Month - 1

To add an instance of the MonthPopup class to a window, switch to the window’s Window Editor and then drag MonthPopup from the Navigator to the Window Editor. When you run the application, the Open event handler will run and populate the PopupMenu with the months of the year, selecting the current month.

Note: Once created, custom control classes can be exported as self-contained objects that can be used in other projects. Just right-click (Control-click on Mac) on the custom control class in the Navigator and choose Export... from the contextual menu.

Subclasses are Classes

Remember, subclasses are classes. They are called subclasses to differentiate them from the original classes and emphasize the point that they inherit the properties, events, and methods of their parent class. Because subclasses are classes, they can be the super class to other subclasses. For example, suppose you had a NumbersOnlyTextField subclass, but now you need a TextField that allows only numbers within a certain range. You could duplicate the NumbersOnlyTextField subclass and then modify its code.

However, this would make your project larger and more difficult to maintain. If you found a bug in the code of the NumbersOnlyTextField, you would have to remember that you used that code in other places as well, track them down, and fix them. A more efficient way is to create a new subclass and choose the NumbersOnlyTextField as its super class. The new subclass (let’s call it “NumberRangeTextField”) would utilize all of the properties, events, and methods of its super class. However, you can add code to the TextChange event handler that allows only numbers within a specific range.

See Also

UserGuide:Object-Oriented Programming, UserGuide:Desktop Custom Controls topics