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

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

Using ListView

(Relates to http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20787492.html )


How do I;

Add Items,
Read data individually from each field of a highlighted item (eg, read the data in column 4 of the currently selected item.),


Matt.
0
pyromatt
Asked:
pyromatt
  • 2
3 Solutions
 
aelatikCommented:
This is the way to add new items :

Dim Item As ListItem

Private Sub Command1_Click()
    Set Item = ListView1.ListItems.Add(, , "Item 1")
        Item.SubItems(1) = "Item 2"
        Item.SubItems(2) = "Item 3"
        Item.SubItems(3) = "Item 4"
End Sub

Private Sub Form_Load()

    ' This part you can set in the control properties also
        ListView1.View = lvwReport
        ListView1.ColumnHeaders.Add , , "One"
        ListView1.ColumnHeaders.Add , , "Two"
        ListView1.ColumnHeaders.Add , , "Three"
        ListView1.ColumnHeaders.Add , , "Four"
End Sub
0
 
aelatikCommented:
This way you can read the selected item and subitems

    ' View the main item
        MsgBox ListView1.ListItems(ListView1.SelectedItem.Index)
    ' View the sub items
        MsgBox ListView1.ListItems(ListView1.SelectedItem.Index).ListSubItems(1)
        MsgBox ListView1.ListItems(ListView1.SelectedItem.Index).ListSubItems(2)
        MsgBox ListView1.ListItems(ListView1.SelectedItem.Index).ListSubItems(3)
0
 
Ryan ChongCommented:
and make sure the selecteditem is exist, or the code may rise an error, to ensure that, add this to check whether an item is selected or not:

..
'Quit if nothing is selected
If ListView1.SelectedItem Is Nothing Then Exit Sub
..

cheers
0

Featured Post

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!

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