• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2208
  • Last Modified:

How do I Programmatically invoke NodeMouseClick event?

Hello,
     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!
0
SRO_TSG
Asked:
SRO_TSG
  • 2
1 Solution
 
UnifiedISCommented:
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
0
 
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.
0
 
UnifiedISCommented:
Not sure, sorry
0
 
almanderCommented:
#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() {
      InitializeComponent();
 
    }
    protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e) {
      MessageBox.Show("Click");
      base.OnNodeMouseClick(e);
    }
  }
}
 
//#2
this.tvw.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.tvw_NodeMouseClick);
 
private void tvw_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { }
 
//Invoke
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now