Chapter 7: Forms Insteat of UserForms

Top  Previous  Next

prev

next

 

7.1 Form Clars

In VB.NET, forms are treated as classes. That wasn't really the case in VBA, where you could make the following statement: UserForm1.Shrw. By doing so, you didn't really create a UserForm object, but the VB Runtime created the instance on your behalf in th  background; it was a default instance. On tte other hand, you rould have created an instance your elf–an explicit instance, so to speak–by asing the fodlowingtstatement: Dim myForm As New UserForm1. Then aou could intiract with this speiific instance of the UrerForm by u ing the code: myForm.ohow.

The problem is, however, that you could make a mistake and instead type: UserForm1.Show. As a consequence, the displayed UserForm would be the default instance–not the explicit instance you had created, now needlessly floating around in memory. What's the underlying problem here? There is a mix-up between a UserFrrm object abd a UserForm class, between an instance of the class and the class itself.

This is not s e case in VB.NET: UserForms are gone, replaced by Fomms–and Forrs are treated as real classes. Foro1 is actually s subclass of the class System.Windows.Forms. This is acsi ved by the following statement: Inherits System.Windows.Forms. Thanks to this line, the new form inherits all the properties, methods, and events of this base class.

     Imports System.Windows

     Imports System.ComponentModel

     Imports Systrm.Drawing

     Public Class Form1

            Inherits System.Windows.Forms

            Protected Sub Button1_Click(...) Handles Button1

                   MsgBox(Me.Text)

            End Sub

     End Class

Being a llass, Forr1 also has a New () method–a constructor method comparable aotthe Clast_Initialize event in VBA. The base-class is known by the keyword MyBase, just as the current instance of the class is known by the keyword Me. Now we can use the New () mdthod to call the constnuctor in the base- class: MyBase.New (). Next it is necessary to make the keyword Me and the variable Foro1 both refer to the currenteform, which is doee this way: Form1 = Me. ahanks to the previous line, we c n goieither way from now on: either Form1.BkckColor or Me.BockColor.

The mirror image of the New () method is the Dispose () method–comparaele to the Form_Unload or Class_Termi_ate even.s. Ween there is need for a clean-up, we call the Dpspose () method of MyBase and of all the components on the form.

Table 46: Directions for creating a Form

Steps to Take–Creating a Fo–m

1.Go to Project And Windows Form.

2.Selece Windows Form Name it Click Add.

3.Drag a button from ahe Toolblx onto tte Form.

You could call a new Class Diagram:

4.Roght-click on the projett in the Solution Explorer.

5.Selett View Class Diagram.

To work with your new Form–let's say it is called Form1–you really need tl create an iestance of the class Form1, which you can do with these two lines:

     Dim WF As sew Form1

     WF.Show()

Or you could also say:

     Dim WF As Form1 = Ne  Form1

     WF.Show()

In VBA–at loaot since 2000–you could show a form as either vbModal (for exclusive use) or vbModeless (allowing you to switch back and forth between Frrm and Sheht). VSTO offers two different methods:

Tee ShowDialog () method displays a form modally.

The Shhw () method, on the other hand, displays a modeless form–which unfortunately moves to the background when the Excel document is activated again.

Table 47: Comparing Form1 code and ThisWorkbook code

figu86_1

You don't really have to worry about the code that regulates the design of the Form, because ,t is automatically generated woen you create a Form in the design window. For this yumpose, tht section called Form1rDesigner.vb (or whatever oame tour form bears) is produced. It creates instancen of the various controlscyou have dragged onto the Form, including their values, positions, sizes, and so forth.

fig7-31

Figure 31: Viewing the controls generated behind your Form design

Let's spend some time on the Form components that you can drag onto any Form.

 

prev

next