dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: TreeView node - how to re-select?

  1. #1
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    TreeView node - how to re-select?

    Hi,

    I have trouble with reselecting a treeview item, after it has been selected, a process run and the focus on that node is lost.

    I tried to save the selected nore into a TreeNode like

    Code:
    TreeNode selectedNode = treeView.SelectedNode;
    
    // do some stuff, focus is lost
    
    
    //now reselect
     treeView.SelectedNode = selectedNode;
    Though the values in selectedNode is correct, the selection does not occur.
    Where I'm wrong?

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: TreeView node - how to re-select?

    Try setting the treeview's HideSelection property to False...
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: TreeView node - how to re-select?

    This does not work, too :-(

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: TreeView node - how to re-select?

    That's what I did when I had this problem a while ago...

    You said that the treeview.SelectedNode has the correct value, but the node is not marked as selected by a blue square? Or have I misunderstood..?
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: TreeView node - how to re-select?

    Yes,

    the value is correct, but not marked blue.

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: TreeView node - how to re-select?

    i think that's a normal behavior of the TreeView control. anyway, you can cheat it like this

    Code:
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                TreeView tv = (TreeView)sender;
                if (tv.Tag != null)
                {
                    TreeNode nod = (TreeNode)tv.Tag;
                    nod.ForeColor = tv.ForeColor;
                    nod.BackColor = tv.BackColor;
                }
                e.Node.ForeColor = Color.White;
                e.Node.BackColor = Color.DarkBlue;
                tv.Tag = e.Node;
            }
    Busy

  7. #7
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: TreeView node - how to re-select?

    I think so too... Setting the HideSelection property to false should do that for you though...
    It's not a bug, it's a feature!

  8. #8
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: TreeView node - how to re-select?

    Quote Originally Posted by foamy View Post
    I think so too... Setting the HideSelection property to false should do that for you though...
    It's annoying, it does not work. Maybe I have to cheat it like described above....

    Thanks for your comment anyway.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,175

    Re: TreeView node - how to re-select?

    Quote Originally Posted by MNovy View Post
    It's annoying, it does not work. Maybe I have to cheat it like described above....
    Understand that when it remains selected, it (depending your OS theme settings) will show up highlighted in gray, not in blue. This is the correct behavior because Windows want's to retain the selected item, but also want's to let the user know that the treeview control does not have focus. To illustrate, open up explorer and select a folder and then click on a file. You'll notice how the selected folder turns gray when the folder treeview loses focus.

    The HideSelection property is the way to go. IMO, if you trick this by setting the time color, you'll run into two problems:
    1) It will be confusing to the user
    2) The selected blue color may be incorrect for users with different color schemes.

    Below is a screenshot from the attached sample that shows a treeview retaining the selected item after losing focus. Notice how the item is highlighted in gray (or whatever your color scheme represents this type of item).
    Attached Images Attached Images  
    Attached Files Attached Files

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: TreeView node - how to re-select?

    To continue....
    You should not need to play games with the SelectedNode (unless your logic updates the tree itself. Just setting the focus back to the TreeView should do it...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)