Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. override root/parent node in tree menu

override root/parent node in tree menu

Scheduled Pinned Locked Moved C#
data-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sonia Jaiswal
    wrote on last edited by
    #1

    currently have a tree menu that allowes nodes to be added and selected, however i am trying to make it so that the parent/root node can't be selected. to achieve this i have been through if statements but if anybody could post some code it would be most helpful! thanks in advance :)

    S 1 Reply Last reply
    0
    • S Sonia Jaiswal

      currently have a tree menu that allowes nodes to be added and selected, however i am trying to make it so that the parent/root node can't be selected. to achieve this i have been through if statements but if anybody could post some code it would be most helpful! thanks in advance :)

      S Offline
      S Offline
      StealthyMark
      wrote on last edited by
      #2

      a) Add an event handler for the TreeView.BeforeSelect event. or b) Override the TreeView.OnBeforeSelect method of your TreeView-inherited control. Then, check the TreeViewCancelEventArgs.Node property and set TreeViewCancelEventArgs.Cancel to false to prevent selecting the node. Example:

      private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
      {
      TreeNode parent = e.Node.Parent;
      if (parent == null)
      {
      // the root node(s) can't be selected
      e.Cancel = true;
      }
      else if (parent.Parent == null)
      {
      // the node(s) in the root node(s) can't be selected
      e.Cancel = true;
      }
      }

      S 1 Reply Last reply
      0
      • S StealthyMark

        a) Add an event handler for the TreeView.BeforeSelect event. or b) Override the TreeView.OnBeforeSelect method of your TreeView-inherited control. Then, check the TreeViewCancelEventArgs.Node property and set TreeViewCancelEventArgs.Cancel to false to prevent selecting the node. Example:

        private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
        TreeNode parent = e.Node.Parent;
        if (parent == null)
        {
        // the root node(s) can't be selected
        e.Cancel = true;
        }
        else if (parent.Parent == null)
        {
        // the node(s) in the root node(s) can't be selected
        e.Cancel = true;
        }
        }

        S Offline
        S Offline
        Sonia Jaiswal
        wrote on last edited by
        #3

        cool thanks will give it a try

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups