Case Surf.com
Index -> About Us -> Add Your Link -> Privacy of Info -> Terms & Conditions -> Submit Article
Search:   
 

How Do Viruses And Worms Affect Your Computer

Also, unlike viruses that most commonly infect an individual computer, the worms usually attack a co ... - Jeff Clark
 

Newest Porsche Cayman: Cheap But Thrilling Nonetheless

The cheaper Porsche Cayman is sold some 7,755 cheaper compared to the Porsche Cayman S. It is greatl ... - Joe Thompson
 

Use Educational Software

If you are a parent of growing children than you know the seriousness of the task of teaching and pr ... - Rachael Cleipher
 
 

How To Calculate Transmission Losses

Chassis dynamometers are the devices that measure the power and torque of an engine, without removin ... - Groshan Fabiola
 

The Wonderfully efficient World of Web Based Training

The increase in popularity of the web based training is enormous today. I believe that the reasons f ... - Morgan Hamilton
 

PsP Software Downloads ?C Review of Software Sites

This article is a review in PsP software downloads - pjs1965
 

2007 Nissan Versa: Gen "Y", Take Notice

Nissan is poised to release a Scion fighter all of its own. The Versa is aimed squarely at young buy ... - Matthew Keegan
 

The Other Side of the Truck Rental Coin

Budget and other companies have what they call their commercial rental divisions, where they handle ... - Scott Best
 
 

Index » Automotive » Auto Industry Software
 

VB.Net: Dynamic Usage Of Eventhandlers

 
Author: 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

 
 
 

Related Articles

 
Bugatti Veyron: A Driving Experience of a Lifetime
 
Jeep Wrangler. No Pretender
 
Top 5 Most Scenic Highways in the US
 
Georgia's Steam Coffin
 
BTCC - Green Flag For Go!
 
How to Prevent Your Car from being Stolen
 
The Five Words That Will Kill Your Service Business
 
Drive a Race Car For One Day
 
2006 Jaguar X Type: Luxury for the Commoner
 
Tom Tom GPS: The GPS Industry Leader In Innovations Explained
 
 
 
Add Url
 

Computers & Software

News & Media

Sports & Adventure

Jobs & Careers

Academics & Education

Science & Space

Creative Arts

Self Help

Indoor Games

Hygiene & Health

Fashion & Relationships

Companies & Business

Estate & Realty

Society & Communities

Food & Recipe

Travel & Accommodation

Government & Politics

Children & Teens

Home Family & Garden

Medicine & Treatment

Online Shopping

Finance & Banking

Recreation

Automotive

 
Index -> Privacy of Info -> Terms & Conditions  
Copyright © 2008 www.casesurf.com All Rights Reserved.