Results 1 to 7 of 7

Thread: How to use ListView anyone plzzzz. . . .

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    How to use ListView anyone plzzzz. . . .

    Im using Listview my problem is that i don't know how to get the values in listview i have 5 columns first one qty, unit,pcode,desc and price. I want to get the values of the price in listview and add them. . . . .

    can anyone help me with this problem i would be grateful. . . .

  2. #2
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: How to use ListView anyone plzzzz. . . .

    This is a copy of code in one of my programs so it displays data in different columns:

    Code:
            ListView1.ListItems.Add , , Text1.Text
            ListView1.ListItems.SubItems(1) = Text2.Text
            ListView1.ListItems.SubItems(2) = Text3.Text

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: How to use ListView anyone plzzzz. . . .

    A short ListView tutorial:

    Code:
    Option Explicit
    Private Sub Form_Load()
     With ListView1
      .View = lvwReport 'to get rows/cols
      .FullRowSelect = True 'optional
      .GridLines = True 'optional
      .Move 0, 0, ScaleWidth, ScaleHeight
     End With
     LoadColumnHeaders
     LoadData
    End Sub
    
    Private Sub Form_Resize()
     ListView1.Move 0, 0, ScaleWidth, ScaleHeight
     SizeHeaders
    End Sub
    Private Sub LoadColumnHeaders()
     'note this can be done at design time
     ' by right clicking the LV & selecting properties
     Dim i As Long
     Dim a() As String
     'your column headers
     a = Split("Qty,Unit,Pcode,Desc,Price", ",")
     For i = 1 To 5
      With ListView1.ColumnHeaders.Add
       .Text = a(i - 1)
      End With
     Next
     SizeHeaders
    End Sub
    Private Sub SizeHeaders()
     Dim i As Long
     For i = 1 To 5 '5 equally sized columns
      With ListView1
       .ColumnHeaders(i).Width = .Width \ 5 - 60
       ' the - 60 above prevents the Horiz
       ' scroll bar from showing
      End With
     Next
    End Sub
    Private Sub LoadData()
     Dim i As Long
     For i = 1 To 50
      With ListView1.ListItems.Add
       .Text = "Qty " & i    ' .Text is Column 1
       .SubItems(1) = "Unit " & i 'cols 2-5
       .SubItems(2) = "PCode " & i
       .SubItems(3) = "Desc " & i
       .SubItems(4) = "Price " & i
      End With
     Next
    ' Another way
    ' Dim i As Long
    ' Dim LI As ListItem
    ' For i = 1 To 50
    '  Set LI = ListView1.ListItems.Add(, , "Qty " & i)
    '  LI.SubItems(1) = "Unit " & i
    '  LI.SubItems(2) = "PCode " & i
    '  LI.SubItems(3) = "Desc " & i
    '  LI.SubItems(4) = "Price " & i
    ' Next
    End Sub

  4. #4
    Hyperactive Member
    Join Date
    Nov 2010
    Location
    Pakistan
    Posts
    289

    Re: How to use ListView anyone plzzzz. . . .

    Here is a link which will be very helpful for you, read and implement it:
    http://www.vbforums.com/showthread.php?t=461267
    _______________________________________
    If I helped you then please help me and rate my post!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    Re: How to use ListView anyone plzzzz. . . .

    I have 5 columns in listview this are qty,pcode which is product,desc, price and amount. my problem is that when i enter a product twice with the same product desc price and amount and when i click delete it will delete ol the same name i dont understand why but if the name is not the same it will delete it but if they are the same it will delete ol the same name. . . .sometimes i get wrong total amount. . . .this is my code when i delete a product on a listview. . . .
    vb Code:
    1. Private Sub cmddelete_Click()
    2. rst.Open "Select * from tblsales", con, adOpenDynamic, adLockOptimistic
    3. While rst.EOF = False
    4. If txttransid = rst!transid And lstlist.SelectedItem.SubItems(1) = rst!pcode Then
    5.     rst.Delete
    6.     rst.Update
    7.     txttot = Val(txttot) - Val(lstlist.SelectedItem.SubItems(4))
    8.     cmddelete.Enabled = False
    9. End If
    10. rst.MoveNext
    11. Wend
    12. rst.Close
    13. Call reloadlist
    14.  
    15. End Sub
    16.  
    17. Private Sub cmddelete_Click()
    18. rst.Open "Select * from tblsales", con, adOpenDynamic, adLockOptimistic
    19. While rst.EOF = False
    20. If txttransid = rst!transid And lstlist.SelectedItem.SubItems(1) = rst!pcode Then
    21.     rst.Delete
    22.     rst.Update
    23.     txttot = Val(txttot) - Val(lstlist.SelectedItem.SubItems(4))
    24.     cmddelete.Enabled = False
    25. End If
    26. rst.MoveNext
    27. Wend
    28. rst.Close
    29. Call reloadlist
    30. End Sub
    31.  
    32. [B]Function reloadlist()[/B]
    33. rst.Open "Select * from tblsales", con, adOpenDynamic, adLockOptimistic
    34. lstlist.ListItems.clear
    35. While rst.EOF = False
    36.     If txttransid = rst!transid Then
    37.         lstlist.ListItems.Add , , rst!qty
    38.         lstlist.ListItems(lstlist.ListItems.Count).ListSubItems.Add , , rst!pcode
    39.         lstlist.ListItems(lstlist.ListItems.Count).ListSubItems.Add , , rst!Desc
    40.         lstlist.ListItems(lstlist.ListItems.Count).ListSubItems.Add , , rst!price
    41.         lstlist.ListItems(lstlist.ListItems.Count).ListSubItems.Add , , rst!amount
    42.     End If
    43.     rst.MoveNext
    44. Wend
    45. rst.Close
    46. End Function
    Last edited by Hack; Mar 11th, 2011 at 07:42 AM. Reason: Added Highlight Tags And Some Indenting

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to use ListView anyone plzzzz. . . .



    Change the beginning of cmddelete.

    Code:
    Private Sub cmddelete_Click()
    rst.Open "Select * from tblsales", con, adOpenDynamic, adLockOptimistic
    
    Do While rst.EOF = False
    If txttransid = rst!transid And lstlist.SelectedItem.SubItems(1) = rst!pcode Then
        rst.Delete
        rst.Update
        txttot = Val(txttot) - Val(lstlist.SelectedItem.SubItems(4))
        cmddelete.Enabled = False
        Exit Do
    End If
    rst.MoveNext
    Loop
    ...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    137

    Re: How to use ListView anyone plzzzz. . . .

    Thank u MartinLiss for helping me. . . .

    Last question what is the difference between the while that i put and the Do while exit Do and Loop? cause i don't use that?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width