?
Solved

How can I disable selection of a TreeNode in a TreeView in VB.NET?

Posted on 2012-04-04
6
Medium Priority
?
788 Views
Last Modified: 2012-04-07
I'm currently implementing a navigation menu using a TreeView control.

In the menu I'm using headers (root nodes) to group menu items. These headers should only have one purpose: double-click collapse/uncollapse. For this reason it should'nt be possible to select and mark the node, but as default this is possible. What can I do to avoid that the headers can be selected? And is there a way to do this by setting TreeNode properties so that selection events doesn't have to be handled?
0
Comment
Question by:andreas_rafn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 37806315
Are you using Windows Forms or Web Forms?
0
 

Author Comment

by:andreas_rafn
ID: 37806592
Windows Forms (System.Windows.Forms.TreeView)
0
 
LVL 1

Accepted Solution

by:
IceBird26 earned 1200 total points
ID: 37806660
You could add some code in the treeview's BeforeSelect event. Since you don't want any selections on the root nodes, you could do the following:

     private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Parent == null)    //if a node doesn't have any parents, it's a root
            {
                e.Cancel = true;
            }
        }



The only drawback is that the node will still be highlighted for a short while while the mouse click is in the down position. Hopefully, your users are not very picky ;)
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 800 total points
ID: 37806665
Use the BeforeSelect() event and set Cancel to True if the Node does not have a Parent (because it's a root):
    Private Sub TreeView1_BeforeSelect(sender As Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect
        e.Cancel = IsNothing(e.Node.Parent)
    End Sub

Open in new window


*The node does get selected momentarily.  =\
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37806670
Hehe...guess I'm 20 seconds too late!  =)
0
 

Author Closing Comment

by:andreas_rafn
ID: 37819014
The fastest submitter gets a little more :) The approach seems good, I'll try to combine it with a handling of the selection color so that the temporary selection will be invisible.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
Starting up a Project
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question