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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. TreeView Control problem

TreeView Control problem

Scheduled Pinned Locked Moved C#
questionhelp
13 Posts 3 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.
  • A Offline
    A Offline
    Adrian Soon
    wrote on last edited by
    #1

    Hi All, I working on my treeView. I had a treeView with both CheckBox and Expandlist. I had a "NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)" method. inside the method I wish to know whether I click on is a checkbox or the expand + button. how can I do it?

    L H 2 Replies Last reply
    0
    • A Adrian Soon

      Hi All, I working on my treeView. I had a treeView with both CheckBox and Expandlist. I had a "NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)" method. inside the method I wish to know whether I click on is a checkbox or the expand + button. how can I do it?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Have a look at TreeView.HitTest() and in particular its Location property. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      A 1 Reply Last reply
      0
      • L Luc Pattyn

        Have a look at TreeView.HitTest() and in particular its Location property. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        A Offline
        A Offline
        Adrian Soon
        wrote on last edited by
        #3

        Hi thanks, but tat don't do the trick. I felt. my tree goes like this. c:\ ---a\ ---b\ -----a\ -----b\ ---c\ -----a\ -----b\ -------a\ -------b\ ---d\ I nd to find out whether I click is a + sign or a check box. how to program it? thanks.

        1 Reply Last reply
        0
        • A Adrian Soon

          Hi All, I working on my treeView. I had a treeView with both CheckBox and Expandlist. I had a "NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)" method. inside the method I wish to know whether I click on is a checkbox or the expand + button. how can I do it?

          H Offline
          H Offline
          Hessam Jalali
          wrote on last edited by
          #4

          You can find list expanding using AfterExpand and BeforeExpands events and from the e argument you can find the affected node and action and the same approach works for check boxes with using AfterCheck and BeforeCheck events

              public Form1()
              {
                  InitializeComponent();
                  this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1\_BeforeExpand);
                  this.treeView1.BeforeCheck += new TreeViewCancelEventHandler(treeView1\_BeforeCheck);
              }
          
              void treeView1\_BeforeCheck(object sender, TreeViewCancelEventArgs e)
              {
                  StringBuilder str=new StringBuilder();
                  str.AppendFormat("\[Check\]   Name: {0}  CurrentStatus: {1}  NextStatus: {2}",e.Node.Text,e.Node.Checked.ToString(),(!e.Node.Checked).ToString());
                  MessageBox.Show(str.ToString());
              }
          
              void treeView1\_BeforeExpand(object sender, TreeViewCancelEventArgs e)
              {
                  StringBuilder str = new StringBuilder();
                  str.AppendFormat("\[Expand\]   Name: {0}  CurrentStatus: {1}  NextStatus: {2}", e.Node.Text, e.Node.IsExpanded.ToString(), (!e.Node.IsExpanded).ToString());
                  MessageBox.Show(str.ToString());
              }
          

          good luck

          A 1 Reply Last reply
          0
          • H Hessam Jalali

            You can find list expanding using AfterExpand and BeforeExpands events and from the e argument you can find the affected node and action and the same approach works for check boxes with using AfterCheck and BeforeCheck events

                public Form1()
                {
                    InitializeComponent();
                    this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1\_BeforeExpand);
                    this.treeView1.BeforeCheck += new TreeViewCancelEventHandler(treeView1\_BeforeCheck);
                }
            
                void treeView1\_BeforeCheck(object sender, TreeViewCancelEventArgs e)
                {
                    StringBuilder str=new StringBuilder();
                    str.AppendFormat("\[Check\]   Name: {0}  CurrentStatus: {1}  NextStatus: {2}",e.Node.Text,e.Node.Checked.ToString(),(!e.Node.Checked).ToString());
                    MessageBox.Show(str.ToString());
                }
            
                void treeView1\_BeforeExpand(object sender, TreeViewCancelEventArgs e)
                {
                    StringBuilder str = new StringBuilder();
                    str.AppendFormat("\[Expand\]   Name: {0}  CurrentStatus: {1}  NextStatus: {2}", e.Node.Text, e.Node.IsExpanded.ToString(), (!e.Node.IsExpanded).ToString());
                    MessageBox.Show(str.ToString());
                }
            

            good luck

            A Offline
            A Offline
            Adrian Soon
            wrote on last edited by
            #5

            I tink U don't get wat I mean See.. when I click using the mouse. it enter tv_local_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) method In the method I wish to know whether I click on the check or click on the expand slot. is there a way??? -- modified at 5:04 Tuesday 14th August, 2007

            H 2 Replies Last reply
            0
            • A Adrian Soon

              I tink U don't get wat I mean See.. when I click using the mouse. it enter tv_local_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) method In the method I wish to know whether I click on the check or click on the expand slot. is there a way??? -- modified at 5:04 Tuesday 14th August, 2007

              H Offline
              H Offline
              Hessam Jalali
              wrote on last edited by
              #6

              this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand); associates method treeView1_BeforeExpand to this.treeView1.BeforeExpand event and same thing for the next one

              1 Reply Last reply
              0
              • A Adrian Soon

                I tink U don't get wat I mean See.. when I click using the mouse. it enter tv_local_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) method In the method I wish to know whether I click on the check or click on the expand slot. is there a way??? -- modified at 5:04 Tuesday 14th August, 2007

                H Offline
                H Offline
                Hessam Jalali
                wrote on last edited by
                #7

                when you click on the node it's not going to expand or checked ,it's just clicked and for expanding them you must click on the + sign not the node so the click event would not be invoked but the BeforeExpand event would as beforeCheck event for checkboxes So there is no way to find the check or expand with click event (I think your problem was here) but with other four events I said before you can. if you need them to call the same method with expanding-checking and clicking you can do this

                    public Form1()
                    {
                        InitializeComponent();
                        this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1\_comb);
                        this.treeView1.BeforeCheck += new TreeViewCancelEventHandler(treeView1\_comb);
                        this.treeView1.Click += new EventHandler(treeView1\_comb);
                        this.treeView1.BeforeCollapse += new TreeViewCancelEventHandler(treeView1\_comb);
                
                    }
                
                
                    void treeView1\_comb(object sender, EventArgs e)
                    {
                        if (e is TreeViewCancelEventArgs)
                        {
                            TreeViewCancelEventArgs te = e as TreeViewCancelEventArgs;
                
                            if (te.Action == TreeViewAction.Expand)
                            {
                                /\*node expanded\*/
                                MessageBox.Show("On Expand");
                            }
                            else if (te.Action == TreeViewAction.Collapse)
                            {
                                /\*node Collapsed\*/
                                MessageBox.Show("On Collapse");
                            }
                            else if (te.Action == TreeViewAction.ByKeyboard || te.Action == TreeViewAction.ByMouse)
                            {
                                /\*node checked\*/
                                MessageBox.Show("On Check");
                            }
                
                        }
                        else
                        {
                            //the node is just clicked
                            MessageBox.Show("On Click");
                        }
                    }
                

                as you see ,there is single method associated to four events for four different signals and you can simply do this with After events ,just change TreeViewCancelEventArgs to TreeViewEventArgs and associate after events instead of before events hope this one Help

                A 1 Reply Last reply
                0
                • H Hessam Jalali

                  when you click on the node it's not going to expand or checked ,it's just clicked and for expanding them you must click on the + sign not the node so the click event would not be invoked but the BeforeExpand event would as beforeCheck event for checkboxes So there is no way to find the check or expand with click event (I think your problem was here) but with other four events I said before you can. if you need them to call the same method with expanding-checking and clicking you can do this

                      public Form1()
                      {
                          InitializeComponent();
                          this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1\_comb);
                          this.treeView1.BeforeCheck += new TreeViewCancelEventHandler(treeView1\_comb);
                          this.treeView1.Click += new EventHandler(treeView1\_comb);
                          this.treeView1.BeforeCollapse += new TreeViewCancelEventHandler(treeView1\_comb);
                  
                      }
                  
                  
                      void treeView1\_comb(object sender, EventArgs e)
                      {
                          if (e is TreeViewCancelEventArgs)
                          {
                              TreeViewCancelEventArgs te = e as TreeViewCancelEventArgs;
                  
                              if (te.Action == TreeViewAction.Expand)
                              {
                                  /\*node expanded\*/
                                  MessageBox.Show("On Expand");
                              }
                              else if (te.Action == TreeViewAction.Collapse)
                              {
                                  /\*node Collapsed\*/
                                  MessageBox.Show("On Collapse");
                              }
                              else if (te.Action == TreeViewAction.ByKeyboard || te.Action == TreeViewAction.ByMouse)
                              {
                                  /\*node checked\*/
                                  MessageBox.Show("On Check");
                              }
                  
                          }
                          else
                          {
                              //the node is just clicked
                              MessageBox.Show("On Click");
                          }
                      }
                  

                  as you see ,there is single method associated to four events for four different signals and you can simply do this with After events ,just change TreeViewCancelEventArgs to TreeViewEventArgs and associate after events instead of before events hope this one Help

                  A Offline
                  A Offline
                  Adrian Soon
                  wrote on last edited by
                  #8

                  Thanks I had tried ur method however no matter wat I click on it when to the last else statement. which means "if (e is TreeViewCancelEventArgs)" <== Had always been false isn't there a way I can stop a NodeMouseClickEvent in a BeforeExpand Method? if i can do something like this my problem will be solve. that wat I felt coz current situation is that when i click on expand it will call the NodeMouseClickEvent too which my mouse click event had other function to do which will affect the expand method which affect the result I wan to show. thanks private void tv_local_BeforeExpand(object sender, TreeViewCancelEventArgs e) { tv_local.Click -= new TreeNodeMouseClickEventHandler(tv_local_NodeMouseClick); tv_local.Click += new TreeNodeMouseClickEventHandler(tv_local_NodeMouseClick); }

                  H 1 Reply Last reply
                  0
                  • A Adrian Soon

                    Thanks I had tried ur method however no matter wat I click on it when to the last else statement. which means "if (e is TreeViewCancelEventArgs)" <== Had always been false isn't there a way I can stop a NodeMouseClickEvent in a BeforeExpand Method? if i can do something like this my problem will be solve. that wat I felt coz current situation is that when i click on expand it will call the NodeMouseClickEvent too which my mouse click event had other function to do which will affect the expand method which affect the result I wan to show. thanks private void tv_local_BeforeExpand(object sender, TreeViewCancelEventArgs e) { tv_local.Click -= new TreeNodeMouseClickEventHandler(tv_local_NodeMouseClick); tv_local.Click += new TreeNodeMouseClickEventHandler(tv_local_NodeMouseClick); }

                    H Offline
                    H Offline
                    Hessam Jalali
                    wrote on last edited by
                    #9

                    Hi it seems very odd so I upload the source that I tried on 2 PCs and it worked well you can test it if you want here is the link http://rapidshare.com/files/49488493/TreeViewTest.zip.html (does not contain executables) I sent it to you because I had some GUI problems in programming after installing transform pack on Xp so this code is tested on other two computers and can show this kind of errors good luck -- modified at 3:50 Friday 17th August, 2007

                    A 1 Reply Last reply
                    0
                    • H Hessam Jalali

                      Hi it seems very odd so I upload the source that I tried on 2 PCs and it worked well you can test it if you want here is the link http://rapidshare.com/files/49488493/TreeViewTest.zip.html (does not contain executables) I sent it to you because I had some GUI problems in programming after installing transform pack on Xp so this code is tested on other two computers and can show this kind of errors good luck -- modified at 3:50 Friday 17th August, 2007

                      A Offline
                      A Offline
                      Adrian Soon
                      wrote on last edited by
                      #10

                      Hi, thanks. I can't see the link. but thanks for the advice it do gif me some clue to do my stuff. thanks alot.

                      H 1 Reply Last reply
                      0
                      • A Adrian Soon

                        Hi, thanks. I can't see the link. but thanks for the advice it do gif me some clue to do my stuff. thanks alot.

                        H Offline
                        H Offline
                        Hessam Jalali
                        wrote on last edited by
                        #11

                        Sorry I Forgot to put the link :-O http://rapidshare.com/files/49488493/TreeViewTest.zip.html best regards

                        A 1 Reply Last reply
                        0
                        • H Hessam Jalali

                          Sorry I Forgot to put the link :-O http://rapidshare.com/files/49488493/TreeViewTest.zip.html best regards

                          A Offline
                          A Offline
                          Adrian Soon
                          wrote on last edited by
                          #12

                          Hi. care to send to my emaiL? pls thanks.. I can't find my way to download this. :):):)

                          H 1 Reply Last reply
                          0
                          • A Adrian Soon

                            Hi. care to send to my emaiL? pls thanks.. I can't find my way to download this. :):):)

                            H Offline
                            H Offline
                            Hessam Jalali
                            wrote on last edited by
                            #13

                            Ok but I couldn't find your public email :( so try this one http://hjk.4shared.com/ and if did not work give me your Email address best regards

                            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