Solved

Raise an treeview_NodeMouseClick from treeview_AfterSelect in vb.net 2.0

Posted on 2009-04-08
6
905 Views
Last Modified: 2012-05-06
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
AddHandler treeview1.NodeMouseClick, AddressOf treeview1_NodeMouseClick

Open in new window

0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24099343
AddHandler, doesn't raise an event, it only adds as the name implies an event handler to the object

Instead of raising the event oneself, I'd suggest create a method that can be called by both AfterSelect & NodeMouseClick, and do the work in that
public sub dostuff(TreeNode node)
end sub
 
public sub treeView1_AfterSelect(sender as object, e as TreeViewEventArgs) handles treeView1.AfterSelect
  dostuff(e.Node)
end sub
 
public sub treeView1_NodeMouseClick(sender as object, e as TreeNodeMouseClickEventArgs) handles treeView1.NodeMouseClick
  dostuff(e.Node)
end sub

Open in new window

0
 

Author Comment

by:RIAS
ID: 24099936
Cheers mate but can you suggest raising singlemouseevent from afterselect event
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24100211
To be quite honest I'm not sure why you'd want to go through all the work of creating TreeNodeMouseClickEventArgs 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 TreeNodeMouseClickEventArgs, filling it with whatever data you wish. Finally it invokes the method on the TreeView that sent the event in the first place.
' 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})

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:RIAS
ID: 24105172
Hi,
Cheers for the solution but I get an signature mismatch error 'ev'
0
 

Author Closing Comment

by:RIAS
ID: 31568120
Excellent
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24105681
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.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question