Solved

Raise an treeview_NodeMouseClick from treeview_AfterSelect in vb.net 2.0

Posted on 2009-04-08
6
879 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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