Treeview Control

I have a question which is related to the treeview control.

I am creating an application for policies and procedures within my company.  The treeview works like this so far it is reading all of the information from a SQL database.  If someone is willing to give me some real help I would be willing to send the code to them.

Division
   |
    Department
         |
          Policy Name
               |
                Procedure Name
                     |
                      Procedure Step
                            |
                             Procedure Step Text (RichText)

What I am having trouble doing is this.  When the user selects the procedure Step, I want the text to display in a rich text box to the right of it.  That part works, but then when the user moves to another step or procedure or basically anything other than the step they are working on from the treeview, I want it to prompt them to save that step, and then move the focus to the item they have chosen.  I will start this out at 100 points, but if you can provide me with real detailed help, I will be willing to up the points a little.
LVL 1
Steve GronerLead Systems EngineerAsked:
Who is Participating?
 
vspeterCommented:
MelissaC solution was to place to code in the NODECLICK event. What I am saying is that, place in the CLICK event (TreeView1_Click) .
0
 
MelissaCCommented:
I think what you need is to handle the event "LostFocus",but there's only the treeview.lostfocus, not a treeview.node.lostfocus. So I think I would solve this with an event "treeview.nodeclick":
The first time a nodeclick event occurs, you store the node ID in a variable. The second time it occurs, you read the variable to know which node the user COMES FROM (the nodeclik event had a parameter that shows which node has been clicked this time, not which one was active before). Hope this solves your problem.

0
 
Steve GronerLead Systems EngineerAuthor Commented:
Can you be a little more specific.  Perhaps you can throw up some code, I think that would help alot.  With a single event to handle everything, it can get confusing.  I would appreciate any code snippets you can provide.  Then I will award the point to you.  Thanks.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
MelissaCCommented:
Well, you've added the treeview in your form, now you can go to source-view and select treeview1 from the combo-box above the source. Then select from the combo-box beside it the "nodeclick event" and VB will generate this code for you:

Private Sub TreeView1_NodeClick(ByVal Node As ComctlLib.Node)
End Sub

Make a global variable called "PreviousNodeID" and initialize it with NULL. Use code like this:

Private Sub TreeView1_NodeClick(ByVal Node As ComctlLib.Node)
  SaveItem(PreviousItemID)  'First handle the things you
                    'wanted to do with the previously selected                     'node
  PreviousItemID=Node   'now store the current item in the                         'variable
End Sub


private sub SaveItem(Node As ComctlLib.Node)
   if not(node=NULL) then
      dowhatyouwanttodo()
   end if  
end sub


0
 
Steve GronerLead Systems EngineerAuthor Commented:
Ok, i saw this the first time.  Have you tested this process.  It does not work as you would expect.  The focus does not return even if I tell it to.  I have used the following code, tell me if you think it is correct.

Private Sub tv_NodeClick(ByVal Node As MSComctlLib.Node)

    Dim Retval As Long
   
    If NeedsSaved = True Then
        Retval = MsgBox("You must first save this procedure step, do you want to do this now?", vbYesNo + vbExclamation, "Save Now?")
        Select Case Retval
            Case vbYes
                MsgBox "Would have saved " & CurrentKey & " _                       moving to " & Node.Index
                NeedsSaved = False
                Set tv.SelectedItem = tv.Nodes(Node.Index)
            Case vbNo
                MsgBox "Not being saved"
                NeedsSaved = False
                Set tv.SelectedItem = tv.Nodes(Node.Index)
        End Select
    End If

End Sub

The message box under the 'YES' answer reports the correct information but the focus does not get to the new item.  can you try this and see what you think.  It is not working for me.
0
 
Steve GronerLead Systems EngineerAuthor Commented:
Damn refresh.  Sorry.  I did not see the code.  Go ahead and answer again.  I will award the points.
0
 
Steve GronerLead Systems EngineerAuthor Commented:
One more quick question.  what type of variable should PreviewItemID be, a LONG or a NODE.


I need to check the TAG before the code in the node click.  It should only be fired if the node.tag = "ProcedureStep"

If I check the PreviousItemID.TAG Property it tells me that it is not initialized.

Any thoughts.
0
 
vspeterCommented:
I think you are almost there. But there is one slight problem. Consider the answer bellow.

DO NOT place the code

    If NeedsSaved = True Then
        Retval = MsgBox("You must first save this procedure step, do you want to do this now?", vbYesNo + vbExclamation, "Save Now?")
        Select Case Retval
            Case vbYes
                MsgBox "Would have saved " & CurrentKey & " _                       moving to " & Node.Index
                NeedsSaved = False
                Set tv.SelectedItem = tv.Nodes(Node.Index)
            Case vbNo
                MsgBox "Not being saved"
                NeedsSaved = False
                Set tv.SelectedItem = tv.Nodes(Node.Index)
        End Select
    End If

that you posted in NodeClick event. Place it in the TreeView Click event.

Cheers
0
 
Steve GronerLead Systems EngineerAuthor Commented:
Sorry, but MelissaC had answered the question already.  Sorry, but I did not refresh my screen when I read her message.  Her solution helped, I must award the points to her.


0
 
MelissaCCommented:
I think you can use  a LONG best, just store the nodeID in it, that's all you need to find the node that was previously active. If you store it in a node, you have to copy the node's properties.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.