Monday, November 5, 2012

Multi Forms in VB.NET

Multi Forms in VB.NET

Tired of having a lot of different forms in your VB.NET projects? You wish the work together as a single program instead of individual and separated class form? Here is one way to help you.
Last time, I show you how you could make a class act like a module. A module helps pour easily share variable between anything in your project. If you want to read the previous posts, you could find them by clicking here. Otherwise, continue reading this one.

Now, let's try to extend a little bit our class with shared stuff in it. Take a look on the code here:
Solution Explorer previews:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Module1.mFormA = Me 'sending the main form (this form) to mFormA

        Module1.mFormB = New Form 'create a form realy from nothing and put a Label
        Module1.mFormB.Text = "Module1.mFormB.Name.ToString"
        Dim myLabel As System.Windows.Forms.Label
        myLabel = New Label
        myLabel.Text = "my New Label"

        Module1.mFormC = New Form2 'create form2 from form2.vb and put it in class Module1 as shared form
        Module1.mFormC.Text = "Module1.mFormC.Name.ToString"

        Dim oForm3 As New Form3 ''create form3 from form3.vb but only put it in the collection

        Dim oForm4a As New Form4 'create form4 from form4.vb 3 times (1st one)

        Dim oForm4b As New Form4 'create form4 from form4.vb 3 times (2nd one)

        Dim oForm4c As New Form4 'create form4 from form4.vb 3 times (3rd one)

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Module1.iNumber = Module1.iNumber + 1 'don't need to initiate the variable because is shared
        TextBox1.Text = Module1.iNumber 'the variable keep raising each time youpress button1
    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    End Sub

End Class

Public Class Module1
    Public Shared iNumber As Integer 'Make this class act like a Module because of Shared
    Public Shared mFormA As Form ' this will be form1 for this exemple
    Public Shared mFormB As Form ' regular vanilla form from scratch
    Public Shared mFormC As Form2 'created from the Visual Studio Forms designer
    Public Shared mForms As New Collection
    ''' <summary>
    ''' very simple and very limited
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub close_all()
    End Sub
    ''' <summary>
    ''' all forms in a collection
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub close_all2()
        For Each mform As Form In mForms
            mform.Close() ' for each form in collection, close.
    End Sub
End Class

I hope the code speak for it self. I put some little comments a little bit everywhere.

Basically, I put a lot of form in a collection. Each form is created differently. All forms are created with the most practical ways.
When you start the program, it shows all the forms. With one single shared method, you could close them all by pressing the Button2 on the first form.

The purpose of this article is not to show you how to close all forms, is really to use a single class to control multiples forms. Not only that, it become easy to access anything in each of those forms. No need to create buffer variables over an over.

Sometimes, too many forms make your project hard to works with.


The program I love to use, buy it:

No comments:

Post a Comment