Windows Forms and Controls

The Complete Form Class

By now you will have noticed that the code behind the form contains a lot more than just the button events we accessed. In fact it contains everything needed to create what we created using the visual designer. We could have opened notepad, and typed all of it in ourselves, then compiled it and it would have executed just fine. In fact lets do just that but let’s not type it in. We can copy and paste what we need. We don’t need some of the designer specific code created by VS.Net so to be clear here’s what we’ll use:-

First off we need to create our references and declare the form Class


Imports System
Imports System.Windows.Forms
Imports System.Drawing

Public Class frmMain
   Inherits System.Windows.Forms.Form

This declares the form and specifies that it Inherits from the form namespace, i.e. it is a form.

Next up we declare our controls outside of the following procedures


   Friend WithEvents cbCancel As System.Windows.Forms.Button

   Friend WithEvents cbAccept As System.Windows.Forms.Button

   Friend WithEvents txtEnteredText As System.Windows.Forms.TextBox

   Friend WithEvents lblEnterText As System.Windows.Forms.Label

The Friend keyword allows us access to them from the second instance of the form we create. You can use Private if you don’t want to directly access them from inherited forms.

   Shared Sub Main()

       System.Windows.Forms.Application.Run(New frmMain())

   End Sub

This is the Main procedure that the compiler looks for in any project.It’s the first thing that runs when you launch the exe.

You might also like...

Comments

About the author

Brian O'Connell Ireland

Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web appli...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“To iterate is human, to recurse divine” - L. Peter Deutsch