iSnare.com - Free Content Articles Directory
Authors Contents [Advanced Search][Add OpenSearch][Job Search]
Distribute your articles to thousands of article sites for only $2 and below! Read more...

Index  Computers and Technology
 

VB.Net: Dynamic Usage Of Eventhandlers

 
[ Contact the Author] [ Send to a Friend] [ Article Publisher] [Make PDF] [ Print] [ Bookmark & Share]
 
Read our Terms of Service before reprinting this article. The submitter specified above has claimed the rights to this article.
Thomas Kaloyani

WithEvents and Handles clause requires form us to declare the object variable and the event handler as we write our code, so linkage is created upon compilation. On the other hand, with AddHandler and RemoveHandler, linkage is created and removed at runtime, which is more flexible.

Let's assume that we want to load several MDI child forms, allowing each of them to be loaded only once, and of course to know when one of the child forms is closed. Since we have several forms to load we would like to use the AddHandler and RemoveHandler keywords so we can be flexible and write the minimal code we can.

Let's get dirty.

1. In each MDI child form we have to declare a public event.
Public Event FormClosed(ByVal f As Form)

2. In each MDI child form we have to use the Form_Closed method which handles the MyBase.Closed class and raise the FormClosed event.

Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Closed
RaiseEvent FormClosed(Me)
End Sub

3. On our MDI form we need to declare two member variables. The first's of type Form and the second's type is ArrayList.
Private m_f(0) as Form
Private m_sLoadedChildForms As New ArrayList

4. We need to implement a method the will search the MDI child forms that are loaded. We'll also use this method when we unload the MDI child forms.

Private Function SearchChildForm(ByVal strSearchForm As String, _Optional ByVal idxEventHandler As Long = -1) As Long
Dim i As Long = 0
For i = 0 To m_sLoadedForms.Count - 1
If m_sLoadedForms.Item(i) = strSearchForm Then
Dim j As Long = 0
For j = m_f.GetLowerBound(0) To m_f.GetUpperBound(0)
If m_f(j).Name = strSearchForm Then idxEventHandler = j
Next j
Return i
End If
Next
Return -1
End Function

5. We need to implement a method to load the mdi child forms and use the SearchChildForm method in order not to load the same mdi child form second time.

Private Sub LoadChildForms(ByVal f As Form)
If m_f.GetUpperBound(0) > 0 Then
ReDim Preserve m_f(m_f.GetUpperBound(0) + 1)
End If
m_f(m_f.GetUpperBound(0)) = f I
f Not SearchChildForm(m_f(m_f.GetUpperBound(0)).Name()) >= 0 Then
m_f(m_f.GetUpperBound(0)).MdiParent = Me
AddHandler m_f(m_f.GetUpperBound(0)).Closed, _
AddressOf UnloadChildForm
m_f(m_f.GetUpperBound(0)).Show()
m_sLoadedChildForms.Add(m_f(m_f.GetUpperBound(0)).Name)
Else
If m_f.GetUpperBound(0) > 0 Then
ReDim Preserve m_f(m_f.GetUpperBound(0) - 1)
End If
End If
End Sub

6. At last we need to implement a method to take out our mdi child form from the array list so we can load it again if we want.

Private Sub UnloadForm(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim i As Long
Dim s As String = sender.GetType().Name
Dim IndexForEventHandler = -1
i = SearchChildForm(s, IndexForEventHandler)
If i >= 0 Then m_sLoadedForms.RemoveAt(i)
If IndexForEventHandler >= 0 Then
RemoveHandler m_f(IndexForEventHandler).Closed, AddressOf UnloadForm
m_f(IndexForEventHandler) = Nothing
End If
End Sub

Important NoticeDISCLAIMER: All information, content, and data in this article are sole opinions and/or findings of the individual user or organization that registered and submitted this article at Isnare.com without any fee. The article is strictly for educational or entertainment purposes only and should not be used in any way, implemented or applied without consultation from a professional. We at Isnare.com do not, in anyway, contribute or include our own findings, facts and opinions in any articles presented in this site. Publishing this article does not constitute Isnare.com's support or sponsorship for this article. Isnare.com is an article publishing service. Please read our Terms of Service for more information.

Thomas is an experienced Visual Basic developer, with expertise of 7+ years developing financial applications. His main IT skills are VB, SQL, Crystal Reports - should you need a VB developer for your projects feel free to contact Thomas through his personal website at http://www.Kaloyani.com or through http://www.VBprofs.com
Article Tags: form [See Dictionary], mdi [See Dictionary], method [See Dictionary]
Got a question about this article? Ask the community!
Article published on November 19, 2005 at Isnare.com
 
Rate this article:

.NET : Solving The Multiple Inheritance Issue Under .NET Platform
Submitted by: Thomas Kaloyani

NET platform does not support multiple inheritance...

How to Prevent the Noise on Your Car Stereo
Submitted by: Jack Wylde

If you do not know or cannot comprehend what that term means “noise” from your car audio system then you perhaps don’t own a car audio system yet...

Relationship With an it Solution Provider?
Submitted by: Lawrence J. Reaves

IT budgets are tight, in fact SMB’s are only reporting a 56% increase in budgets (before inflation) for the next 4 years (2009 to 2013)...

Easy Tips on How to Purchase a Cheap Laptop?
Submitted by: Seomul Evans

Ah yes, I want a laptop computer but I'm not going to spend few 1000 dollars for one Well guess what you do not have to...

Choosing a Refrigerator For Kegerator Conversions
Submitted by: Felicia Jenkins

For those who are not in the know a kegerator is a beer dispensing device The keg is kept under refrigerated conditions to keep it chilled...

GBC CombBind C55 Review
Submitted by: Jeff McRitchie

GBC is one of the leading manufacturers of plastic comb binding machines They make both entry-level and advanced machines with units available in every imaginable price bracket...

Reviewing the GBC Comb Bind C95e Electric Comb Binding System
Submitted by: Jeff McRitchie

If you need an electric comb binding machine for your small office, the GBC CombBind C95e is definitely a machine you may want to consider...

Reviewing the GBC Comb Bind C340 Comb Binding Machine
Submitted by: Jeff McRitchie

The GBC CombBind C340 plastic comb binding machine is a relatively new product from the company, which still has the market cornered when it comes to binding machines...

Reviewing the GBC C450e Comb Binding Machine
Submitted by: Jeff McRitchie

When it comes to plastic comb binding machines, it's hard to go wrong with a machine from General Binding Corporation (GBC)...

GBC GL Shredmaster Paper Shredders - Tough Machines For Tough Jobs
Submitted by: Jeff McRitchie

Document security is a very hot topic these days due to the rise in identity theft and the appearance of federal privacy laws such as the Health Insurance Portability and Accountability Act (HIPPA) and the Fair and Accurate Credit Transactions Act (FACTA)...

GBC HeatSeal H100 Laminator Review
Submitted by: Jeff McRitchie

As a reasonably priced small pouch laminator, the HeatSeal H100 is designed for use in home offices or small businesses that occasionally need to laminate small items such as ID cards...

GBC HeatSeal H110 Laminator Review
Submitted by: Jeff McRitchie

If you're looking for a new laminator and you frequently laminate a lot of small items such as business cards, photographs, and bookmarks, you're reading the right review...

GBC HeatSeal H210 Laminator Review
Submitted by: Jeff McRitchie

The GBC HeatSeal H210 is one of the manufacturer's entry-level pouch laminating machines, intended for use by small and/or home offices...

A Review of the GBC HeatSeal H212 Laminator
Submitted by: Jeff McRitchie

Do you need an affordable, easy-to-use laminator for home or office use If so, you're reading the right review because the GBC HeatSeal H212 laminator could be just what you need...

Ways a Camera Phone Can Help in an Emergency
Submitted by: Seomul Evans

In an emergency you will require to allow and welcome help, and later it is over, you will have to return, repair, and rebuild...

Facts About Cell Phones Health Risks?
Submitted by: Seomul Evans

Through the long time, there have been so many rumors concerning the wellness risks of cell phones But we are a society that would almost rather die, than give them up...

Isnare.com Footer Divider

© 2004-2009. Isnare Free Articles - An Isnare Online Technologies Free Articles Project. All Rights Reserved.   Privacy Policy