RIAS
asked on
Raise an treeview_NodeMouseClick from treeview_AfterSelect in vb.net 2.0
Hi,
I need to raise an treeview1_NodeMouseClick from treeview1_AfterSelect in vb.net 2.0
AddHandler treeview1.NodeMouseClick, AddressOf treeview1_NodeMouseClick
does not work any suggestion?
Cheers
I need to raise an treeview1_NodeMouseClick from treeview1_AfterSelect in vb.net 2.0
AddHandler treeview1.NodeMouseClick, AddressOf treeview1_NodeMouseClick
does not work any suggestion?
Cheers
AddHandler treeview1.NodeMouseClick, AddressOf treeview1_NodeMouseClick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To be quite honest I'm not sure why you'd want to go through all the work of creating TreeNodeMouseClickEventArg s object, only to handle it immediately.
The problem is that to raise an event in a control, you need to call the On.... method, for example, TreeNodeMouseClick is called by OnOnNodeMouseClick. Unfortunately this method is protected, so unless you're creating a custom treeview, that injerits TreeView you don't have access to it.
You can of course use Reflection to call the OnNodeMouseClick method yourself, but you have to ask yourself "if it's this complicated, am I doing it the correct way?"
The code below uses reflection to get the Protected OnNodeMouseClick method from a TreeView control. It then creates TreeNodeMouseClickEventArg s, filling it with whatever data you wish. Finally it invokes the method on the TreeView that sent the event in the first place.
The problem is that to raise an event in a control, you need to call the On.... method, for example, TreeNodeMouseClick is called by OnOnNodeMouseClick. Unfortunately this method is protected, so unless you're creating a custom treeview, that injerits TreeView you don't have access to it.
You can of course use Reflection to call the OnNodeMouseClick method yourself, but you have to ask yourself "if it's this complicated, am I doing it the correct way?"
The code below uses reflection to get the Protected OnNodeMouseClick method from a TreeView control. It then creates TreeNodeMouseClickEventArg
' This code goes in the AfterSelect event handler
' Create the EventArgs
Dim ev As New TreeNodeMouseClickEventArgs(e.Node, MouseButtons.Left, 1, 10, 10)
' Get the OnNodeMouseClick method
Dim onNodeClick As System.Reflection.MethodInfo = GetType(TreeView).GetMethod("OnNodeMouseClick", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
onNodeClick.Invoke(sender, New Object() {ev})
ASKER
Hi,
Cheers for the solution but I get an signature mismatch error 'ev'
Cheers for the solution but I get an signature mismatch error 'ev'
ASKER
Excellent
Thanks for the points.
As for the reflection code, I can't understand why you should get a signature mismatch when invoking the method, it works straight away for me.
As for the reflection code, I can't understand why you should get a signature mismatch when invoking the method, it works straight away for me.
ASKER