Visual Basic .NET Nightmare or Guide to upgrading a Guru.

Page 4

Your Inheritance!

 

    Okay so we know what OOPs is and we know how we create objects from classes. Sometimes our own classes and most of the time from the .NET Framework's classes.

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

 

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

'Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Name = "Form1"

Me.Text = "Form1"

End Sub

 

Now let's collapse the whole designer thing. Select it by clicking it once, then delete! Don't be scat! It won't blow up any more than any other coding process gone awry! Wow nothing happens, well almost. This brings you to your base class. it's .Text property is empty now.  But if you look at your form's properties in the properties window you'll still see a name. This is the name of the class.  So what do we have now?

 

Public Class Form1

Inherits System.Windows.Forms.Form

End Class

 

Public Sub New()

InitializeComponent()

End Sub

Me.Text = "Fun Form"
Public Class Form1

Inherits System.Windows.Forms.Form

Friend WithEvents Button1 As System.Windows.Forms.Button

Public Sub New()

InitializeComponent()

End Sub

Private Sub InitializeComponent()

Me.Button1 = New System.Windows.Forms.Button()

Me.SuspendLayout()

'Button1

Me.Button1.Location = New System.Drawing.Point(184, 96)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(184, 56)

Me.Button1.TabIndex = 0

Me.Button1.Text = "Button1"

'Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(496, 374)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})

Me.Name = "Form1"

Me.Text = "Fun Form"

Me.ResumeLayout(False)

End Sub

End Class

 

Friend WithEvents Button1 As New System.Windows.Forms.Button()

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

components = New System.ComponentModel.Container()

Me.Text = "Form2"

End Sub

 

So what we found out is although the forms designer code seems scary and weird and ominous at first it's just the IDE writing happy code all by itself and saving you work. We also found out you can create a form from code by hand for the viewing in the designer.  This may be good to know when it comes to make some groovy macros!

 

Page 5