0
votes

I have a c# windows form application where I have a treeView inside a tabPage of a tabControl which is a part of the main form.

For the tree view, I click on the items of the treeView which I want to select then some change happens based on my selected Items.

I am using the AfterSelect event for item selection and and the mouseUp event for undoing the selection.

The item selection and deselection happens right away with a single click (no problem). The other change with should happen based on the selected items happens after two clicks! A single click either on the item node or outside the node's area do not trigger this change. I have to click again in order to see the change. That is wired. I am not using mouse double click events for this or something similar, I am only using the events I described above.

How can this be happening? and How to resolve it? Thanks.

EDIT: I am using my own multi-selection version of the treeView and I found (using debug) that when I get the selected nodes of the tree in the AfterSelect event after the first click is zero, then it is the number of selected nodes with the second click. How come this is happening when selected nodes are added and to the current selectedNodes list with every click in the overrided OnAfterSelect event of the treeView?

here is part of tree view code:

public List<TreeNode> SelectedNodes
        {
            get
            {
                return selectedNodes; 
            }

            set
            {
                removeSelectionFromNodes();
                selectedNodes = value;
                selectNodes();
            }
        }

   protected override void OnAfterSelect(TreeViewEventArgs e)
        {
            base.OnAfterSelect(e); 
            base.SelectedNode = null;
           List<MSTreeNode> nodes = new List<MSTreeNode>();
                   .
                   .
                   .
               removeSelectionFromNodes();
                selectedNodes.Clear();
                selectedNodes.AddRange(nodes);
                selectNodes();
        }
3
This is a side effect of mouse capture, Control.Capture property. How that happened is completely unclear from the description. Post a snippet.Hans Passant

3 Answers

0
votes

Maybe Treeview is losing focus in between clicks (?). You could try setting Treeview HideSelection property to False to keep the currently selected item highlighted when the control loses focus.

0
votes

I tried to use the MouseDown event instead of the AfterSelect event. I override it in the my own multi-selection version of the treeView and used in the c# application I am developing but still it did not work. I am not sure how mouse events really work. If not used carefully, you may see wired behaviors.

Well, I ended up overriding the MouseUp and MouseUp events in my treeView subclass then I created an event which listens for changes in the selectedNodes list. If a change to the selectedNodes happnes in any of the mouse events this event is triggered. Then, I used the ChangedSelectedNodes event handler of the treeview instance in my application to do the other changes when there is a change in the node selection. This time it worked as expected.

I posted this in hope that it would be beneficial to anyone else who ran into the same problem like me.

P.S. Sometimes things do not work as you expect them to be and you just have fight and go through every other possibility until you find the solution.

0
votes

Disable the hide selection option and use afterSelect option in my project that works well