|
-
May 30th, 2006, 03:27 PM
#1
Thread Starter
Hyperactive Member
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 .....
-
May 30th, 2006, 03:37 PM
#2
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:
Private Sub AddToList(sItem As String)
Dim tmp() As String
Dim iTmp() As String
iTmp = Split(sItem, " ")
For x = 0 To List1.ListCount - 1
tmp = Split(List1.List(x), " ")
If tmp(0) = iTmp(0) Then
List1.List(x) = tmp(0) & " " & (Val(tmp(1)) + Val(iTmp(1)))
Exit Sub
End If
Next
End Sub
Private Sub Command1_Click()
AddToList "Fish 7"
End Sub
Private Sub Form_Load()
List1.AddItem "Fish 5"
List1.AddItem "Cars 3"
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
May 30th, 2006, 03:42 PM
#3
Re: It's a possible mathematics in a ListBox?
Something like the following may work for you:
VB Code:
Private Sub Command1_Click()
'============================
Dim iOldQty%, iNewQty%, i%
Dim strWhat$
Dim blnExist As Boolean
iNewQty = CInt(txtQty.Text) 'txtQty is a textbox that user enters new quantity
strWhat = "Fish "
For i = 0 To List1.ListCount - 1
If InStr(1, List1.List(i), strWhat) > 0 Then
iOldQty = CInt(Mid(List1.List(i), InStr(1, List1.List(i), strWhat) + Len(strWhat)))
List1.List(i) = "Fish " & iOldQty + iNewQty
blnExist = True
Exit For
End If
Next i
If Not blnExist Then
List1.AddItem "Fish " & iNewQty
End If
End Sub
Last edited by RhinoBull; May 30th, 2006 at 03:46 PM.
-
May 31st, 2006, 02:21 PM
#4
Thread Starter
Hyperactive Member
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:
Option Explicit
Private Sub Command1_Click()
'============================
Dim iOldQty%, iNewQty%, i%
Dim strWhat$
Dim blnExist As Boolean
iNewQty = CInt(txtQty.Text) '<<< txtQty is a textbox that user enters new quantity
strWhat = txtName.Text '<<< txtName is a textbox that user enters new name
For i = 0 To List1.ListCount - 1
If InStr(1, List1.List(i), strWhat) > 0 Then
iOldQty = CInt(Mid(List1.List(i), InStr(1, List1.List(i), strWhat) + Len(strWhat)))
List1.List(i) = txtName.Text & " " & iOldQty + iNewQty '<<< I added a bit of space betveen word and number
blnExist = True
Exit For
End If
Next i
If Not blnExist Then
List1.AddItem txtName.Text & " " & iNewQty '<<< I added space here also
End If
End Sub
Private Sub Form_Load() '<<< I used two line from Staic's code (Static, sorry..)
List1.AddItem "Fish 5"
List1.AddItem "Cars 3"
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 .....
-
May 31st, 2006, 02:33 PM
#5
Re: It's a possible mathematics in a ListBox?
 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:
'change this line
If InStr(1, List1.List(i), strWhat) > 0 Then
'to this
If InStr(1, [B]LCase[/B](List1.List(i)), [B]LCase[/B](strWhat)) > 0 Then
-
May 31st, 2006, 03:22 PM
#6
Thread Starter
Hyperactive Member
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:
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 .....
-
May 31st, 2006, 04:48 PM
#7
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:
[B]If IsNumeric(txtQty.Text) = False Or Trim(txtName.Text) = "" Then Exit Sub[/B]
iNewQty = CInt(txtQty.Text) '<<< txtQty is a textbox that user enters new quantity
strWhat = txtName.Text
-
Jun 2nd, 2006, 03:22 PM
#8
Thread Starter
Hyperactive Member
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 .....
-
Jun 6th, 2006, 12:43 PM
#9
Thread Starter
Hyperactive Member
Re: It's possible mathematics in a ListBox?
Hi All
still not solved this problem
I know, I know, my English is bad, sorry .....
-
Jun 6th, 2006, 12:50 PM
#10
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"
-
Jun 6th, 2006, 02:12 PM
#11
Thread Starter
Hyperactive Member
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 .....
-
Jun 7th, 2006, 07:55 AM
#12
Thread Starter
Hyperactive Member
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 .....
-
Jun 7th, 2006, 08:02 AM
#13
Re: It's possible mathematics in a ListBox?
VB Code:
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"
-
Jun 7th, 2006, 02:20 PM
#14
Thread Starter
Hyperactive Member
Re: It's possible mathematics in a ListBox?
Thanks, this problem it's solved
I made it so:
I added yet one a variable
Next I rebuilt record a bit
VB Code:
For i = 0 To List1.ListCount - 1
If InStr(1, List1.List(i), strWhat, vbTextCompare) > 0 Then
Find = InStrRev(List1.List(i), " ")
If Find <> 0 Then
List1.List(i) = Left$(List1.List(i), Find) & Val(Right$(List1.List(i), Len(List1.List(i)) - Find)) + Val(txtQty)
blnExist = True
Exit For
End If
End If
Next
If Not blnExist Then List1.AddItem txtName.Text & " " & iNewQty
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|