Solved

Treeview Control

Posted on 1998-09-28
10
227 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:sgroner
  • 5
  • 3
  • 2
10 Comments
 
LVL 2

Expert Comment

by:MelissaC
ID: 1487183
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
 
LVL 1

Author Comment

by:sgroner
ID: 1487184
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
 
LVL 2

Expert Comment

by:MelissaC
ID: 1487185
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
 
LVL 1

Author Comment

by:sgroner
ID: 1487186
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
 
LVL 1

Author Comment

by:sgroner
ID: 1487187
Damn refresh.  Sorry.  I did not see the code.  Go ahead and answer again.  I will award the points.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:sgroner
ID: 1487188
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
 
LVL 2

Expert Comment

by:vspeter
ID: 1487189
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
 
LVL 1

Author Comment

by:sgroner
ID: 1487190
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
 
LVL 2

Accepted Solution

by:
vspeter earned 100 total points
ID: 1487191
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
 
LVL 2

Expert Comment

by:MelissaC
ID: 1487192
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now