How do I Programmatically invoke NodeMouseClick event?

Posted on 2008-11-12
Last Modified: 2013-12-17
     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!
Question by:SRO_TSG
    LVL 18

    Expert Comment

    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

    Author Comment

    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.
    LVL 18

    Expert Comment

    Not sure, sorry
    LVL 5

    Accepted Solution

    #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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now