TreeView Question

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

Hi all,

Quick question about the TreeView control. I'm using code like this to
determine the currently clicked TreeNode in the TreeView.

private void MyTreeView_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
currentNode = MyTreeView.GetNodeAt(e.X, e.Y);
}

The problem I have is, if you click on whitespace to the right of a
TreeNode, it returns the TreeNode to the left. I would like it to return a
null value if you click on whitespace to the right of the TreeNode; as you
receive a null value if you click on whitespace at the bottom of the
TreeView. Anyone know how I can accomplish this?

Thanks,
Michael C., MCDBA
 
Hi Michael,

I suppose you can test if the click is within the left -> left + width of the TreeNode using TreeNode.Bounds.
However, in my test I could only get bounds from the root and the first generation, any deeper generation had no bounds.
 
Thanks Morten,

I was working along similar lines and I think I came up with something.
Here's what I have:

private void MyTreeView_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
// Convert Mouse X, Y coords to a TreeNode
currentNode = MyTreeView.GetNodeAt(e.X, e.Y);
// If currentNode is not null and the mouse point is within the
bounds
// of currentNode, we are clicking directly over the TreeNode
// otherwise, if currentNode is null, or the mouse point is outside
the
// bounds of currentNode, we are not directly over the TreeNode
if (currentNode != null && currentNode.Bounds.Contains(e.X, e.Y))
InNode = true;
else

InNode = false;
}

The only issue I'm having now is a simple display issue. When I right-click
on the whitespace to the right of a TreeNode, I don't want the TreeNode to
be highlighted. I'm not sure how to stop it from being highlighted if it's
not clicked directly on. I'm thinking I have to intercept another event,
but not sure which one. Any ideas?

Thanks,
Michael C., MCDBA

Morten Wennevik said:
Hi Michael,

I suppose you can test if the click is within the left -> left + width of
the TreeNode using TreeNode.Bounds.
However, in my test I could only get bounds from the root and the first
generation, any deeper generation had no bounds.
 
Hi all,

Quick question about the TreeView control. I'm using code like
this to determine the currently clicked TreeNode in the
TreeView.

private void MyTreeView_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
currentNode = MyTreeView.GetNodeAt(e.X, e.Y);
}

The problem I have is, if you click on whitespace to the right
of a TreeNode, it returns the TreeNode to the left. I would
like it to return a null value if you click on whitespace to the
right of the TreeNode; as you receive a null value if you click
on whitespace at the bottom of the TreeView. Anyone know how I
can accomplish this?

Michael,

..Net doesn't expose all of the native TreeView capability. There's a
way to send a HIT_TEST_INFO message to a TreeView and find out
exactly where the mouse click occurred. I have a small C# library of
utility routines on my web site at:

http://www.crtimmonsinc.com/Frontend/Desktop/DesktopDefault.aspx?tabi
d=1017

or

http://tinyurl.com/4evd6

It includes a subclass of System.Windows.Forms.TreeView that exposes
a GetHitTestInfoAt method, plus an example program showing how it's
used.
 
Back
Top