VB.Net Dynamic Usage Of Eventhandlers

Below is a MRR and PLR article in category Computers Technology -> subcategory Software.

AI Generated Image

VB.Net: Dynamic Use of Event Handlers


Introduction


In VB.Net, handling events dynamically can significantly enhance flexibility. While the `WithEvents` and `Handles` clauses establish object-event connections during compilation, `AddHandler` and `RemoveHandler` allow for runtime linking, offering greater adaptability.

Managing MDI Child Forms


Imagine you want to manage several MDI child forms, allowing each to load only once, and tracking when they close. Using `AddHandler` and `RemoveHandler`, we can achieve this efficiently with minimal code.

Step-by-Step Implementation


1. Declare a Public Event in Each MDI Child Form
```vb
Public Event FormClosed(ByVal f As Form)
```

2. Handle the Form Closed Event
Implement the `Form_Closed` method to trigger the `FormClosed` event.
```vb
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
RaiseEvent FormClosed(Me)
End Sub
```

3. Declare Member Variables in the MDI Form
You'll need a `Form` array and an `ArrayList` to track loaded forms.
```vb
Private m_f(0) As Form
Private m_sLoadedChildForms As New ArrayList
```

4. Search for Loaded MDI Child Forms
Create a method to search through loaded child forms, useful when loading or unloading forms.
```vb
Private Function SearchChildForm(ByVal strSearchForm As String, Optional ByVal idxEventHandler As Long = -1) As Long
For i As Long = 0 To m_sLoadedChildForms.Count - 1
If m_sLoadedChildForms.Item(i) = strSearchForm Then
For j As Long = 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. Load MDI Child Forms
Use this method to load child forms, ensuring no duplicates.
```vb
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(f.Name()) >= 0 Then
f.MdiParent = Me
AddHandler f.Closed, AddressOf UnloadForm
f.Show()
m_sLoadedChildForms.Add(f.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. Unload MDI Child Forms
Implement a method to remove forms from the array list, allowing them to be loaded again later.
```vb
Private Sub UnloadForm(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim s As String = sender.GetType().Name
Dim IndexForEventHandler = -1
Dim i = SearchChildForm(s, IndexForEventHandler)
If i >= 0 Then m_sLoadedChildForms.RemoveAt(i)
If IndexForEventHandler >= 0 Then
RemoveHandler m_f(IndexForEventHandler).Closed, AddressOf UnloadForm
m_f(IndexForEventHandler) = Nothing
End If
End Sub
```

Conclusion


By utilizing `AddHandler` and `RemoveHandler`, your VB.Net applications can handle events more dynamically and efficiently. This approach streamlines the process of managing multiple MDI child forms, promoting clean and maintainable code.

You can find the original non-AI version of this article here: VB.Net Dynamic Usage Of Eventhandlers.

You can browse and read all the articles for free. If you want to use them and get PLR and MRR rights, you need to buy the pack. Learn more about this pack of over 100 000 MRR and PLR articles.

“MRR and PLR Article Pack Is Ready For You To Have Your Very Own Article Selling Business. All articles in this pack come with MRR (Master Resale Rights) and PLR (Private Label Rights). Learn more about this pack of over 100 000 MRR and PLR articles.”