Results 1 to 14 of 14

Thread: It's possible mathematics in a ListBox?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    It's possible mathematics in a ListBox?

    Hi All

    It's possible adding a different of values in a ListBox. For example. I have in a ListBox some a value 5 then I add other the value 3. If will be the same of item this should add only an value, and the name of item should be the same also the quantity of thing should stay the same.

    e.g.

    I have an item:

    Fish 5

    I want to add:

    Fish 3

    it should still be only 1st thing, but only quantity should change, it's possible something this?

    I would have then:

    Fish 8

    Thanks in advance
    Last edited by Tamgovb; Jun 6th, 2006 at 12:42 PM.
    I know, I know, my English is bad, sorry .....

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: It's a possible mathematics in a ListBox?

    sure.. basically loop thru the list box.. looking for a match of the word.. then add numbers

    VB Code:
    1. Private Sub AddToList(sItem As String)
    2. Dim tmp() As String
    3. Dim iTmp() As String
    4. iTmp = Split(sItem, " ")
    5. For x = 0 To List1.ListCount - 1
    6.     tmp = Split(List1.List(x), " ")
    7.     If tmp(0) = iTmp(0) Then
    8.         List1.List(x) = tmp(0) & " " & (Val(tmp(1)) + Val(iTmp(1)))
    9.         Exit Sub
    10.     End If
    11. Next
    12.  
    13. End Sub
    14.  
    15. Private Sub Command1_Click()
    16.     AddToList "Fish 7"
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.     List1.AddItem "Fish 5"
    21.     List1.AddItem "Cars 3"
    22. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: It's a possible mathematics in a ListBox?

    Something like the following may work for you:
    VB Code:
    1. Private Sub Command1_Click()
    2. '============================
    3. Dim iOldQty%, iNewQty%, i%
    4. Dim strWhat$
    5. Dim blnExist As Boolean
    6.  
    7.     iNewQty = CInt(txtQty.Text) 'txtQty is a textbox that user enters new quantity
    8.     strWhat = "Fish "
    9.    
    10.     For i = 0 To List1.ListCount - 1
    11.         If InStr(1, List1.List(i), strWhat) > 0 Then
    12.             iOldQty = CInt(Mid(List1.List(i), InStr(1, List1.List(i), strWhat) + Len(strWhat)))
    13.             List1.List(i) = "Fish " & iOldQty + iNewQty
    14.             blnExist = True
    15.             Exit For
    16.         End If
    17.     Next i
    18.    
    19.     If Not blnExist Then
    20.         List1.AddItem "Fish " & iNewQty
    21.     End If
    22.  
    23. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: It's a possible mathematics in a ListBox?

    Hi all

    Static: thanks for your solved, your code it works nicely...but nothing writes into a ListBox if I don't add any number to each a word or I write a new the word.

    Rhinobull: I thank you very also. Also your code it works nicely. I added several details. I try to solve now one small an problem.

    How to make that this code will be not distinguish small and large letters?

    When I write a word with small of letter then this code will treat such the word as new the word and it adds a new an item.

    this code is such now:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. '============================
    5. Dim iOldQty%, iNewQty%, i%
    6. Dim strWhat$
    7. Dim blnExist As Boolean
    8.  
    9.     iNewQty = CInt(txtQty.Text) '<<< txtQty is a textbox that user enters new quantity
    10.     strWhat = txtName.Text      '<<< txtName is a textbox that user enters new name
    11.    
    12.     For i = 0 To List1.ListCount - 1
    13.         If InStr(1, List1.List(i), strWhat) > 0 Then
    14.             iOldQty = CInt(Mid(List1.List(i), InStr(1, List1.List(i), strWhat) + Len(strWhat)))
    15.             List1.List(i) = txtName.Text & " " & iOldQty + iNewQty '<<< I added a bit of space betveen word and number
    16.             blnExist = True
    17.             Exit For
    18.         End If
    19.     Next i
    20.    
    21.     If Not blnExist Then
    22.         List1.AddItem txtName.Text & " " & iNewQty '<<< I added space here also
    23.     End If
    24.  
    25. End Sub
    26.  
    27. Private Sub Form_Load() '<<< I used two line from Staic's code (Static, sorry..)
    28.     List1.AddItem "Fish 5"
    29.     List1.AddItem "Cars 3"
    30. End Sub

    So, how to make?
    Thanks in advance
    Last edited by Tamgovb; May 31st, 2006 at 02:30 PM.
    I know, I know, my English is bad, sorry .....

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: It's a possible mathematics in a ListBox?

    Quote Originally Posted by Tamgovb
    ...When I write a word with small of letter then this code will treat such the word as new the word and it adds a new an item....
    Just convert text to Upper or Lower case:
    VB Code:
    1. 'change this line
    2. If InStr(1, List1.List(i), strWhat) > 0 Then
    3.  
    4. 'to this
    5. If InStr(1, [B]LCase[/B](List1.List(i)), [B]LCase[/B](strWhat)) > 0 Then

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: It's a possible mathematics in a ListBox?

    Thanks Rhino, sorry, but I get "run-time error '13' " Type mismatch in this line:
    VB Code:
    1. iOldQty = CInt(Mid(List1.List(i), InStr(1, List1.List(i), strWhat) + Len(strWhat)))

    though that conversion in this line it's done

    I don't know what to do.
    Thanks
    Last edited by Tamgovb; May 31st, 2006 at 03:27 PM.
    I know, I know, my English is bad, sorry .....

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: It's a possible mathematics in a ListBox?

    Work fine for me...

    However, if txtQty or txtName textbox is empty then you will get that error so you need some validation.
    You can do something like the following:
    VB Code:
    1. [B]If IsNumeric(txtQty.Text) = False Or Trim(txtName.Text) = "" Then Exit Sub[/B]
    2.    
    3.     iNewQty = CInt(txtQty.Text) '<<< txtQty is a textbox that user enters new quantity
    4.     strWhat = txtName.Text

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: It's a possible mathematics in a ListBox?

    Hi All, hi Rhino

    Sorry, long I was absent

    I get this error when I try to write each word with other a first the character than was written in a ListBox.
    BTW: Always is written something in every textboxes - never is no empty

    e.g.

    if is written there in a ListBox:

    Cars

    And I will be a write in a txtName

    cars

    then I get this error - I hope that you understand me

    if I wrote in a txtName

    Cars

    Then it's okay. Hmmm, let's say….

    Obviously in a txtQty it's written always some number - is not empty

    Thanks in advance
    I know, I know, my English is bad, sorry .....

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: It's possible mathematics in a ListBox?

    Hi All

    still not solved this problem
    I know, I know, my English is bad, sorry .....

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: It's possible mathematics in a ListBox?

    did u use Rhino's code Lcase() in the compare?

    either that or use

    Instr(1,string1,string2,vbTextCompare)

    that will ignore case....
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: It's possible mathematics in a ListBox?

    Hmm... sincerely saying - I don't know how to apply this vbTextCompare (exactly in which a line - I never used from this)
    thanks
    I know, I know, my English is bad, sorry .....

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: It's possible mathematics in a ListBox?

    Hi all

    Someone you know, how to make? Please, help me.

    thanks in advance
    I know, I know, my English is bad, sorry .....

  13. #13
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: It's possible mathematics in a ListBox?

    VB Code:
    1. If InStr(1, LCase(List1.List(i)), LCase(strWhat),vbTextCompare) > 0 Then
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: It's possible mathematics in a ListBox?

    Thanks, this problem it's solved

    I made it so:

    I added yet one a variable

    VB Code:
    1. Dim Find As Integer

    Next I rebuilt record a bit

    VB Code:
    1. For i = 0 To List1.ListCount - 1
    2.       If InStr(1, List1.List(i), strWhat, vbTextCompare) > 0 Then
    3.          Find = InStrRev(List1.List(i), " ")
    4.          If Find <> 0 Then
    5.             List1.List(i) = Left$(List1.List(i), Find) & Val(Right$(List1.List(i), Len(List1.List(i)) - Find)) + Val(txtQty)
    6.             blnExist = True
    7.             Exit For
    8.          End If
    9.       End If
    10.    Next
    11.    
    12.    If Not blnExist Then List1.AddItem txtName.Text & " " & iNewQty
    13. End Sub

    It work fine for me,

    Thanks All
    I know, I know, my English is bad, sorry .....

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