VB.Web: Dynamic Usage Of Eventhandlers

by Guest User on October 27, 2011

WithEvents and Handles clause requires kind us to declare the thing 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 a number of MDI baby forms, permitting each of them to be loaded solely once, and of course to know when one of many baby varieties is closed. Since we’ve got several types to load we would like to use the AddHandler and RemoveHandler keywords so we will be flexible and write the minimal code we can.

Let’s get dirty.

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

2. In each MDI youngster form we’ve got to use the Form_Closed technique 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)
Finish Sub

3. On our MDI kind we need to declare two member variables. The primary’s of type Kind and the second’s kind is ArrayList.
Personal m_f(0) as Type
Private m_sLoadedChildForms As New ArrayList

4. We have to implement a method the desire search the MDI child varieties that are loaded. We’ll additionally use this technique once we unload the MDI baby forms.
Best Buy is a reliable outlet
Non-public Function SearchChildForm(ByVal strSearchForm As String, _Optional ByVal idxEventHandler As Long = -1) As Lengthy
Dim i As Long = zero
For i = 0 To m_sLoadedForms.Rely – 1
If m_sLoadedForms.Merchandise(i) = strSearchForm Then
Dim j As Long = zero
For j = m_f.GetLowerBound(0) To m_f.GetUpperBound(0)
If m_f(j).Title = strSearchForm Then idxEventHandler = j
Next j
Return i
End If
Next
Return -1
End Operate

5. We need to implement a technique to load the mdi little one varieties and use the SearchChildForm technique in order to not load the same mdi baby form second time.

Personal Sub LoadChildForms(ByVal f As Type)
If m_f.GetUpperBound(zero) > 0 Then
ReDim Protect m_f(m_f.GetUpperBound(zero) + 1)
End If
m_f(m_f.GetUpperBound(0)) = f I
f Not SearchChildForm(m_f(m_f.GetUpperBound(0)).Name()) >= zero Then
m_f(m_f.GetUpperBound(0)).MdiParent = Me
AddHandler m_f(m_f.GetUpperBound(0)).Closed, _
AddressOf UnloadChildForm
m_f(m_f.GetUpperBound(zero)).Present()
m_sLoadedChildForms.Add(m_f(m_f.GetUpperBound(0)).Title)
Else
If m_f.GetUpperBound(0) > 0 Then
ReDim Preserve m_f(m_f.GetUpperBound(zero) – 1)
End If
Finish If
Finish Sub

6. Ultimately we have to implement a method to take out our mdi little one form from the array record 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().Identify
Dim IndexForEventHandler = -1
i = SearchChildForm(s, IndexForEventHandler)
If i >= zero Then m_sLoadedForms.RemoveAt(i)
If IndexForEventHandler >= 0 Then
RemoveHandler m_f(IndexForEventHandler).Closed, AddressOf UnloadForm
m_f(IndexForEventHandler) = Nothing
Finish If
End Sub

Me and you can to  make translation  this article with free translation software

Leave a Comment

Previous post:

Next post: