Library tutorials & articles

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.

Comments

  1. 01 Aug 2008 at 19:22

     Only 5 years after the last post eh :) Anyway because things may have changed I recommend you look at the microsoft docs on this - http://msdn.microsoft.com/en-us/library/ms229597(VS.80).aspx

  2. 22 Oct 2003 at 16:26

    It may not work with mdi children.

  3. 11 Jul 2003 at 03:26

    I use VB.Net (framework 1.0.3705) on Windows 2000, and I noticed the 'MinimumSize' property doesn't seem to have any effect on my forms (MDIChildren). Does that property really prevent users from resizing a form to a smaller size than the MinimumSize? Any other experiences?


    Thank you,

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Windows Forms and Controls.

Leave a comment

Sign in or Join us (it's free).

Brian O'Connell 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 applicat...

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

We'd love to hear what you think! Submit ideas or give us feedback