Solved

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

Posted on 2012-04-04
6
777 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 300 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL and CLR parameter strings 9 63
Select coumns form Data table? 3 26
COM server issue 2 23
VB.NET Parsing UDP Bytes 15 36
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

732 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