Link to home
Start Free TrialLog in
Avatar of Steve Groner
Steve Groner

asked on

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.
Avatar of MelissaC
MelissaC

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.

Avatar of Steve Groner

ASKER

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.
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


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.
Damn refresh.  Sorry.  I did not see the code.  Go ahead and answer again.  I will award the points.
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.
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
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.


ASKER CERTIFIED SOLUTION
Avatar of vspeter
vspeter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.