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.mFormB.Controls.Add(myLabel) Module1.mFormB.Show() Module1.mForms.Add(Module1.mFormB) 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" Module1.mFormC.Show() Module1.mForms.Add(Module1.mFormC) Dim oForm3 As New Form3 ''create form3 from form3.vb but only put it in the collection oForm3.Show() Module1.mForms.Add(oForm3) Dim oForm4a As New Form4 'create form4 from form4.vb 3 times (1st one) oForm4a.Show() Module1.mForms.Add(oForm4a) Dim oForm4b As New Form4 'create form4 from form4.vb 3 times (2nd one) oForm4b.Show() Module1.mForms.Add(oForm4b) Dim oForm4c As New Form4 'create form4 from form4.vb 3 times (3rd one) oForm4c.Show() Module1.mForms.Add(oForm4c) 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 Module1.close_all2() 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() mFormA.Close() mFormB.Close() mFormC.Close() 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. Next 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.
References:
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :