Listbox problems..Need help

Status
Not open for further replies.

gecko_au2003

Banned
Messages
50
If you change the style of a listbox from simple or normal or w/e it is in the properties window to checkbox. It makes it show every item you add to it with check marks, my question is , how do you detect which checkbox is checked and relate values ie double or integer values to each item in the listbox and then do a sum of every checkbox checked after all the ones the user wants to check are checked.

Ive managed to relate values if it is just a normal listbox but I am stuck with this. I found some examples using a for loop to go through the whole listbox to see which ones were checked but that dont work because it just does some weird stuff that I cant figure out.

Any coding examples or URL's that point me in the right direction would be very much appreicated !!

Thanks in advance to anyone who can help me with both of my problems, especially the other problem !!
 
is there anyway i can include a zip file with the code or would i have to copy and paste it all in to here ??
 
'Option Explicit
Private strSizes(3) As String
Private strToppings(3) As String
Private dblSizePrices(3) As Double
Private dblToppingPrices(3) As Double
Private varStore(3) As Variant

Private Sub cmdCompute_Click()
Dim Answer As Double

Answer = dblSizePrices(lstSize.ListIndex) * dblToppingPrices(lstToppings.ListIndex)
lblShowPrice.Caption = "Total: P " & Answer
End Sub

Private Sub Command1_Click()
'MsgBox dblToppingPrices(lstToppings.Style)
'For i% = 0 To lstToppings.ListCount - 1
' If lstToppings.Selected(i%) = True Then
'MsgBox "Item number " & i% & " is selected"
'varStore(i%) = dblToppingPrices(i%)
'Ans = dblSizePrices(lstSize.ListIndex) *

lbltoppingssub.Caption = lbltoppingssub.Caption + (dblToppingPrices(i%))
'MsgBox Ans 'dblToppingPrices(i%)
' MsgBox varStore(i%)
' End If
'Next i%

'For x = (lstToppings.ListCount - 1) To 0 Step -1
'If lstToppings.Selected(x) Then
' MsgBox lstToppings.Selected(x)
' End If
' Next
For i% = 0 To lstToppings.ListCount - 1
If lstToppings.Selected(i%) = True Then
'MsgBox "Item number " & i% & " is selected"
'varStore(i%) = dblToppingPrices(i%)
'Ans = dblSizePrices(lstSize.ListIndex) *
Var = Var + dblToppingPrices(i%)
MsgBox Var
lbltoppingssub.Caption = lbltoppingssub.Caption + (dblToppingPrices(i%))
'MsgBox Ans 'dblToppingPrices(i%)
' MsgBox varStore(i%)
End If
Next i%
End Sub

Private Sub Form_Load()
Dim i As Integer
strSizes(0) = "Small"
strSizes(1) = "Medium"
strSizes(2) = "Large"
strSizes(3) = "Family"
strToppings(0) = "Cheese"
strToppings(1) = "Ham"
strToppings(2) = "Onions"
strToppings(3) = "Peppers"
dblSizePrices(0) = 40
dblSizePrices(1) = 75
dblSizePrices(2) = 100
dblSizePrices(3) = 140
dblToppingPrices(0) = 5
dblToppingPrices(1) = 15
dblToppingPrices(2) = 8
dblToppingPrices(3) = 10

For i = 0 To 3
lstSize.AddItem (strSizes(i))
lstToppings.AddItem (strToppings(i))
'lstSize.Selected(i) = dblSizePrices(i)
Next i

End Sub

Private Sub lstSize_Click()
lblSize.Caption = "Size : " & lstSize.Text & " "
lblsizesub.Caption = dblSizePrices(lstSize.ListIndex)
End Sub
 
my project has 2 buttons, 5 labels and 2 listboxes, one listbox is in the standard style and the other one is normal style. The problem being that i want to sum up all of the selected values that relate to the checked checkboxes in the 2nd listbox after the user has made there selection of checkboxes that they wanted to select. Cos so far it sums it up each time u check a check box. I figured if i put the code in to a command button it would fix that but then the code doesn't work the same as what it does if u put it into a different event.
 
Please try this code now, i remove the code from command button, to many object but if you want to you may...


'Option Explicit
Private strSizes(3) As String
Private strToppings(3) As String
Private dblSizePrices(3) As Double
Private dblToppingPrices(3) As Double
Private varStore(3) As Variant


Private Sub Form_Load()
Dim i As Integer
lblSize.Caption = "Size/s : "
strSizes(0) = "Small"
strSizes(1) = "Medium"
strSizes(2) = "Large"
strSizes(3) = "Family"
strToppings(0) = "Cheese"
strToppings(1) = "Ham"
strToppings(2) = "Onions"
strToppings(3) = "Peppers"
dblSizePrices(0) = 40
dblSizePrices(1) = 75
dblSizePrices(2) = 100
dblSizePrices(3) = 140
dblToppingPrices(0) = 5
dblToppingPrices(1) = 15
dblToppingPrices(2) = 8
dblToppingPrices(3) = 10

For i = 0 To 3
lstSize.AddItem (strSizes(i))
lstToppings.AddItem (strToppings(i))
Next i

End Sub

Private Sub lstSize_Click()
'This code will automatically compute the prices per size
Dim tmpVar As Integer
lblSize.Caption = lblSize.Caption & " " & lstSize.Text
For i = 0 To lstSize.ListCount - 1
If lstSize.Selected(i) = True Then 'if selected then add the prices
tmpVar = tmpVar + dblSizePrices(i)

End If
Next i
lblsizesub.Caption = tmpVar
End Sub

Private Sub lstToppings_Click()
Dim i As Integer
Dim tmpVar As Integer

For i = 0 To lstSize.ListCount - 1
If lstSize.Selected(i) Then
tmpVar = tmpVar + dblSizePrices(i) * dblToppingPrices(lstToppings.ListIndex)
End If
Next i
lbltoppingssub.Caption = tmpVar
End Sub

Remove all the code in your program and copy and paste this one.. hope it works
 
Thank you for your help, just wanted to say I havent had a chance to do that yet but I will shortly !! Not sure if it is what I wanted but I will let you know!!
 
Status
Not open for further replies.
Back
Top Bottom