Search code examples
vb.netwinformssyncfusion

How can I handle events from a control in winforms that is created in the code and not the drag/drop designer?


As you can see when my application starts I am creating a treeview from syncfusion named "trHome". Now I want to do something if a specific node in the tree is selected but I do not know how to handle this control which is not present in the designer.

Private Sub Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  Dim TreeStruct As TreeViewAdv = New TreeViewAdv()
  Dim font As Font = New Font("Microsoft Sans Serif", Single.Parse("12"))
  TreeStruct.Name = "trHome"
  TreeStruct.Font = font
  gb.Client = TreeStruct
End Sub

Private Sub trHome_Click(sender As Object, e As EventArgs) Handles trHome.Click

End Sub

I was hoping when I clicked on a node in the tree control my code would execute the sub trHome_Click.


Solution

  • Microsoft tells:

    ' Associate an event handler with an event.
    AddHandler Obj.Ev_Event, AddressOf EventHandler
    

    for you:

    AddHandler TreeStruct.Click AddressOf trHome_Click