Solved

Raise an treeview_NodeMouseClick from treeview_AfterSelect in vb.net 2.0

Posted on 2009-04-08
6
924 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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