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

51 Views Asked by At

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.

2

There are 2 best solutions below

1
UHM On BEST ANSWER

Microsoft tells:

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

for you:

AddHandler TreeStruct.Click AddressOf trHome_Click
0
Dhivya Bharathi On

The requirement is to perform some operations on a specific selected node in TreeViewAdv. This can be achieved by utilizing events. After initializing the control, it is possible to hook into the events. The AfterSelect event is suitable for this scenario. In this event, necessary operations for the specific selected node can be performed.

Please find the below code snippets attached for the reference.

Public Sub New()
    treeViewAdv1 = New TreeViewAdv()
    treeViewAdv1.Name = "treeView1"
    treeViewAdv1.ItemHeight = 20
    Dim treeNode1 As New TreeNodeAdv("Node1")
    Dim treeNode2 As New TreeNodeAdv("Node0", New TreeNodeAdv() { treeNode1})
    Dim treeNode3 As New TreeNodeAdv("Node3")
    Dim treeNode4 As New TreeNodeAdv("Node2", New TreeNodeAdv() { treeNode3})
    Dim treeNode5 As New TreeNodeAdv("Node5")
    Dim treeNode6 As New TreeNodeAdv("Node4", New TreeNodeAdv() { treeNode5})

    treeNode1.Name = "Node1"
    treeNode1.Text = "Child1"
    treeNode2.Name = "Node0"
    treeNode2.Text = "Parent"
    treeNode3.Name = "Node3"
    treeNode3.Text = "Child1"
    treeNode4.Name = "Node2"
    treeNode4.Text = "Parent1"
    treeNode5.Name = "Node5"
    treeNode5.Text = "Child1"
    treeNode6.Name = "Node4"
    treeNode6.Text = "Parent2"

    'Add the nodes in TreeViewAdv nodes collection

    treeViewAdv1.Nodes.AddRange(New TreeNodeAdv() { treeNode2, treeNode4, treeNode6})
    treeViewAdv1.Size = New System.Drawing.Size(377, 250)

    ' Add the TreeViewAdv to the Form controls
    Me.Controls.Add(treeViewAdv1)
    AddHandler treeViewAdv1.AfterSelect, AddressOf TreeViewAdv1_AfterSelect
End Sub                                                                          

Private Sub TreeViewAdv1_AfterSelect(ByVal sender As Object, ByVal e As EventArgs)
If treeViewAdv1.SelectedNode.Text = "Parent1" Then
    treeViewAdv1.SelectedNode.Font = New Font("Microsoft Sans Serif", Single.Parse("22"))
End If
End Sub