[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Raise an treeview_NodeMouseClick from treeview_AfterSelect in vb.net 2.0

Posted on 2009-04-08
6
Medium Priority
?
972 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 2000 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

650 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