Results 1 to 15 of 15

Thread: Freaky Treeview Checkbox Thing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230

    Freaky Treeview Checkbox Thing

    I am trying to get the users confirmation when clicking a checkbox in the treeview control.
    Code snippet:

    Code:
    Sub treeGuideline_NodeCheck(ByVal Node As MSComctlLib.Node)
     If Node.Children > 0 Then 'Parent
            msgreturn = MsgBox("Checking this node will select all sections and guidelines below it. Do you wish to continue?", vbYesNo + vbQuestion)
    
    '       If msgreturn = vbNo Then
    '           Node.Checke= False '***
    '        Else
    '             bDirty = True
            'All the work done when checking/unchecking is done in the
            'following 2 subs.
            CheckNodes frmTree.treeGuideline, Node, Node.Checked, True
            checkforsiblings frmTree.treeGuideline, Node, Node.Checked
    '        End If
    
    End if
    
    ' some other stuff
    
    End Sub
    In debug mode, I can see the checkbox clear when the line *** is executed, but at the end of the sub, the check box rechecks itself. Nothing I do inside the sub, as far as checking/unchecking is persistent, when the sub ends, it sets the value to whatever was going to be when the user clicked. Is there any way to cancel a checkbox click?
    Shawn Hull
    VB6, SP3 (Professional Edition)

  2. #2
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Freaky Treeview Checkbox Thing

    Originally posted by a2427


    VB Code:
    1. Sub treeGuideline_NodeCheck(ByVal Node As MSComctlLib.Node)
    2.  If Node.Children > 0 Then 'Parent
    3.         msgreturn = MsgBox("Checking this node will select all sections and guidelines below it. Do you wish to continue?", vbYesNo + vbQuestion)
    4.  
    5. '       If msgreturn = vbNo Then
    6. '           Node.Checke= False '***
    7. '        Else
    8. '             bDirty = True
    9.         'All the work done when checking/unchecking is done in the
    10.         'following 2 subs.
    11.         CheckNodes frmTree.treeGuideline, Node, Node.Checked, True
    12.         checkforsiblings frmTree.treeGuideline, Node, Node.Checked
    13. '        End If
    14.  
    15. End if
    16.  
    17. ' some other stuff
    18.  
    19. End Sub
    Are you sure thats the code you want debugged? All your code lines are commented out except for 5 of them, including your *** line. Also, Typo alert :Node.Checke= False

    -Lou

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230
    Try it again,

    I had some temp stuff in there

    Code:
    Sub treeGuideline_NodeCheck(ByVal Node As MSComctlLib.Node)
    Dim tnode As Node 'Node to pass
    Dim msgreturn As Long
    Dim chkflag As Boolean
    Dim gid As String
    
    
    If Node.Checked = True Then
    
    
        If Node.Children > 0 Then 'Parent
            msgreturn = MsgBox("Checking this node will select all sections and guidelines below it. Do you wish to continue?", vbYesNo + vbQuestion)
    
            If msgreturn = vbNo Then
                Node.Checked= False '***
            Else
                 bDirty = True
            'All the work done when checking/unchecking is done in the
            'following 2 subs.
            CheckNodes frmTree.treeGuideline, Node, Node.Checked, True
            checkforsiblings frmTree.treeGuideline, Node, Node.Checked
            End If
    End If
    End if
    At least I know someone is paying attetion, any help?
    Shawn Hull
    VB6, SP3 (Professional Edition)

  4. #4
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Hmmmmm......tried your code and it does what you say and I can't figure a way around it.

    Has to be a bug.....I have VB 6 EE SP 3 as well.

  5. #5
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Strange!
    That check event Seems to freeze the treeview up to the point whare you can't change its check value.

    But, I got something to work.
    Put a timer control on your form, set its interval to 10, enabled = false.
    Then, add the following code:

    VB Code:
    1. Private Sub Timer1_Timer()
    2.     treeGuideline.Nodes(Val(Timer1.Tag)).Checked = False
    3.     Timer1.Enabled = False
    4. End Sub

    And, finally alter your existing at the vbNo, to the following:

    VB Code:
    1. If msgreturn = vbNo Then
    2.                     Timer1.Tag = Node.Index
    3.                     Timer1.Enabled = True
    4.                     Exit Sub
    5.                 Else

    Run it, and report back what happens. I hope it solves
    your problem though.

    -Lou

  6. #6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230
    I had thoght about the timer but did not want to go there because of a user can start clicking other checkboxes before the first timer event is fired. I might have take that chance if there is no other way.

    Thanks guys any other ideas.
    Any other ideas.
    Shawn Hull
    VB6, SP3 (Professional Edition)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230

    Unhappy

    I had to use the timer solution to this prroblem with a time of a about 10 th of second. It does not matter what you do inside the check event, the end result is the same. Also anything that breaks the event, hangs the sub- like launching a modal form. I am really beginning to dislike the treeview.
    Shawn Hull
    VB6, SP3 (Professional Edition)

  9. #9
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    It sure is a pain!

    Perhaps someone has made a custom Treeview? Or, perhaps you
    could sub-class it? ie.., intercept its windows messages, and
    respond when you get the Check message.

    BTW, Like i previously mentioned, 10 works for me, that is
    10/1000, or 1 oneHundreth of a sec. But, Just as long as its
    under 1 sec, you should be good to go.

    Now, you COULD make it invisible on a check event, and have the
    timer turn it back, thus preventing users from checking before
    your code is completed.

    -Lou

  10. #10
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    I admit I did not examine this thread in detail, but this may apply. There is a known bug when using the checkbox style with the treeview, and a workaround for it. Check out this thread and see if it applies:

    http://www.vbforums.com/showthread.p...hreadid=100901
    "It's cold gin time again ..."

    Check out my website here.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230

    Talking

    Thanks Lou, I had set the timer interval to 20 and it was definately fast enough. I kind of like the invisible idea. As far as subclassing, I am not at that at level but I get the concept.

    Bruce,

    I ran into that problem the first day with the treeview and actually used the code in that post to check my checkboxes and fix the bug.

    This forum I has bailed me out several times on my current project.

    Thanks all.
    Shawn Hull
    VB6, SP3 (Professional Edition)

  12. #12
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by BruceG
    I admit I did not examine this thread in detail, but this may apply. There is a known bug when using the checkbox style with the treeview, and a workaround for it. Check out this thread and see if it applies:

    http://www.vbforums.com/showthread.p...hreadid=100901
    Bruce, that article only talks about the Node_Click event. I still have a problem here.

    I want to implement a functionality where I shall verify the node checked and if a certain condition is false, I need to uncheck that node.

    I have a treeview, and using the API calls provided in this article, I set its Checkboxes property, the .Checkboxes property is set to False. I write the following code in my form:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetWindowLong Lib "user32" Alias _
    4.     "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    5.  
    6. Private Declare Function SetWindowLong Lib "user32" Alias _
    7.     "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
    8.     ByVal dwNewLong As Long) As Long
    9.  
    10. Private Sub Form_Load()
    11.     Const TVS_CHECKBOXES = &H100
    12.     Const GWL_STYLE = (-16)
    13.    
    14.     Dim CurStyle As Long
    15.     Dim Result As Long
    16.    
    17.     Dim nodX As Node
    18.     Set nodX = TreeView1.Nodes.Add(, , "R", "Root")
    19.     Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C1", "Child 1")
    20.     Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C2", "Child 2")
    21.     Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C3", "Child 3")
    22.     Set nodX = TreeView1.Nodes.Add("R", tvwChild, "C4", "Child 4")
    23.     nodX.EnsureVisible
    24.    
    25.     TreeView1.LabelEdit = tvwManual
    26.     TreeView1.Style = tvwTreelinesText   ' Style 4.
    27.     TreeView1.BorderStyle = vbFixedSingle
    28.     CurStyle = GetWindowLong(TreeView1.hwnd, GWL_STYLE)
    29.     Result = SetWindowLong(TreeView1.hwnd, GWL_STYLE, _
    30.              CurStyle Or TVS_CHECKBOXES)
    31. End Sub
    32.  
    33. Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
    34.     If Node.Text = "Child 2" Then
    35.         Node.Checked = False
    36.     End If
    37. End Sub

    Now, if I set .Checkboxes property of the treeview control to False, the Node_Check event is never fired. And if I set the .Checkboxes property to True, the event is duly fired, and after Node.Checked = False, the node is unchecked, but when the procedure exits, the node has been checked back. This is a behaviour not addressed by the article you mentioned. And I think that's exactly the problem reported in this thread. Since I am facing a similar problem, I am curious to know the solution.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  13. #13
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Got It!

    The key is the mouse_up event occurs After the node_check event.

    Heres some code to illustrate how its done:

    VB Code:
    1. Dim let_me_check As Boolean
    2. Dim mouse_down As Boolean
    3. Dim C_NODE As Node
    4.  
    5. Private Sub Form_Load()
    6. Dim nodX As Node   ' Declare the object variable.
    7. Dim NodY As Node
    8.  
    9. Dim I As Integer   ' Declare a counter variable.
    10.     For I = 1 To 4
    11.        Set nodX = TreeView1.Nodes.Add(, , , "Node " & CStr(I))
    12.         For J = 1 To 4
    13.             Set NodY = TreeView1.Nodes.Add(nodX, tvwChild, , "Node " & I & J)
    14.         Next J
    15.     Next I
    16. End Sub
    17.     'it doesn't seem needed, but as a looping safety precaution
    18.     'the mouse_down boolean signifies that, if a node_check occured
    19.     'while mouse_down is true, it occured from the user physically
    20.     'checking it.
    21.     'ie.. if you uncheck or check it programically, You won't start looping
    22.     'if you use it correctly
    23. Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    24.     mouse_down = True
    25. End Sub
    26.  
    27. Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    28.     'of course, mouse_up occurs after mouse_down, so we dont need to check the mouse_down bool
    29.     'but, still reset it to false
    30.         mouse_down = False
    31.         'if a node was checked, then let_me_check indicates you need to inspect the node value
    32.         'against your conditions. Just for testing,
    33.         'Im checking the passed node value against the check controls check value.
    34.         'In this routine, it allows un-checking when Check1 = 1,
    35.         'but not checking when check1.Value = 1
    36.         If let_me_check = True Then     'place your inspection routine inside this if...endif
    37.             If (Check1.Value = 1) And (C_NODE.Checked = True) Then
    38.                C_NODE.Checked = False
    39.             End If
    40.             let_me_check = False
    41.         End If
    42. End Sub
    43.  
    44. Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
    45.     'if the check occured from a mouse_down event, then do a setup
    46.     'on the variables needed for your inspection routine.
    47.     If mouse_down = True Then
    48.         Set C_NODE = Node
    49.         let_me_check = True
    50.     End If
    51. End Sub

    and I attached my development project.

    -Hope this helps
    -Lou

  14. #14

  15. #15
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    No, I am posting under just the name 'honeybee'. I don't wanna post under a different name and divide up the posts between them. I want to get to 4096 fast!!

    I guess I have been dumb enough not to have thought of MouseUp. Thanks for bringing it up, I shall give it a thought.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width