VB.Net Form Subclassing

By | 2022-09-21

In object-oriented programming, subclassing is the process of creating a new class that is a modified version of an existing class. The new class is called the subclass, and the existing class is the superclass.

The subclass inherits all of the properties and methods of the superclass, and can also have additional properties and methods of its own. This allows you to reuse code and create more specialized versions of a class without having to rewrite all of the code from scratch.

For example, you might have a Person class with properties such as NameAge, and Address. You might then create a Student subclass that inherits from the Person class and adds additional properties such as StudentID and GPA. The Student class would then have all of the properties and methods of the Person class, as well as the additional properties and methods that are specific to students.

Subclassing is a powerful concept that allows you to create a hierarchy of related classes and take advantage of inheritance and polymorphism in your code.

To subclass a form in VB.NET, you can use the Inherits keyword to specify that the form is a subclass of another form. Here is an example of how you can create a subclass of a form:

Public Class MyForm
   Inherits System.Windows.Forms.Form

   ' Add your form's controls and code here

End Class

You can then use the MyForm class just like any other form in your VB.NET application.

If you want to override any methods or properties of the base form class, you can do so by using the Overrides keyword. For example:

Public Class MyForm
   Inherits System.Windows.Forms.Form

   Public Overrides Sub OnLoad(e As EventArgs)
      ' Add your code to execute when the form is loaded here
   End Sub

End Class

You can also use the MyBase keyword to access the base form class from within the subclass. For example:

Public Class MyForm
   Inherits System.Windows.Forms.Form

   Public Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
      ' Add your code to execute after the base form's OnLoad method is called
   End Sub
End Class

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.