Reputation: 11
I'm working with a windows form app which I have treeview to show the list of folders , and I have attached NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
event.
and on click of node I call server method to populate the treeview.
Here I could see that NodeMouseClick for one of my tree node is not at all getting triggered.
however for rest of the nodes its working fine with no issues. can anyone tell me what is the exact reason that its not getting triggered.
and I dont want to use After_Select event.
public Form1()
{
InitializeComponent();
Init();
}
private void Init()
{
treeView1.Nodes.Add("root");
for (int i = 0; i < 23; i++)
{
treeView1.Nodes[0].Nodes.Add(i.ToString());
treeView1.Nodes[0].Nodes[i].Nodes.Add("child" + i.ToString());
}
treeView1.Nodes[0].Expand();
}
use treeview of size = 280,369
Upvotes: 0
Views: 2610
Reputation: 244772
As I mentioned before in the comments, the workaround is to drop down to the level of the Windows API, intercept mouse messages, and raise the node click event yourself. The code is ugly, but functional.
Add the following code to a new class in your project (I called it CustomTreeView
):
class CustomTreeView : System.Windows.Forms.TreeView
{
public event EventHandler<TreeNodeMouseClickEventArgs> CustomNodeClick;
private const int WM_LBUTTONDOWN = 0x201;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_LBUTTONDOWN) // left mouse button down
{
// get the current position of the mouse pointer
Point mousePos = Control.MousePosition;
// get the node the user clicked on
TreeNode testNode = GetNodeAt(PointToClient(mousePos));
// see if the clicked area contained an actual node
if (testNode != null)
{
// A node was clicked, so raise our custom event
var e = new TreeNodeMouseClickEventArgs(testNode,
MouseButtons.Left, 1, mousePos.X, mousePos.Y);
if (CustomNodeClick != null)
CustomNodeClick(this, e);
}
}
// call through to let the base class process the message
base.WndProc(ref m);
}
}
Then change all references to the System.Windows.Forms.TreeView
control in your code to the new CustomTreeView
class that you just created. This is a subclass of the existing TreeView
control that you want to use instead. In case you're not familiar with subclassing, this is the way we modify the existing functionality, or bolt on new functionality to, an existing control. In this case, we've subclassed the original TreeView
control to add the CustomNodeClick
event that we'll be raising ourselves whenever we detect that a node has been clicked by the user.
Finally, change the event handler method in your form class to listen for the CustomNodeClick
event that we're raising, rather than the buggered NodeMouseClick
event you were trying to use before.
Compile and run. Everything should work as expected.
Upvotes: 1
Reputation: 7150
try to use AfterSelect
Event it must be triggered after any node selection .
Upvotes: 0