Windows Forms TreeView always selects node in focus

TreeView in Windows Forms always seems to want the node to be selected when it regains focus. If I do not have the selected nodes, and this tree-like focus receives focus, I get an event AfterSelectwith the first node, even if I did not select it using the keyboard, mouse, or programmatically. The only workaround I can find is to check if it is equal TreeViewCancelEventArgs.Action TreeViewAction.Unknownand then deselect. It seems really hacky, so I wonder if there is another way to fix this.

+5
source share
5 answers

I agree that use TreeViewAction.Unknownin this case is less desirable. Consider using an event BeforeSelectthat provides an opportunity to prevent the event AfterSelect.

Create an event handler GotFocusthat sets the flag. Then create an event handler BeforeSelectthat, if the flag is set, cancels the event and clears the flag. For example:

private bool treeViewWasNewlyFocused = false;

private void TreeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if(treeViewWasNewlyFocused)
    {
        e.Cancel = true;
        treeViewWasNewlyFocused = false;
    }
}

private void TreeView1_GotFocus(object sender, EventArgs e)
{
    treeViewWasNewlyFocused = true;
}
+6
source

I solved my version of this problem by disabling TabStop for the tree structure.

+5
source

( ), BeforeSelect ( ).

, , !

TreeView ( ), "" node FOCUS.

  • TreeView_MultSel: System.Windows.Forms.TreeView

Then I redefine the event handlers as such:

/// <summary>
/// //This actually occurs AFTER actual Treeview control:
///   -  Got Focus in reality
///   -  Executed the "innate" behaviour (like a button showing "depressed")
///   -  The "innate and UNWANTED behaviour of the Treeview is to selected the first Node 
///          when gets the focus.
///The key here is the Treeview executes in this order (when Tree Selected and didn't have focus):
///   -  First the Node is selected (before OnGotFocus is executed)
///         Since when LostFocus "treeHasFocus" = false the OnAfterSelect handler isn't called!!
///
///   -  Then the OnGotFocus is called:
///         This will set treeHasFocus to True and will not react to selections
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(EventArgs e)
{
    treeHasFocus = true;
    //base.OnGotFocus(e);
}

/// <summary>
/// Alot easier to handle here (in Derived TreeView control then using all kinds of 
///     -= events to try to prevent.
/// 
/// This was the cleanest way I could find (prevent firing of AfterSelect)
/// </summary>
/// <param name="e"></param>
protected override void OnLostFocus(EventArgs e)                
{                                                               
    treeHasFocus = false;                                       
    //base.OnLostFocus(e);
}

/// <summary>
/// -  Treeview Control defaults to selecting the first node (when gets focus)
/// -  We do NOT want this - since would automatically Highlight the first node (select)
/// -  treeHasFocus is NOT true for the first unwanted "automatic" selection of the first item
/// -  Upon loosing Focus, the AfterSelect handler is never called.
/// </summary>
/// <param name="e"></param>
protected override void OnAfterSelect(TreeViewEventArgs e)      
{                                                               
    if (treeHasFocus)                                           
        base.OnAfterSelect(e);                                   
    this.SelectedNode = null;                                   
}
+2
source

My version of the same problem with the following code has been fixed.

private TreeNode _selectedNode;

public FormMain()
{
    InitializeComponent();
    myTreeView.LostFocus += (sender, args) => _selectedNode = myTreeView.SelectedNode;
    myTreeView.GotFocus += (sender, args) => myTreeView.SelectedNode = _selectedNode;
}
0
source

Solved the problem by sending TVM_SELECTITEMwith lparam 0.

0
source

All Articles