ArticlesReader.com Menu
Newest Articles
Most Viewed Articles
ArticlesReader.com RSS
Submit Article
Login
Signup
Search the articles

Articles Main Categories
Advice
Animals
Automobiles
Business
Career
Communications
Computer Programming
Computers
Entertainment
Environment
Family
Fashion
Finance
Food
Health & Medical
Home & Garden
Humor
Internet Business
Internet Marketing
Legal
Leisure & Recreation
Marketing
Other
Politics
Reference & Education
Religion
Self Improvement
Sports
Technology & Science
Travel
Writing
Subscribe
Receive alert message from us when new articles submitted to our site for free.

Enter your name

Enter your email

Syndicate

















Related Products
Home::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


Article Source: http://www.articledashboard.com





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 www.Kaloyani.com or through www.VBprofs.com





Spam emails More free articles

Related articles


  1. Scrap Booking Online: Word Perfect or Corel Graphics Suite?
  2. Great Plains Dexterity Customization Options – Overview For Developers
  3. Microsoft Great Plains Security Setup - Overview For Consultant
  4. Microsoft Great Plains Implementation: Collection Management – Overview For Consultant
  5. Best Spyware Removers
  6. Monitoring Software Can be Used for Spying as Well
  7. Beware of Spyware
  8. Microsoft CRM Integration & Customization: SharePoint Document Gateway
  9. Adware, What Is It?
  10. Microsoft CRM Modification – Overview for IT Specialist
  11. Reporting Tools for Microsoft Great Plains – Overview For Developers
  12. Microsoft Great Plains Upgrade – Things to Consider and FAQ
  13. Great Plains Dexterity History and Programming Overview
  14. Microsoft Great Plains on Ctree or Pervasive SQL – What to Do – Tips for IT Manager
  15. Microsoft Business Solutions VAR/Partner Selection – Overview for IT Director/Manager/Controller
  16. Great Plains Accounting Migration to Microsoft Great Plains - Overview for IT Specialist
  17. Microsoft Great Plains Data Conversion – Overview For Developer
  18. Great Plains Dynamics/eEnterprise Upgrade – Things to Consider and FAQ
  19. Crystal Reports - Geterogeneus databases: SQL Server, Oracle, Unidata, Btrieve – Tips For Developer
  20. Great Plains Dexterity Programming – Overview For Developer
  21. Great Plains Custom Development: Dexterity, VBA, SQL, Crystal, eConnect – Overview For Programmer
  22. How To Create A Data Capture Procedure Checklist For Your Small Business CRM Software
  23. Chinese Input - Step by Step Instruction on How to Input Chinese Characters in English Windows XP
  24. Microsoft Great Plains - Payroll & HR Inexpensive Solution? Not Any More
  25. 20 Extra Hours Per Week: What Would You Do?
More related feeds
VB.Net: Dynamic Usage Of Eventhandlers
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 ...

Using the Media Element control in Silverlight 2
This article describes how to use the Media Element control in Silverlight 2. ... Effective VB.NET · GDI+ and Graphics · Mobile .NET · Internet and Web · Visual Basic Language · Networking · Office and VB.NET ...

.NET - Making Asynchronous Calls to WCF Services from ASP.NET
If you would like to keep the port number fixed, Select the project in the Solution Explorer > Go to the Properties window > Set 'Use Dynamic Ports' to False > Set the Port number. I am using port 64258. Build the Solution. ...

Digimaker .net Content Management System (cms) | Computer Software
Recent Posts. Spyware The Enemy To You And Your Computer · Vb.net: Dynamic Usage Of Eventhandlers · Business Portal – Microsoft Dynamics Gp 9.0 – Highlights For Consultant · Software Evolution · How To Avoid Spyware? ...

On Releasing COM objects in .NET
Usually, such parameters are released just after the event handler finishes. But there are numerous exceptions. Just recently, we saw how a customer released the inspector passed to the NewInspector handler in the Dynamic Command Bar ...

Silverlight CheckBox Control
This article demonstrates how to create and use a CheckBox control in Silverlight with the help of XAML and C#. ... Office and VB.NET · Pocket PC · Printing · Security · Strings & Arrays · Tablet PC · Multithreading · Windows Forms ...

Download Adware Programs From Lavasoft - For Free! | Computer Software
Any computer connected to the Internet needs some sort of protection against unscrupulous companies and persons who use the Internet to spread malicious software. The new lavasoft.com Adware 6.0 has been designed to protect the computer ...

.NET - Silverlight 2 - Saving and Retrieving Images from ...
The site covers technical articles on Microsoft.NET, ASP.NET, C#, VB.NET, WinForms and related technologies.

Log File Unix Commands | Computer Software
Vb.net: Dynamic Usage Of Eventhandlers · Business Portal – Microsoft Dynamics Gp 9.0 – Highlights For Consultant · Software Evolution · How To Avoid Spyware? How To Find Totally Free Spyware Removal Program And Avoid The Shams ...

OpenFileDialog in Silverlight
The OpenFileDialog allows users to select one or more files on the local computer or on a networked computer. This article demonstrates how to create and use an OpenFileDialog control in Silverlight using XAML and C#.

 


 

© 2007 articlesreader.com - All Rights Reserved