# Vb6:



## EnterDeath (Dec 6, 2002)

ok, the last problem or so i think ... now after the user has pushed the button to show the listbox's items. i need to get it to calculate through another cmd button the (selling price of a unit - cost of unit) = result displayed back in the original listbox.

use this line of code for demonstration.

.AddItem "C101" & vbTab & "1" & vbTab & "Roadhandler " & vbTab & "97.56 " & vbTab & "125.11" & vbTab & "25 "


say i need it to subtract 97.56 from 125.11 and display the result of it back into the original listbox. These numbers and end result must be in currency.

here is how i tried to code it however i can only get it to come up in a msgbox, and i couldn't get it to do the calculation of subtractiong the 2 prices and display the results it just displays the original coding.

Private Sub cmdTotal_Click()
For I = 0 to lstExample.ListCount - 1
If lstExample.Selected(I) Then
MsgBox lstExample.List(I) & " is selected"
End If
Next I
End Sub

i know my code is way of but i dont know the specific commands to get it to select... i tried the Mid$ command but i couldn't get it working. any help would be greatly appreciated


----------



## Mosaic1 (Aug 17, 2001)

I'll get you started until someone more Experienced in VB comes along.

First question is how do you have your program organized? Where are you storing the information regarding Price and cost. Are you using a Database? Is the cost figured using a formula? Or do you have a figure stored somewhere? 

Any number can be formatted as currency using this function:

FormatCurrency(Number)

But that's way down the line.



In your program, is it possible to select more than one item from the listbox? You might want to show the results in a Label control instead. 

Post back with more information and I am sure someone can help. Possibly changing the way you have this organized.


----------



## YSB (Mar 7, 1999)

Umm... It seems like your starting us off in the middle of something. I can't really picture exactly what you have now.

Could you please explain exactly what you have and what you want to do and then we can try to help you do it.

Good Luck!


----------



## EnterDeath (Dec 6, 2002)

the cost is figured based off of the figures that are stored in the listbox when a user clicks the compute button. then im using another button to clear the lstbox and place the total calculation. and yes more than one item can be selected. i need it to add up the items selected and display a total.


----------



## Mosaic1 (Aug 17, 2001)

We really need more information. You are trying to manipulate a string in order to get the numbers you want to work with, correct?

What would help is knowing how the Listbox is loaded. What is the source of the numbers. Are you using a Database or even loading from a textfile? Or are you manually loading the information? It sounds like you are manually loading each Line using String Concantenation. Then you are trying to retrieve numbers from their postition in the String and perfom calculations.

You need to post more complete information.
The way you are going at this sounds very disorganized. If you have information and store that information in Variables using arrays you can then perform the calculations more easily.


----------



## happyguyz (Dec 6, 2002)

In case u said that user can multi select the list box and when click on cmd button. show result of added all selected then:

Private Sub cmdTotal_Click() 
Dim sngTotal as Single

For I = 0 to lstExample.ListCount - 1 
If lstExample.Selected(I) Then
sngtotal = sngTotal + lstExample.Selected(I).value
End If
Next

lstExample.Clear
lstExample.additem sngTotal
End sub


This is what I understand your question. If you need something more, tell us more specific.


----------



## EnterDeath (Dec 6, 2002)

ok, here is some more info. i am manual entering info from a string, and yes i need to parse through to pull out 2 different numbers within that string heres an example:
with lstexample
.additem "X & "Y"
end with

ok so now i need to subtract X from Y.


would my best bet be to use the Mid$... if so the resources i have don't explain that function very well. and i think it is where my problem is steming from
something like this i think
Mid$("word to start counting from", "number of spaces","number of characters to select" )

my problem here is counting. how would you count in this example?

lstExample.additem "Computer" "999.00" "1499.00" "25"
listbox description our cost selling cost quantity

to use the Mid$ function would i code like this?

Mid$(computer, 8, 6)
computer- starts on c
8 - count 8 spaces over
6 - select 6 spots from space 8

would this be correct or does it start counting from last letter of computer being the R?

if i can get the Mid$ function working the rest is cake if i am using it incorrectly please post the proper way to use the Mid$ function. i think thats all i would really need. thanks you guys are big help!


----------



## Mosaic1 (Aug 17, 2001)

I need the practice and so I took your problem and wrote a sample program. This technique would work very well for your purposes, I think. I am sure someone else could write more sophisticated code, but this is the best I can do at this point. Especially given the constraints here.

* See attachment in next post. *

I have three Fields. Each field has its own listbox and its own text file which when called, populates the listbox.

My three fields are
Name
Price
Cost

I have a final Listbox named lstTotal. It's invisible until the User clicks the Show Total Button. The math calculation involved is Price - cost = Total. The Total along with it's name will show in the fourth listbox. This listbox will become visible when the total button is pressed. The other boxes will become invisible.

Conversely, when you reset, the Total Listbox will become invisible and the original three will appear.

Here's how it works.

Create 3 text files in this location: C:\VBFiles\Inventory
Names.txt Prices.txt Cost.txt

The key here is to coordinate these files. The first entry in Names.txt will become the first entry in the first list box.
The price of that item will be the first entry in prices.txt and the first entry in the second listbox. 
The cost(to you) will be the first entry in Cost.txt and the first entry in the third listbox.

In this example I have three entries. 
See attachment. You would probably want to Add a Label above each ListBox too.

Name= Dog Price = $240.00 Cost= $20.00
Horse $300.00 $30.00
Cat $650.00 $40.00

Names.txt looks like this:
"Dog"
"Horse"
"Cat"

Prices.txt looks like this:
240
300
650

Cost.txt looks like this:
20
30
40

Which means I have a Dog whose price is 240 and the cost is 20
A horse whose Price is 300 and the cost is 30
A Cat whose price is 650 and the cost is 40

In the form's load event, I use these text files to populate the three visible Listboxes.

Here's the code: 

```
Private Sub Form_Load()
List4.Visible = False
Dim StrName As String
Dim intCounter As Integer

Open "C:\VBFiles\Inventory\names.txt" For Input As #5
  Do While Not EOF(5)
  Input #5, StrName
    List1.AddItem StrName
    intCounter = intCounter + 1
DoEvents
Loop
Close #5

Dim StrPrice As String
Dim intCounter1 As Integer

Open "C:\VBFiles\Inventory\Prices.txt" For Input As #6
    Do While Not EOF(6)
     Input #6, StrPrice
      List2.AddItem FormatCurrency(StrPrice)
       intCounter1 = intCounter + 1
DoEvents
Loop
Close #6

Dim StrCost As String
Dim intCounter2 As Integer

  Open "C:\VBFiles\Inventory\Cost.txt" For Input As #7
   Do While Not EOF(7)
    Input #7, StrCost
     List3.AddItem FormatCurrency(StrCost)
       intCounter2 = intCounter + 1
   DoEvents
   Loop
   Close #7
 End Sub

Private Sub List1_Click()
Dim n As Integer
Dim p As Integer
Dim c As Integer
Dim t As Integer
Dim X As Single
Dim Y As Single
Dim Z As Single
n = p = c = t
List4.Clear
  For n = 0 To List1.ListCount - 1

   If List1.Selected(n) Then
    X = List2.List(n)
    Y = List3.List(n)
          Z = X - Y

 List4.AddItem List1.List(n) & vbTab & _
  FormatCurrency(Z)
   End If
     Next
 End Sub

Private Sub cmdReset_Click()
List1.Clear
List2.Clear
List3.Clear
List4.Clear
List1.Visible = True
List2.Visible = True
List3.Visible = True
Call Form_Load
End Sub

Private Sub cmdTotals_Click()
List1.Visible = False   
List2.Visible = False
List3.Visible = False
List4.Visible = True
End Sub
```
 To use the program, click one or more entries in the first Listbox.(The names list) Then click the cmdTotals command button to show the fourth listbox with the results.

It will need tweaking. And some error handling more than likely.

When you create the form, place the fourth Listbox on top of the other three. One Form. Four Listboxes. Two Command Buttons.


----------



## Mosaic1 (Aug 17, 2001)

Here's how the program looks when loaded.

I just noticed something. When the forum parsed the code, it left out a slash in each path to the text files.


C:\VBFiles\Inventory

is showing as
C:VBFiles\Inventory

and a couple other slashes are missing too in the paths. 

You'll have to fix that if you decide to try the program.


----------



## Rollin' Rog (Dec 9, 2000)

Here's the code:


```
Private Sub Form_Load()
List4.Visible = False
Dim StrName As String
Dim intCounter As Integer

Open "C:\\VBFiles\\Inventory\names.txt" For Input As #5
  Do While Not EOF(5)
  Input #5, StrName
    List1.AddItem StrName
    intCounter = intCounter + 1
DoEvents
Loop
Close #5

Dim StrPrice As String
Dim intCounter1 As Integer

Open "C:\\VBFiles\\Inventory\\Prices.txt" For Input As #6
    Do While Not EOF(6)
     Input #6, StrPrice
      List2.AddItem FormatCurrency(StrPrice)
       intCounter1 = intCounter + 1
DoEvents
Loop
Close #6

Dim StrCost As String
Dim intCounter2 As Integer

  Open "C:\\VBFiles\\Inventory\\Cost.txt" For Input As #7
   Do While Not EOF(7)
    Input #7, StrCost
     List3.AddItem FormatCurrency(StrCost)
       intCounter2 = intCounter + 1
   DoEvents
   Loop
   Close #7
 End Sub

Private Sub List1_Click()
Dim n As Integer
Dim p As Integer
Dim c As Integer
Dim t As Integer
Dim X As Single
Dim Y As Single
Dim Z As Single
n = p = c = t
List4.Clear
  For n = 0 To List1.ListCount - 1

   If List1.Selected(n) Then
    X = List2.List(n)
    Y = List3.List(n)
          Z = X - Y

 List4.AddItem List1.List(n) & vbTab & _
  FormatCurrency(Z)
   End If
     Next
 End Sub

Private Sub cmdReset_Click()
List1.Clear
List2.Clear
List3.Clear
List4.Clear
List1.Visible = True
List2.Visible = True
List3.Visible = True
Call Form_Load
End Sub

Private Sub cmdTotals_Click()
List1.Visible = False   
List2.Visible = False
List3.Visible = False
List4.Visible = True
End Sub
```
Odd about those slashes, I haven't seen that happen here before and it must be a bug in the php code tag I guess.

Even odder is that it isn't even consistent, one slash did display on the first line, but none of the others. If you double them up though, they display -- as in this post.


----------



## Mosaic1 (Aug 17, 2001)

Rog,
Thank you for fixing it. And thanks for the information on how to do it.

Mo


----------



## EnterDeath (Dec 6, 2002)

short story:

thanks to all. everyone helped me in completing my task. wow i am still amazed at how well the forum works, and the generosity that is time and effort, that everyone puts in. 

long story:

basically i got "cocked" so to say by UNLV with a horrible teacher. He knows his stuff however he doesn't explain it for sh!t. i have used the book for most of my class and up until now (the final) i have had no troubles. however this did cause me great pain and you guys have been more than generous in offering up your services. I started this project on monday of last week and due to the very fast responses and such i completed it very early.

The Tech Guy boards can be proud to host a forum for people who are very effective in certain situations. it has one very happy newbie (me) who will hopefull contribute back what he has learned. thanks again everyone lots of luck with any problems you encounter


----------



## Mosaic1 (Aug 17, 2001)

EnterDeath,

First. I am not sure how happy your teacher is going to be with this half baked method. Although it does work, it is a bit clumsy.

Also, at one point I was going to have the corresponding entries in the other two listboxes highlight when a name was chosen. So I used three For Nexts and had to use more variables. I decided against that for several reasons.(one of which was it didn't work right! LOL) I forgot to remove the extra lines in the list1 click event.

So these are not needed in the List1 CLick event. I didn't use them in the end.

Dim p As Integer
Dim c As Integer
Dim t As Integer
n = p = c = t

Sorry about that. 

I am teaching myself VB slowly in my spare time. I agree. It is hard to learn. Teaching is very difficult. I have noticed that on Forums in Tutorials and a book I have. People do not explain well. 
It's something you learn by example as well as by reading. Too many words and not enough hands on makes it impossible. 

Although if you ever want a good teacher who writes well, look into the books by John Smiley


----------



## EnterDeath (Dec 6, 2002)

well half assed is better than most of the people in my class who stare at e-mail during class. i think the simple fact i completed it should get me a passing grade and from the teacher an OMFG someone actually finished it


----------



## Mosaic1 (Aug 17, 2001)

EnterDeath,

LOL Sounds like a terrible class. How do you ever learn anything?

Good luck on this one. And Happy Holidays.

Mo


----------



## EnterDeath (Dec 6, 2002)

i read the book about 10 times, browsed every forum for all info i could get, then i taught myself. When i would not know how to do things i would ask friends etc... and easiest way by far was through forums, everyone is more than helpful when you actually do the stuff yourself, and just need help. i noticed alot of post like do my assignment. those people got ripped up, so i learned as much as i could before posting with stupid problems.


----------

