Results 1 to 10 of 10

Thread: Check and Uncheck treeview when ask for user

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Question Check and Uncheck treeview when ask for user

    Hi

    I have a treeview with checkbox , Ordered me when the user click in Checkbox of the treeview I must to ask if the user want really to check , if Answer equal yes keep checked , but if the user responds NO , I must uncheck

    How can I to it ?

    I tried something like

    Code:
    Private Sub tvLista_NodeCheck(ByVal node As MSComctlLib.node)
        
        Dim mIndAtiva As String
        Dim strSQL As String
        Dim Ds As Object
        Dim mnode As node
        Dim msgNode As String
            
        On Error GoTo ErrorHandler
        
       '----ask to user
       if anwwer is NO  
               node.CHECKED = False
    But no worked

    Tia
    Last edited by mutley; Feb 1st, 2018 at 08:34 AM.

  2. #2

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Check and Uncheck treeview when ask for user

    Hi
    Sorry for the insistence
    but does anyone know how to solve this?

    when the user responds NO to a question when clicking on the checkbox he can not check the treeview checkbox

    https://s18.postimg.org/njx8a3lsp/pergunta.jpg

    Name:  pergunta.jpg
Views: 590
Size:  12.2 KB

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Check and Uncheck treeview when ask for user

    see if this will do what you want
    Code:
       '----ask to user
       If MsgBox("Do you really want to check the box?", vbYesNo, "Are you sure") = vbNo Then
               node.CHECKED = False
       end if
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Re: Check and Uncheck treeview when ask for user

    Thank you

    But when I put
    Code:
      node.CHECKED = False
    The checkbox stiil marked

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Check and Uncheck treeview when ask for user

    Code:
    Dim n As Node, c As Boolean
    Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    If c Then n.Checked = False: c = False
    End Sub
    
    Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
    If Node.Checked Then
        If MsgBox("Do you really want to check the box?", vbYesNo, "Are you sure") = vbNo Then
            c = True
            Set n = Node
        End If
    End If
    End Sub
    i tested this it seemed to work as requested, but only if the checkbox is changed by the mouse
    if you need to trap if the user uses the keyboard to check the box, i think you will have to use some sort of a timer, or force a lostfocus event
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Check and Uncheck treeview when ask for user

    i tested with a timer, worked correctly, however the checkbox was checked
    Code:
    Dim n As Node, c As Boolean
    Private Sub Timer1_Timer()
    If c Then
        n.Checked = False
        c = False
        Timer1.Enabled = False
    End If
    
    End Sub
    
    Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
    
    If Node.Checked Then
        If MsgBox("Do you really want to check the box?", vbYesNo, "Are you sure") = vbNo Then
            c = True
            Set n = Node
            Timer1.Enabled = True
        End If
    End If
    End Sub
    i just had the timer interval of 1 and enabled = false
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Check and Uncheck treeview when ask for user

    Module level node is enough

    Code:
    Option Explicit
    
    Private mCheckedNode As MSComctlLib.Node
    
    Private Sub Form_Load()
        TreeView1.Nodes.Add , , , "1"
        TreeView1.Nodes.Add , , , "2"
        TreeView1.Nodes.Add , , , "3"
        TreeView1.Nodes.Add , , , "4"
        TreeView1.Nodes.Add , , , "5"
    End Sub
    
    Private Sub TreeView1_KeyUp(KeyCode As Integer, Shift As Integer)
        UndoNodeCheck
    End Sub
    
    Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
        UndoNodeCheck
    End Sub
    
    Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
        If Node.Checked Then
            If MsgBox("Do you really want to check this node """ & Node.Text & """?", vbYesNo + vbDefaultButton2 + vbQuestion, "Confirm") = vbNo Then
                Set mCheckedNode = Node
            End If
        End If
    End Sub
    
    Private Sub UndoNodeCheck()
        If Not mCheckedNode Is Nothing Then
            mCheckedNode.Checked = False
            Set mCheckedNode = Nothing
        End If
    End Sub
    Note that, when using the keyboard to check a Node, the message box should close by keyboard too otherwise the node remain checked!!!



  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Check and Uncheck treeview when ask for user

    Note that, when using the keyboard to check a Node, the message box should close by keyboard too otherwise the node remain checked!!!
    yes, i was not real keen to use a timer, but i could not get the key up to be reliable, as i often closed the msgbox by mouse
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Check and Uncheck treeview when ask for user

    Quote Originally Posted by westconn1 View Post
    yes, i was not real keen to use a timer, but i could not get the key up to be reliable, as i often closed the msgbox by mouse
    Sorry, i missed your comment about this issue.



  10. #10
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Check and Uncheck treeview when ask for user

    Yes, you can't change .Checked within the NodeCheck event handler. I tried this with success:

    Code:
    Private Sub tmrDefer_Timer()
        With tmrDefer
            .Enabled = False
            TreeView1.Nodes(CInt(.Tag)).Checked = False
        End With
    End Sub
    
    Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
        With Node
            If .Checked Then
                If MsgBox("Do you really want to check:" _
                        & vbNewLine & vbNewLine _
                        & """" & .FullPath & """", vbOKOnly Or vbYesNo) = vbNo Then
                    tmrDefer.Tag = CStr(.Index)
                    tmrDefer.Enabled = True
                End If
            End If
        End With
    End Sub

Tags for this Thread

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