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?
almanderConnect With a Mentor Commented:
#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

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.