Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • Last Modified:

Code for Windows Explorer type tree view wanted

Hi
I am trying to create a tree view of departments in a company within each department there are Workcentres.
When I click on a work centre I want the employees to be displayed as files would be in Windows explorer if I clicked on a sub folder. I will be using an Employee Access table which holds all employee details. I am using ADO and VB6.  Currently I am selecting the employees from a dropdown list, but this does not categorise employees in workcentres which would make it easier for the user to browse.

Can the experts help?

Thanks

morrisette  
0
morrisette
Asked:
morrisette
1 Solution
 
mdouganCommented:
The somewhat brief answer is that you will need at least two queries.  One to fill the treeview, and one to select the employees for a given workcenter.

The query for filling the treeview should look something like this:

Select D.DepartmentID, D.DepartmentName,
       W.WorkCenterID, W.WorkCenterName
From Department D, WorkCenter W
Where W.DepartmentID = D.DepartmentID
Order by D.DepartmentName, W.WorkCenterName

Then When filling the TreeView, you code might look like this:

Dim xNode As Node
Dim i As Long
Dim lDeptID as long
   
    On Error Resume Next
   
    Screen.MousePointer = vbHourglass
   
    ' clear out the tree
    tvwObjects.Nodes.Clear
   
    ' set the imagelist to an image list on the form
    Set tvwObjects.ImageList = Nothing
    Set tvwObjects.ImageList = imlObjects
   
    ' assuming that you've already opened the recordset
    While Not RS.EOF
        ' if are now on a new department, add the department node
        if lDeptID <> RS("departmentID").Value Then
           Set xNode = tvwObjects.Nodes.Add , , "D" & RS("departmentid").Value, RS("departmentname").Value, 1)
           lDeptID = RS("departmentID").Value
        End if
        ' now, add the workcenter node don't worry about setting the relationships yet
        Set xNode = tvwObjects.Nodes.Add , , "W" & RS("workcenterid").Value, RS("workcentername").Value, 1)
        xNode.Tag = "D" & RS("departmentID").Value
        RS.MoveNext
    Wend
   
    ' now, go through and set the hierarchical relationships
    For Each xNode In tvwObjects.Nodes
      sParent = xNode.Tag
      If Len(xNode.Tag) > 0 Then  ' Don't try and reparent the Departments!
          Set xNode.Parent = tvwObjects.Nodes(xNode.Tag)
      End If
    Next
   
    ' these numbers correspond to folder and leaf icons respectively
    ' so, set the appropriate icons
    For Each xNode In tvwObjects.Nodes
        If xNode.Children > 0 Then
            xNode.Image = 1
        Else
            xNode.Image = 2
        End If
    Next

    tvwObjects.Refresh
 
    Screen.MousePointer = vbDefault


Then, in the node_click event of the tree, you can use the tvwObjects.SelectedNode.Key (Look at the first character on the left and if it's a W it's a Workcenter else they've clicked on a Department) truncate the first character and you have the workcenter id which you can then use to run the second query that gets all employees that work for that workcenter.  I'd suggest that you use a ListView control for the area to the right of the treeview to display the resulting employees.  The ListView has a report mode that allows you to display the Employee details in a grid-like format, similar to the List view of Windows Explorer, or, there are Icon views that let you display them as Icons, also like Windows Explorer....
   
0
 
morrisetteAuthor Commented:
Thanks mdougan

I will try this and let you know. it looks very good.

morrisette
0
 
mdouganCommented:
Depending on how many departments and workcenters you have, if it seems to take a long time to load the tree let me know, I have a routine that can be used to lock the treeview window so that it does not update after each node is added which really slows down the loading.  You wont notice it if you're just adding 50 or 100 nodes, but if you have several hundred, you might.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
magoo2Commented:
morrisette,

here is a link to a class sample for treeview that may also be helpful...

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=34439

0
 
morrisetteAuthor Commented:
Thanks magoo2. I will check this out

morrisette
0
 
morrisetteAuthor Commented:
Hi mdougan

could I have the routine to lock the tree during update, please.
morrisette
0
 
mdouganCommented:
Here is the same code above (more or less) with the additional routine to lock the window.  Let me know if you need any samples of loading the listview control in report mode when someone clicks on the tree:

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

Dim CN As ADODB.Connection
Dim RS As ADODB.Recordset

Private Sub Command1_Click()
Dim xNode As Node
Dim i As Long
   
    On Error Resume Next
   
    Screen.MousePointer = vbHourglass
   
' Here, you need to Lock the Tree
    TreeRedraw tvwObjects.hwnd, False

    tvwObjects.Nodes.Clear
   
    Set tvwObjects.ImageList = Nothing
    Set tvwObjects.ImageList = imlObjects
   
    Set xNode = tvwObjects.Nodes.Add(, , "ROOT", "ROOT", 1)
     
    While Not RS.EOF
        Set xNode = tvwObjects.Nodes.Add("ROOT", tvwChild, "C" & RS("name_object_no").Value, RS("name").Value, 1)
        xNode.Tag = "C" & RS("parent_name_object_no").Value
        RS.MoveNext
    Wend
   
    For Each xNode In tvwObjects.Nodes
      sParent = xNode.Tag
      If Len(xNode.Tag) > 0 Then        ' Don't try and reparent the ROOT !
          If xNode.Tag = "C0" Then
              xNode.Tag = "ROOT"
          End If
          Set xNode.Parent = tvwObjects.Nodes(xNode.Tag)
      End If
    Next
   
    For Each xNode In tvwObjects.Nodes
        If xNode.children > 0 Then
            xNode.Image = 1
        Else
            xNode.Image = 3
        End If
    Next
   
    tvwObjects.Nodes("ROOT").Expanded = True
    tvwObjects.Refresh

' Here you need to unlock the Tree
    TreeRedraw tvwObjects.hwnd, True

    Screen.MousePointer = vbDefault
   
End Sub

Private Sub TreeRedraw(ByVal lHwnd As Long, ByVal bRedraw As Boolean)

If bRedraw Then
        LockWindowUpdate 0
    Else
        LockWindowUpdate lHwnd
    End If
End Sub
0
 
DanRollinsCommented:
Hi morrisette,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept mdougan's comment(s) as an answer.

morrisette, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now