[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2012-04-04
6
Medium Priority
?
800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Technology Partners: 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

In this post we will learn different types of Android Layout and some basics of an Android App.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

649 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