spacer
spacer

Visual Basic & VB.NET resources | VBprofs.com

Google
 
Main Menu
Home
IT News
Articles
Source Codes
Books
Calendar of Events
Publish your Content
Links Directory
Reviews and Recs
Premium Content
 VBprofs Premium Content Become a member to get access to our premium content! 

 Membership is entirely free, and allows you to:
  •  access programming projects posted by our partners;
  •  publish your articles, news, links;
  •  publish a project request;
  •  access our VB Careers section.
Polls
How did you reach VBprofs.com?
  
Which is your favorite programming language?
  
Buy Text Link Ad
LinkWorth Stats
Check our stats on LinkWorth. If you like what you see, signup with LinkWorth and buy a link with us.
 
Home arrow Source Codes arrow .NET arrow VB.NET: Dynamic Usage of Event Handlers

VB.NET: Dynamic Usage of Event Handlers PDF Print E-mail
Source Codes - .NET
Written by Thomas Kaloyani   
Visual Basic / .NET ArticlesWithEvents 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

    If 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


About the author: Thomas is an experienced Visual Basic developer, with expertise of 7+ years developing especially financial applications. His main IT skills are VB, SQL, Crystal Reports - should you need a Visual Basic developer for your projects feel free to contact Thomas through his personal website at www.Kaloyani.com

Source: www.vbprofs.com
< Prev

 
Antivirus Shop
BitDefender Antivirus v10
Newsflash

PowerRefresh 1.0 released!
A simple yet extremely useful tool for webmasters, publishers and SEO working bees: PowerRefresh allows you to automatically refresh your IE windows every x minutes. It can handle unlimited number of windows and, unlike similar applications, is using full browsers.


Get PowerRefresh from Kaloyani.com: download
Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one
Popular

VBprofs.com - online resources for Visual Basic and VB.NET professionals: Visual Basic and VB.NET articles, industry news and events, career tools, VB / VB.NET books, calendar and much more. VBprofs.com is an interactive web site with free membership.
(c) Copyright 2005 - 2006 by VBprofs.com
powered by Mambo Open Source Software
spacer