These are some examples of control subclasses. Subclassing can be used in all project types.
Here is another example. Suppose you want to create a DesktopPopupMenu 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 Opening 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 Opening 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 Var d As New DateTime Self.SelectedRowIndex = 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 Opening 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.
Desktop Custom Controls topic