|
Home / Computers / Software
VB.Net: Dynamic Usage Of Eventhandlers
By: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
Digg
del.icio.us
Blink
Stumble
Spurl
Reddit
Netscape
Furl
Article keywords: vb, .net, vb.net, visual basic, basic, software, programming, development, computer, IT, developer, event
Article Source: http://www.articles2k.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
|
|
| Top Software Articles |
- 1). Track Your MySpace Profile Visitors By : PaulG
I know that everyone who is on MySpace has had the same burning questions that I’ve had. How can I see who has viewed my MySpace profile? Where can I find a MySpace Tracker? Since MySpace has become so popular, so have the MySpace profile stalkers. Could be your friends, neighbors, ex-boyfriends or ex-girlfriends, or even someone you don’t even know.
|
- 2). 13 Comparisons of Vista vs Tiger By : paul wilson1
Competitive and fast are two terms that are applicable to the world of computers. Constantly changing and evolving computer systems bring many advantages to the users and techies alike. The race for supremacy between the yet to be launched Vista (scheduled for 2006) and Mac OSX Tiger began in 2003 with Microsoft’s announcements of plans for an integrated desktop search functionality in Windows Vista.
|
|
|
- 4). The Top Five Spyware Issues Dealing With Internet Security By : Greg Lietz
One of the main ways to compromise internet security on your PC is via a program called a Trojan Horse. A Trojan Horse is a program that quietly runs in the background, inviting the user to run it, while spreading its malicious code. This code can do any number of things. It can start right away or it may simply install a program that won't start for sometime.
|
- 5). The Benefits of Open Source By : Matthew C. Keegan
If you are a building a website or a forum, chances are you are using a web editor or paying for message board services. The costs of using these "out of the box" products can add up, especially if you are trying to grow your business. Let's look at some good "open source" options for you to consider. You may be surprised at how much money open source can save you.
|
- 6). Virus Strains By : J Square Humboldt
What we need is the Dewey Decimal System to go digital ...
Specifically, someone needs to coax their keepers into putting some logical order into how computer viruses are sorted.
Recently, warnings abounded about the Kama Sutra virus quickly proliferating cyberspace, joining the Grew.A and Nyxem.E as serious threats to computer file security. However, only those who took a closer look at these strains were able to discover that they all had something in common.
|
- 7). How To Install And Setup Noah's Classifieds By : Bedrich Omacka
In this article I will explain the basic Noah's Classifieds features, show you how to install this script and how to setup it to run properly. First of all let's mention that Noah's Classifieds script is written in PHP can be used for free. Noah's Classifieds is one of the most popular classified ads tool on the Internet. Using this great script you can create unlimited categories and subcategories, upload images for the categories and the advertisements, define variable fields for the advertisements per categories etc.
|
- 8). Trojan Horse Delivered In Automatic Update By : Darren Miller
Trojan Horse - One Mans “Worse Case Scenario” Prediction
----------------------------
This is a fictional article about a Trojan Horse Virus, or you could say it is one mans prediction of a “worse case scenario”. Because of the field I’m in, I maintain a personal list of my top 10 “worse case scenarios”. Every time I perform a security assessment I run into something new or identify a situation that is ripe for a potential vulnerability.
|
- 9). New to gaming, all you need to know about Flash gaming sites By : paul wilson1
Whew! According to Computer Gaming World, there are 80 gazillion free Flash games sites in the cyber world.
Popular sites are:
• www.games.yahoo.com -- this has card games, word games, arcade games, as well as puzzles. The number of violent games is limited so the site appeals to the “old-young” –those who are young at heart.
• www.shockwave.com -- a popular site with original games as well as PopCap ones.
|
- 10). Improve PC Performance - 6 Tips You Must Know By : Michael Braid
Are you frustrated with your PC?
Is it feeling sluggish or crawling at a snail's pace?
Are programs running slower than they used to?
Are you just plain fed up with frequent slowdowns, freezes or crashes?
Then it's time to stop what you're doing and optimize your system to improve PC performance!
Follow these 6 simple sure-fire tips to help improve PC performance and you'll be surprised by the results! Each comes with an indication of how often you should do it.
|
| New Software Articles |
|
|
|
|
- 3). Business Intelligence guide By : mansi gupta
Business intelligence can be defined as a set of business processes designed to garner and analyze business information. It is a vast category of application of programs that includes providing access to data to help an entrepreneur in his business decisions, task of query and reporting, online analytical processing (OLAP), statistical analysis, forecasting and data mining.
|
- 4). PsP Software Downloads – Review of Software Sites By : pjs1965
PsP handhelds are one of the newest and most fun ways to play games, watch movies and listen to music. PsP software downloads are just some things that you can do to improve your psp. PsP software downloads are fun and easy.
There are many places on the net were one can find psp software downloads. Some sites have you pay per download and sometimes charge up to a couple dollars for each download.
|
- 5). PsP Software – Review of Software Download Sites By : pjs1965
PsP handhelds are one of the newest and most fun ways to play games, watch movies and listen to music. PsP software downloads are just some things that you can do to improve your psp. PsP software downloads are fun and easy.
There are many places on the net were one can find psp software downloads. Some sites have you pay per download and sometimes charge up to a couple dollars for each download.
|
- 6). Microsoft Has Stunted Innovation By : Chris Young
Creativity is what spurred the growth of the computer industry and technology. Challenges were met and new frontiers conquered through sharp-edged innovations and competition. Sadly, over the years small companies like Microsoft grew into giants and then lost their thirst for adventure. They deliberately hid their proprietary source code and this stunted innovation.
|
- 7). Download Music, Movies and Games for Free - A Hot Issue By : Harry Rackers
The Internet is used by a lot of people to download music, movies, games and various other soft wares for free. The legality of this practise is, and will be for a long time, a hot issue. Some countries want to totally stop the downloading of copy wright protected materials, like the U.S.A. Other countries, like France, want to allow this type of downloading, but only for personal use.
|
- 8). Expressing Love With Ecards - Is It Effective? By : CD Mohatta
Can love be effectively expressed with ecards? With many mediums available today, which medium should be our medium of choice in expressing our feelings. We have choice of sending a voice mail, sending an email, a printed card, and ecards. Of course the first choice always remains - our own talk. How are ecards better than other mediums to express love? Let us look at that.
|
- 9). Learn Management With Desktop Wallpapers By : CD Mohatta
Learning over the ages has undergone many changes. In recent years many rapid changes have taken place, from classroom teaching to online education. Could you ever think that management could be taught with the help of Desktop wallpapers? Here is how it is being done now days.
Learning - Let us look at learning. How do we learn? By reading, reflecting and thinking and then remembering what we have been taught.
|
- 10). Adware is a Pain but you Can Deal with Fast By : Carol Hansonly
Adware is one of those things that many people simply do not worry about until the ghastly stuff actually does some damage. Yes it can cause damage and sometimes this is too late to fix. The plain facts are that adware or spyware can number one effect your computers functionality and two this can lead to system failure and ultimately data loss.
Now without putting the fear of god into you the data loss scenario is certainly a possibility but in the most extreme of cases.
|
|
|