Form Initialization and Termination

A form needs a way of initializing its data before it is displayed on the screen. It also needs a way of cleaning up after itself when the form goes away. In .NET, forms initialize themselves in the New method. Because the New method is called when a new instance of a form is created, you can put your code in the New method (or create another method that is called from the New method) in order to initialize your form.

When you create a new form using Visual Studio .NET, it generates the following code:

Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "

    Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() ...

Get Visual Basic® .NET by Example 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.