views:

111

answers:

1

I have a winforms TreeView control that allows you to browse an object hierarchy. Incidentally, there are some circular references between objects.

I have no problem letting the user navigate circular references, but I want to prevent a '*' keypress or ExpandAll() command from executing.

How do you go about doing this?

A: 

For the ExpandAll method, you can't as it is just handled as a recursive call to Expand and its non-virtual so you can't even override it.

As for preventing the '*' key, you can hook into the tree view's OnKeyDown event and cancel the key using the following code:

private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Multiply)
    {
        e.Handled = true;
        e.SuppressKeyPress = true;
    }
}

As a safe-guard you could look out for recursion in your controller/presenter/view model and only allow a certain number of recursions, e.g. 4.

KeeperOfTheSoul