i m using two list box one(check style list box) is for food item another(simple list box) is for cost.when i selects any fooditem then a input will occour for entry of food quantity.and after selecting all items and entry of their quantity they will store in ms access databse.
please help me for this project coding....

Dani AI

Generated

A short diagnosis and a reliable fix for the symptom reported (wrong cost used / prompts appearing at odd times). The root cause is the interaction between VB6 multi-select list behavior and handling everything inside the List_Click handler: in a multi-select list the control exposes per-item selection via the Selected array, while ListIndex does not necessarily identify the item the user just toggled. Also, the Click event can be triggered in ways that make a simple "scan all Selected items and prompt" approach behave incorrectly. (learn.microsoft.com)

A practical, low-risk approach: remember the previous Selected state for every item and, inside List_Click, compare current Selected states to the saved array to find the single index that changed. Only prompt (or add/remove a cart row) for that toggled index. Keep an "inHandler" flag so adding items to other controls does not re-enter your handler. This keeps logic deterministic and avoids multiplying by the wrong price. Example pattern (VB6):

Private prevSel() As Boolean
Private inHandler As Boolean

Private Sub Form_Load()
  ReDim prevSel(0 To List1.ListCount - 1)
  Dim idx As Long
  For idx = 0 To List1.ListCount - 1
    prevSel(idx) = List1.Selected(idx)
  Next idx
End Sub

Private Sub List1_Click()
  If inHandler Then Exit Sub
  inHandler = True

  Dim idx As Long
  For idx = 0 To List1.ListCount - 1
    If List1.Selected(idx) <> prevSel(idx) Then
      If List1.Selected(idx) Then
        Dim sQty As String
        sQty = InputBox("Enter quantity")
        If IsNumeric(sQty) Then
          lstCost.AddItem CInt(sQty) * Val(List2.List(idx))
        End If
      Else
        ' handle deselect (remove from cart)
      End If
      prevSel(idx) = List1.Selected(idx)
      Exit For
    End If
  Next idx

  inHandler = False
End Sub

A few quick tips: validate the quantity (IsNumeric) before conversion, prefer a small modal quantity form rather than InputBox for better UX, and keep your cart/state in a collection or ListView (columns: item, qty, unit price, subtotal) instead of relying on multiple ListBoxes. The Selected property is the correct way to detect multi-select state; use it rather than relying on ListIndex for this scenario. (learn.microsoft.com)

This pattern should address the timing/multiplication issue; it also makes it easy to show the code to if they want to post a polished solution.

Recommended Answers

All 3 Replies

This sounds like you need help for the entire project, which unfortunately we do not do here. You need to show us what you have already in code, then we can help i9f any errors occur.:)

when i select option button of fastfood it retrieve the data in two listbox.
i m using two list box one(check style list box) is for food item another(simple list box) is for cost.when i selects any fooditem then a input will occour for entry of food quantity.and after selecting two item the input box occur before checked 3rd item and it multiply with previous item cost instead of current item's cost.
please check this error.......
this is my coding

Dim rs As New ADODB.Recordset
Dim i As Integer
Dim var As Integer

Private Sub Form_Load()
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=g:\ak.mdb;Persist Security Info=False"
rs.Open " select* from table1", con, adOpenDynamic, adLockOptimistic

End Sub

Private Sub List1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
var = InputBox("enter quantity")
lstcost.AddItem var * (List2.List(i))

End If
Next i


End Sub
Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
Form1.lstfood.AddItem List1.List(i)

End If
Next i

End Sub

Private Sub Option1_Click()

If rs.State = adStateOpen Then
rs.Close
End If
rs.Open "select productname,cost from table1 where category='ff'"

If Not rs.EOF Then rs.MoveFirst
Do While Not rs.EOF
List1.AddItem rs!ProductName
List2.AddItem rs!cost

rs.MoveNext
Loop
rs.Close

End Sub

I'll post a solution tomorrow morning. Off to home now.;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.