VB.NET•Raise Events From a Method

Listing 3. The LoadDatabaseTree method calls the methods used to retrieve the database objects. LoadDatabaseTree raises the events to the calling class during this process. The calling class in turn loads the nodes into the TreeView on the main form.

Public Sub LoadDatabaseTree()

   Dim ds As DataSet = _
      _dbHelper.GetDatabases _

   DatabaseCount = _

   For Each dr As DataRow _
      In ds.Tables(0).Rows

      ' Create a new TreeNode object 
      ' which is the database name
      Dim n As New TreeNode
      With n

         ' code to set properties
         ' in the node

         ' Raise the event back to the 
         ' calling thread to add the node 
         ' to the existing TreeView 
         ' in the form
         RaiseEvent AddNode _
            (Nothing, Nothing, n)

         ' Update the counter in this 
         ' class, which is passed
         ' to the event on the 
         ' calling thread
         CurrentCount += 1

         ' Raise an event back to the 
         ' calling thread to update the
         ' progress bar count
         RaiseEvent UpdateProgress _
            (CurrentCount, DatabaseCount)

      End With
End Sub