Managing windows on a visual basic application has changed a lot since version 6.0, back then you had a direct reference to windows as instantiated objects, but in .NET this has changed and even if somebody disagrees , I think is for the best.
However there are ways to obtain the a similar functionality when handling windows, On this occasion we will see these tricks to handle MDI forms.
As we know MDI applications consist on a Parent Form inside which the other forms called Children are shown.
A very common problem we face with MDI forms is not being able to know if the children forma are already created, and if they are created, how to pass a message or have a direct reference to objects inside of them.
Besides this solution comes handy when your need a children form to communicate to another children form. And to control that there are no duplicate children forms, if that's what you wish. Well, the implementation is as follows:
First step - Shared Module:
As a first step you must create a module in which you can declare the form objects you want to use, for example suppose we have two children forms, frm_studet and frm_course :
Module children_forms Public objstudent As frm_student Public objcourse As frm_course End Module
In this case we have two forms to control.
Second Step - Tunning the children forms:
To be able to control if a children form is instantiated, it's necessary that once closed there instantiation passes to be nothing, We can achieve this asigning "nothing" to the form object on the Closing event of the form. For example on the frm_student form.
Private Sub frm_student_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing children_forms.frm_student.Dispose() children_forms.frm_student= Nothing End Sub
Third Step - How to open the Forms:
When you want to open one of this forms you have to do it taking into consideration the object declared on the children_forms module, following is the example to open the courses forms from the main Window:
If children_forms.frm_course Is Nothing Then children_forms.frm_course= New frm_course children_forms.frm_course.MdiParent = Me End If children_forms.frm_course.Show() children_forms.frm_course.BringToFront ()
Practical Use - what is this for?:
Using this technique we achieve:
- That only one instance of every children form is created. If the form is already created it only brings it to the front.
- We can directly point to any children form or its public objects, from the Main Form, but also from any other form on the application, using the following:
Direct reference to the txtCode.text on the frm_student form (assuming the textbox is declared public):
children_forms.frm_student.txtCode.Text
Direct reference to the frm_course Window:
children_forms.frm_course
That allow us to have full communication with our children forms, and between children forms.
And that, my Friend, It's all!
No comments:
Post a Comment