ListView Added Item
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: ListView Added Item

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    34

    ListView Added Item

    hello Guys,, I am Developing POS and I have A Code To Add An item into Listview Then Exactly Work.. Then My Problem is, what if the Customer Select two Similar Item let Say 3 Pack of Milk and 2 Sardines..
    Example:
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2

    I forgot to add 2 pack of milk, Then I want to become like this
    Example:
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2
    Milk--------------- 2..
    But in My code this is What happen
    Item--------------QTy
    Milk--------------- 3
    Sardines---------- 2
    Its Updated the QTy Instead Make A another Row.
    HElo Guys,, This is My code..
    Code:
    Dim qtyadd As String
    Dim subtotaladd As String
    Dim itm As MSComctlLib.ListItem
    Dim i As Integer
    
    
        Set itm = ListView1.FindItem(txtCode.Text)
         If Not itm Is Nothing Then
            itm.EnsureVisible
            itm.Selected = True
            
       qtyadd = Val(txtqty.Text) + Val(ListView1.SelectedItem.SubItems(2))
       subtotaladd = Val(lbltotal.Text) + Val(ListView1.SelectedItem.SubItems(4))
        ListView1.SelectedItem.SubItems(2) = qtyadd
        ListView1.SelectedItem.SubItems(4) = Format(subtotaladd, "###################.00")
    Else
    ListView1.ListItems.Add , , txtCode.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtproductname.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtqty.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtprice.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , lbltotal.Text
    
    End If

  2. #2
    Join Date
    Jul 2005
    Posts
    1,076

    Re: ListView Added Item

    That's what you asked for in your previous post and that's what your code does
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  3. #3
    Join Date
    Feb 2013
    Posts
    34

    Re: ListView Added Item

    Yes Does it. Sir Help me for that matter..

  4. #4
    Join Date
    Jul 2005
    Posts
    1,076

    Re: ListView Added Item

    If you want to just add to listview control every scanned item, then replace all your posted code by the next:
    Code:
      Dim itm As MSComctlLib.ListItem
    
      Set itm = ListView1.ListItems.Add(, , txtCode.Text)
            itm.SubItems(1) = txtproductname.Text
            itm.SubItems(2) = txtqty.Text
            itm.SubItems(3) = txtprice.Text
            itm.SubItems(4) = lbltotal.Text
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  5. #5
    Join Date
    Feb 2013
    Posts
    34

    Re: ListView Added Item

    Quote Originally Posted by jggtz View Post
    If you want to just add to listview control every scanned item, then replace all your posted code by the next:
    Code:
      Dim itm As MSComctlLib.ListItem
    
      Set itm = ListView1.ListItems.Add(, , txtCode.Text)
            itm.SubItems(1) = txtproductname.Text
            itm.SubItems(2) = txtqty.Text
            itm.SubItems(3) = txtprice.Text
            itm.SubItems(4) = lbltotal.Text
    This is what i mean if the Item is already in Listview Then there are two items in Listview like
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2
    Then The Customer Decide a Buy Another Milk then it should be like this
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2
    Milk--------------- 1
    Then My Problem is If The Customer Buy Another Milk then Code Run it should be like this
    Item--------------QTy
    Milk--------------- 2
    Sardines---------- 2
    not this one..
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2
    Milk--------------- 1

    What Should I do?

    Thanks in Advance

  6. #6
    Join Date
    Jul 2005
    Posts
    1,076

    Re: ListView Added Item

    Code:
    Dim Itm  As MSComctlLib.ListItem
    Dim X    As Integer
    Dim Dbl1 As Double
    Dim Dbl2 As Double
    Dim Aux1 As String
    
    Aux1 = ""
    For X = 1 To ListView1.ListItems.Count
      If ListView1.ListItems(X).Text = txtCode.Text Then
        'Qty
        Dbl1 = Cdbl(ListView1.ListItems(X).SubItems(2))
        Dbl2 = Cdbl(txtqty.Text)
        ListView1.ListItems(X).SubItems(2) = Format((Dbl1 + Dbl2))
        'Total
        Dbl1 = Cdbl(ListView1.ListItems(X).SubItems(4))
        Dbl2 = Cdbl(lblTotal.Caption)
        ListView1.ListItems(X).SubItems(4) = Format((Dbl1 + Dbl2))
        Aux1 = "*"
      End If
    Next X
    
    If Aux1 = "" Then
      Set itm = ListView1.ListItems.Add(, , txtCode.Text)
          itm.SubItems(1) = txtproductname.Text
          itm.SubItems(2) = txtqty.Text
          itm.SubItems(3) = txtprice.Text
          itm.SubItems(4) = lbltotal.Caption
    End If
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  7. #7
    Join Date
    Feb 2013
    Posts
    34

    Re: ListView Added Item

    Quote Originally Posted by jggtz View Post
    Code:
    Dim Itm  As MSComctlLib.ListItem
    Dim X    As Integer
    Dim Dbl1 As Double
    Dim Dbl2 As Double
    Dim Aux1 As String
    
    Aux1 = ""
    For X = 1 To ListView1.ListItems.Count
      If ListView1.ListItems(X).Text = txtCode.Text Then
        'Qty
        Dbl1 = Cdbl(ListView1.ListItems(X).SubItems(2))
        Dbl2 = Cdbl(txtqty.Text)
        ListView1.ListItems(X).SubItems(2) = Format((Dbl1 + Dbl2))
        'Total
        Dbl1 = Cdbl(ListView1.ListItems(X).SubItems(4))
        Dbl2 = Cdbl(lblTotal.Caption)
        ListView1.ListItems(X).SubItems(4) = Format((Dbl1 + Dbl2))
        Aux1 = "*"
      End If
    Next X
    
    If Aux1 = "" Then
      Set itm = ListView1.ListItems.Add(, , txtCode.Text)
          itm.SubItems(1) = txtproductname.Text
          itm.SubItems(2) = txtqty.Text
          itm.SubItems(3) = txtprice.Text
          itm.SubItems(4) = lbltotal.Caption
    End If
    Hello Sir,,
    Sorry for My Mistake,,
    This is what i mean if the Item is already in Listview Then there are two items in Listview like
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2
    Then The Customer Decide a Buy Another Milk then it should be like this
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2
    Milk--------------- 1
    Then My Problem is If The Customer Buy Another Milk then Code Run it should be like this
    Item--------------QTy
    Milk--------------- 1
    Sardines---------- 2
    Milk--------------- 1
    not this one..

    Item--------------QTy
    Milk--------------- 2
    Sardines---------- 2

    What Should I do?
    Thanks Sir

  8. #8
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: ListView Added Item

    Use 3 variables, and update the listview whenever any ONE of them changes...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Jul 2005
    Posts
    1,076

    Re: ListView Added Item

    Then use code in post #4 instead of your code in post #1
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  10. #10
    Join Date
    Feb 2013
    Posts
    34

    Re: ListView Added Item

    Quote Originally Posted by jggtz View Post
    Then use code in post #4 instead of your code in post #1
    Ive Already use code in post #4 instead of My code..
    But still My Problem not Fixed.. My Code And your Code Same Output..

    Scenario..
    Customer Buy 3 Milk and 2 Nescafe then the Cashier Punch it
    Item-----------------Qty
    Milk-------------------2
    Nescafe---------------2

    The Cashier Forget to Punch the 1 Milk. the Cashier should Punch Another One Milk..But Using Your Code And Mine This is What Happen

    Item---------------Qty
    Milk-----------------3
    Nescafe-------------2

    Then I want to become like this
    Item---------------Qty
    Milk-----------------2
    Nescafe-------------2
    Milk-----------------1

    in First row It is Milk then 2nd Row is Nescafe then Third tow in Milk Again..
    The Qty, Of Milk Doesn't Updated Because Nescafe Insert Between Them,

    If Nescafe Doesn't Inserted Between them The Qty Become 3/..

    Just Like What i Mean Sir..

    What Should I do ?

    Many Thanks Sir

  11. #11
    Join Date
    Jul 2005
    Posts
    1,076

    Re: ListView Added Item

    Ive Already use code in post #4 instead of My code..
    But still My Problem not Fixed.. My Code And your Code Same Output..
    DID YOU REPLACE ALL YOUR CODE POSTED IN #1 FOR THE CODE POSTED IN #4 ?

    The code in post #4 only add to listview without accumulate
    So
    Do not say... post your actual code
    Last edited by jggtz; March 2nd, 2013 at 12:11 PM.
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  12. #12
    Join Date
    Feb 2013
    Posts
    34

    Re: ListView Added Item

    Quote Originally Posted by jggtz View Post
    DID YOU REPLACE ALL YOUR CODE POSTED IN #1 FOR THE CODE POSTED IN #4 ?

    The code in post #4 only add to listview without accumulate
    So
    Do not say... post your actual code
    Yes I did.

    But

    I don't know how to start sir, Please give some Idea, Thanks Sir.

  13. #13
    Join Date
    Jul 2005
    Posts
    1,076

    Re: ListView Added Item

    This a small listview example
    Let's start with it
    Study it
    Modify it...
    add textboxes Qty, Description, Price & Total
    add columns to listview
    Repost when done

    Say your Country
    Attached Files Attached Files
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  14. #14
    Join Date
    Feb 2013
    Posts
    34

    Re: ListView Added Item

    Code:
    Dim Dbl1 As Double
    Dim Dbl2 As Double
    Dim Aux1 As String
    Dim qtyadd As String
    Dim subtotaladd As String
    Dim itm As MSComctlLib.ListItem
    Dim i As Integer
      Set itm = ListView1.FindItem(txtCode.Text)
    If itm Is Nothing Then
    ListView1.ListItems.Add , , txtCode.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtproductname.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtqty.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtprice.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , lbltotal.Text
    ElseIf ListView1.ListItems.Item(ListView1.ListItems.Count).Text <> txtCode.Text Then
    ListView1.ListItems.Add , , txtCode.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtproductname.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtqty.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , txtprice.Text
    ListView1.ListItems.Item(ListView1.ListItems.Count).ListSubItems.Add , , lbltotal.Text
    ElseIf Not itm Is Nothing Then
    For i = 1 To ListView1.ListItems.Count
    If ListView1.ListItems(i).Text = txtCode.Text Then
        Dbl1 = (ListView1.ListItems(i).SubItems(2))
        Dbl2 = (txtqty.Text)
        ListView1.ListItems(i).SubItems(2) = Format((Dbl1 + Dbl2))
        'Total
        Dbl1 = (ListView1.ListItems(i).SubItems(4))
        Dbl2 = (lbltotal.Text)
        ListView1.ListItems(i).SubItems(4) = Format((Dbl1 + Dbl2), "###################.00")
        Aux1 = "*"
    End If
    Next i
      
    End If
    So if you have.

    item1 1



    then add item1 again you get

    item1 2



    also if you have

    item2 1
    item1 1



    and then you add item1 you get

    item2 1
    item1 2




    but, if you have

    item1 1
    item2 1



    then you add another item1, you want

    item1 1
    item2 1
    item1 1

    in my code the Output is
    All Item 1 Added by 1
    like then you add another item1, you want

    item1 2
    item2 1
    item1 2

    help me Sir to Correct My Code..
    thanks

  15. #15
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: ListView Added Item

    Code:
    Dim item1 as Integer
    Dim item2 as Integer
    Dim item3 as Integer
    item1 = 0
    item2 = 0
    item3 = 0
    Why not just STORE the value???
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center