Common Control Basics Using TreeView, ListView, and ImageList - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Common Control Basics Using TreeView, ListView, and ImageList

Description:

Microsoft , Windows , and ActiveX are registered trademarks of ... Like a sub folder. Have parents. Can have children. And so on... Treeview.Nodes.Add Method ... – PowerPoint PPT presentation

Number of Views:514
Avg rating:5.0/5.0
Slides: 35
Provided by: M336
Category:

less

Transcript and Presenter's Notes

Title: Common Control Basics Using TreeView, ListView, and ImageList


1
Common Control BasicsUsing TreeView, ListView,
and ImageList
  • Sarah Worthen
  • United Technologies
  • Pratt Whitney

Microsoft, Windows, and ActiveX are registered
trademarks of Microsoft Corporation
2
Who am I?
  • Sarah Worthen
  • Quality Engineer and IT Lead
  • United Technologies Pratt Whitney
  • Global Service Partners
  • Global Quality Assurance
  • Speaker at the CT Access Users Group
  • Published in Access Advisor Magazine
  • Email sarahmw42_at_sbcglobal.net

3
In this section
  • ImageList
  • TreeView
  • ListView

4
ImageList
  • The ImageList is a control that holds the icons
    you will use in your application. Various file
    formats can be inserted into the control, and
    each image will be the same size.

5
Exercises ImageList Control
  • In Form Design View
  • Add ImageList To Form
  • Point to InsertActiveX Control
  • At the dialog, select Microsoft ImageList
  • Click OK
  • View Image List Properties
  • Right Click the ImageList
  • On the Menu, Point to ImageList Object
  • On the Menu, Point to Properties
  • Add pictures in the Properties Dialog

6
TreeView
  • TreeView
  • Like the Folders Side of Windows Explorer
  • Made up of Nodes
  • Nodes
  • Are like Tree Branches
  • Are related to create a hierarchy
  • Respond to Click and Drag and Drop Events
  • Are Expandable and Collapsible

7
Exercises TreeView Control
  • In Form Design View
  • Add TreeView To Form
  • Point to InsertActiveX Control
  • At the dialog, select Microsoft TreeView
  • Click OK
  • View TreeView Properties
  • Right Click the TreeView
  • On the Menu, point to TreeCtl Object
  • On the Menu, Point to Properties

8
Nodes
  • Branches of a Tree
  • Arranged in a hierarchy
  • Respond to
  • Click Events
  • Drag and Drop
  • Renaming
  • Expandable ()
  • Collapsible (-)
  • Can have Icons

9
Relationships Create the Hierarchy
  • Nodes are related
  • Parent Nodes
  • Like a folder
  • Can have children
  • Child Nodes
  • Constant tvwChild
  • Like a sub folder
  • Have parents
  • Can have children
  • And so on

10
Treeview.Nodes.Add Method
  • Syntax Add(Relative, Relationship, Key,
    Text, Image, SelectedImage)
  • Set mNode TreeView.Nodes.Add ( _
  • Relative, Relationship)
  • With mNode
  • .Text rst(Field)
  • .Image ImageList.Key
  • End With
  • To make your code easy to read, set the
    properties after you add the
  • Node, rather than within the Add Method.
    Relative and Relationship
  • are only available in the Add Method.

11
Treeview.Nodes.Add Arguments
Syntax Add(Relative, Relationship, Key,
Text, Image, SelectedImage)
12
VBA Exercise Create nodes (1)
  • Create a Parent node with a fixed text value.
  • Dim mNodeParent as Node
  • Set mNodeParent TreeView.Nodes.Add()
  • With mNodeParent
  • .Text "Parent 1"
  • .Image "Closed"
  • End With

13
VBA Exercise Create nodes (2)
  • Create a Child Node with a fixed text value
  • Dim mNodeChild as Node
  • Set mNodeChild TreeView.Nodes.Add( _
  • mNodeParent.Index, tvwChild)
  • With mNodeChild
  • .Text "Child 1"
  • .Image "Closed"
  • End With

14
VBA Exercise Create Nodes (3)
  • Create a node by looping through a DAO recordset
  • Dim db as database
  • Dim rst as recordset
  • Dim mNode as Node
  • Set rst db.OpenRecordset(SQL)
  • Do Until rst.EOF
  • set mNode TreeView.Nodes.Add()
  • mNode.Text rst(field name)
  • rst.MoveNext
  • Loop

15
VBA Exercise Create Nodes (4)
  • Create parents and children at the same time
  • Set rstParent db.OpenRecordset(SQL)
  • Do Until rstParent.EOF
  • set mNodeParent TreeView.Nodes.Add()
  • mNodeParent.Text rstParent(field)
  • Set rstChild db.OpenRecordset(SQL)
  • Do Until rstChild.EOF
  • Set mNodeChild TreeView.Nodes.Add( _
  • mNodeParent, tvwChild)
  • mNodeChild.Text rst(field)
  • rstChild.MoveNext
  • rstParent.MoveNext
  • Loop

Note rstChild based on current rstParent.field
value
16
ListView
  • ListView
  • Like the Details side of the Windows Explorer
  • Made up of ListItems
  • Supports Icons and multiple views
  • Report View ideal for record display
  • ListItems
  • The lines of the listview
  • Respond to click events
  • Respond to Drag and Drop

17
Exercises ListView Control
  • In Form Design View
  • Add ListView To Form
  • Point to InsertActiveX Control
  • At the dialog, select Microsoft ListView
  • Click OK
  • View ListView Properties
  • Right Click the TreeView
  • Point to TreeCtl Object
  • Point to Properties

18
ListItems
  • Lines of a ListView
  • Not hierarchical
  • Respond To
  • Click Events
  • Drag and Drop
  • Renaming
  • Can have icons
  • Many properties

19
ListView.ListItems.Add Method
  • Syntax Add(Index, Key, Text, Icon,
    SmallIcon)
  • Set mListItem ListView.ListItems.Add()
  • With mListItem
  • .Text rst(field)
  • .Icon LargeIconImageList.Key
  • .SmallIcon SmallIconImageList.Key
  • .SubItems(1) rst(another field)
  • End With
  • To make your code easy to read, set the
    properties after you add the
  • ListItem, rather than within the Add Method.

20
ListView.ListItems.Add Arguments
Syntax Add(Index, Key, Text, Icon,
SmallIcon)
Icon and Small Icon will display depending on the
current view. View constants are lvwIcon,
lvwSmallIcon, lvwList, lvwReport.
21
VBA Exercise Create ListItems (1)
  • Create a ListItem with a fixed Text Value
  • Dim mListItem as ListItem
  • Set mListItem ListViewIcon.ListItems.Add()
  • With mListItem
  • .Text "List Item A"
  • .Icon "Closed"
  • .SmallIcon "Closed"
  • End With

22
VBA Exercise Create ListItems (2)
  • Create ListItems by looping through a DAO
    recordset
  • Dim db as database
  • Dim rst as recordset
  • Dim mListItem as ListItem
  • Set rst db.OpenRecordset(SQL)
  • Do Until rst.EOF
  • set mListItem ListView.ListItems.Add()
  • mListItem.Text rst(field name)
  • rst.MoveNext
  • Loop

23
Wrap Up
  • In this session, you
  • Created and added images to an ImageList
  • Created a TreeView and added Nodes
  • Created a ListView and added ListItems

24
Improved Method for Adding Nodes and ListItems to
a Record Browser
  • Sarah Worthen
  • United Technologies
  • Pratt Whitney

25
Our Current Method
  • Create a recordset for each level of data in our
    browser
  • Open the Highest level and iterate through each
    row. While on the current row, open the child
    level recordset.

26
Improved Method
  • Open 1 recordset with the required data for each
    level of the tree
  • Iterate through the recordset
  • While on each row, check field values to
    determine which nodes and listitems to add to the
    browser

27
Getting Started
  • Create a query containing all required fields.

28
The Code Step 1 Variables
  • Dim db As Database
  • Dim rst As Recordset
  • Dim mNodeParent As Node
  • Dim mNodeChild As Node
  • Dim mListItem As ListItem
  • new vars
  • Dim strLastParent As String
  • Dim strLastChild As String
  • Dim strLastListItem As String

29
Step 2 Get everything ready
  • ListView.ListItems.Clear
  • TreeView.Nodes.Clear
  •  
  • Now I will open the recordset. This is the only
    time I will query the database.
  •  
  • Set db CurrentDb
  • Set rst db.OpenRecordset("qryBrowserData")
  •  
  • rst.MoveFirst
  • Do Until rst.EOF

30
Step 3 The Parent Label
  • PARENT
  • If rst("Parent") ltgt strLastParent Then
  • Set mNodeParent TreeView.Nodes.Add()
  • With mNodeParent
  • .Text rst("Parent")
  • End With
  • strLastParent rst("parent")

31
Step 4 The Child Label
  • CHILD
  • If rst("child") ltgt strLastChild Then
  • Set mNodeChild TreeView.Nodes.Add( _
  • mNodeParent.Index, tvwChild)
  • With mNodeChild
  • .Text rst("Child")
  • End With
  • strLastChild rst("child")

32
Step 4 The Grandchild Label
  • GRANDCHILD
  • If rst("ListItem") ltgt strLastListItem Then
  • Set mListItem ListView.ListItems.Add()
  • With mListItem
  • .Text rst("ListItem")
  • .SubItems(1) rst("Detail1")
  • End With
  • Else
  • 'do not add ListItem. Loop at end of
    proc.
  • End If

33
Step 6 End those Ifs
  • Else rst("child") strLastChild
  • GoTo GRANDCHILD
  • End If
  • Else If rst("Parent") strLastParent
  • GoTo CHILD
  • End If
  • rst.MoveNext
  • Loop

34
The complete code
  • Private Sub cmdLoad_Click()
  •  
  • Dim db As Database
  • Dim rst As Recordset
  • Dim mNodeParent As Node
  • Dim mNodeChild As Node
  • Dim mListItem As ListItem
  • new vars
  • Dim strLastParent As String
  • Dim strLastChild As String
  • Dim strLastListItem As String
  •  
  • ListView.ListItems.Clear
  • TreeView.Nodes.Clear
  •   
  • Set db CurrentDb
  • Set rst db.OpenRecordset("qryBrowserData")
  •  
  • rst.MoveFirst
  • CHILD
  • If rst("child") ltgt strLastChild Then
  • Set mNodeChild TreeView.Nodes.Add( _
  • mNodeParent.Index, tvwChild)
  • With mNodeChild
  • .Text rst("Child")
  • End With
  • strLastChild rst("child") 
  • GRANDCHILD
  • If rst("ListItem") ltgt strLastListItem Then
  • Set mListItem ListView.ListItems.Add()
  • With mListItem
  • .Text rst("ListItem")
  • .SubItems(1) rst("Detail1")
  • End With
  • Else
  • 'do not add ListItem. Loop at end of
    proc.
  • End If 
  • Else child last child
Write a Comment
User Comments (0)
About PowerShow.com