How do I Programmatically invoke NodeMouseClick event?

     We are using a TreeView control. We would like to programmatically invoke the NodeMouseClick event, is this possible and how? We were looking for something like performClick() like Buttons have.

Thank You!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In that type of situation, I like to move my NodeMouseClick code to a separate method and call it from the click event or from somewhere else in code
SRO_TSGAuthor Commented:
Thank you for your suggestion and that does get us to half way solving our issue.  We are doing Test Driven Development (aka Unit Testing) and pulling out the code for the NodeMouseClick to call makes the result of a click testable. What it does not accomplish is the ability to test that the correct  Event Handler got attached to a the NodeMouseClick event.  To test this we would like to invoke the event in our Unit test.
Not sure, sorry
#1. Your best bet would be to subclass the TreeView.
#2. You could Call the existing EventHandler in your code.
#3. You may be able to get InvokeMember to work.

I would highly recommend the subclassing route...

//#1 Subclass
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace xlii.UI.Controls {
  public partial class MDMTreeView : TreeView {
    public MDMTreeView() {
    protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e) {
this.tvw.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.tvw_NodeMouseClick);
private void tvw_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { }
TreeNode node = tvw.Nodes[0];
TreeNodeMouseClickEventArgs tvea = new TreeNodeMouseClickEventArgs(node, MouseButtons.Left, 1, 0, 0);
tvw_NodeMouseClick(this, tvea);
//#3 InvokeMember
 typeof(TreeView).InvokeMember("NodeMouseClick", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod, null, tvwSheets, new object[] { this, tvea });

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.