Superclassing a Control

Superclassing a control is similar to superclassing a WinForm. First, you write a new class that inherits from the control that you want to superclass; for the following example, it will be a button control. Next, you override the WndProc for the control and add your own functionality.

The application

This example application contains a text box and a superclassed button control on the WinForm. Table 22-6 lists the nondefault properties of the form and its controls. Figure 22-6 presents a screenshot of the application. When you right-click the superclassed button, a message box is displayed indicating that the button was, in fact, right-clicked.

Table 22-6. Nondefault Properties of the Form and Its Controls

Object

Property Name

Property Value

Form

Caption

“VB.NET Control Superclassing Example”

Form

Height

357

Form

Width

328

SCButton

Caption

“SC”

SCButton

Top

170

SCButton

Left

8

SCButton

Height

40

SCButton

Width

40

The application containing the superclassed button control

Figure 22-6. The application containing the superclassed button control

The .VB file

Example 22-8 presents the code for this technique. The important modifications to the code are in boldface.

Example 22-8. The Form1.vb File for Superclassing a Button Control

Imports System.ComponentModel Imports System.Drawing Imports System.WinForms Public Class Form1 Inherits System.WinForms.Form Public Sub New( ) MyBase.New( ) Form1 = ...

Get Subclassing and Hooking with Visual Basic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.