4

I'm working with a .NET Treeview control (not WPF, but regular winforms) and am having trouble with the right-click event (or any click event) not firing when the control has no nodes inside of it. As per the response to another thread on Stackoverflow, my event handler code is as follows:

    private void tvTest_MouseClick(object sender, MouseEventArgs e)
    {
        // Note: this block below is needed so that the menu appears on
        // the correct node when right-clicking.
        if (e.Button == MouseButtons.Right)
        {
            tvTest.SelectedNode = tvTest.GetNodeAt(e.X, e.Y);
            if (tvTest.SelectedNode != null)
            {
                tvTestContextMenuStrip.Show(tvTest, e.Location);
            }
            else
            {
                tvTestContextMenuStrip.Show(tvTest, tvTest.Location);
            }
        }
    }

The problem comes in that while this works fine when nodes are present, if the control is empty, I can't right-click the control and choose "add a node" to add to the root. The handler isn't entered AT ALL, as I set a breakpoint right at the beginning, and it appears the method is never entered.

Does anybody know how to get "something" to happen when the Treeview is empty?

Community
  • 1
  • 1
Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
  • Are you using a context menu to add nodes? Is this what you expect to pop-up when the tree contains no nodes? – Bernard Nov 08 '10 at 20:10
  • This works fine if there's a node there, and doesn't if the Treeview contains zero nodes. So the menu is fine, it's the MouseClick event that isn't working for me. – Kevin Anderson Nov 08 '10 at 21:08
  • The `TreeView` control is made specifically to handle `TreeNode`s. Try placing/showing a "ghost" control over the tree view when it contains no nodes. – Vercas Nov 08 '10 at 21:12

2 Answers2

5

I was curious about this particlar problem you described so I created a new project then added a Treeview Control to the form.

I then created an event handler for the MouseDown event, and made it show a message, when the right button is pressed. If you need the code I am happy to provided upon request, based on the fact its about 2 lines and Visual Studio created the event method I don't see the point.

Security Hound
  • 2,577
  • 3
  • 25
  • 42
  • Thanks man. Works good enough. Still a few pieces of weirdness of creation/deletion, but that may be more about how I'm doing it. You've solved the fundamental issue. – Kevin Anderson Nov 08 '10 at 21:17
  • 1
    If you have any additional questions be sure to ask them. You could get around this problem by adding a default node if there isn't real data to display. – Security Hound Nov 09 '10 at 13:13
4

I don't think this was explicitly identified above, but the original poster implies that he's using MouseClick from his code:

private void tvTest_MouseClick

But the answer responds with an answer to use MouseDown:

I then created an event handler for the MouseDown event...

I was able to reproduce the original problem by using MouseClick. It seems as if MouseClick is only raised if clicking on a node, but the MouseDown is raised regardless of whether you are on a node or not - so you can choose one or the other depending on the behavior you want.

Victor Chelaru
  • 4,491
  • 3
  • 35
  • 49