With events and Handles clause requires form us to declare the object variable and the operator of events in which we write our code, so the link is created at the time of compilation. On the other hand, with AddHandler and RemoveHandler, the connection is created and removed at runtime, which is more flexible.
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 a child forms is closed.
Since we have different forms ofwe would like to load and use the keywords AddHandler RemoveHandler so we can be flexible and we can write the minimum code.
Let's get dirty.
1. In any MDI child form we have to declare a public event.
Public event FormClosed (ByVal f As Form)
2. In any MDI child form we have to use the method that handles the class Form_Closed MyBase.Closed FormClosed and raise the 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 module of type I and the second is ArrayList.
m_f Private (0) as a module
m_sLoadedChildForms As New ArrayList
4. We need to implement a method of seeking the will of the MDI child forms that are loaded. We will also use this method when you download the MDI child forms.
SearchChildForm Private Function (ByValstrSearchForm 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) For m_f.GetUpperBound (0)
If strSearchForm m_f (j). Then j = Name = idxEventHandler
Next j
I return
End If
Next
Return -1
End Function
5. We need to implement a method to load the mdichild form and use the method SearchChildForm so as 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 you do 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, _
AddressOfUnloadChildForm
m_f (m_f.GetUpperBound (0)). Show ()
m_sLoadedChildForms.Add (m_f (m_f.GetUpperBound (0)). Name)
Other
If m_f.GetUpperBound (0)> 0 Then
ReDim Preserve m_f (m_f.GetUpperBound (0) - 1)
End If
End If
End Sub
6. In the end we need to implement a method of keeping out of our child MDI form from the list of arrays so that we can reload, if you will.
Private Sub UnloadForm (ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim iAs Long
Dim s As String = sender.GetType (). Name
= -1 Dim IndexForEventHandler
i = SearchChildForm (s, IndexForEventHandler)
If i> = 0 Then m_sLoadedForms.RemoveAt (i)
If IndexForEventHandler> = 0 Then
m_f RemoveHandler (IndexForEventHandler). Closed, AddressOf UnloadForm
m_f (IndexForEventHandler) = Nothing
End If
End Sub
No comments:
Post a Comment