Results 1 to 6 of 6

Thread: listbox and checkboxes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Hello...

    The user is shopping for a computer...
    After having several items in the listbox, user decided not to include the Zip Drive.

    Form1: Has all the combo boxes; listboxes and checkboxes

    Form2: is the receipt where it shows all the selected items and the prices for each selection, except for the
    lstComponents (a listbox populated from checkbox selection) which will only show a subtotal for all the addidtional components (zipDrive, DVD, modem, cdrom,etc...)

    I tried using the code below for deleting a previously selected item (checked from the checkbox and was then
    additem into the listbox).

    Here's my code in Form1:

    Private Sub chkZipDrive_Click()
    If frmComputerSales.chkZipDrive.Value = 1 Then
    frmReceipt.lstComponents.AddItem "Zip Drive"
    booZipDrive = True
    intzipdrive = consZipDrive
    Else
    booZipDrive = False
    intzipdrive = 0
    frmReceipt.lstComponents.clear 'tried this one and it obviously cleared the whole listbox... I only wanted to clear the "Zip Drive" if it is unchecked by user...

    End If
    End Sub


    Thanks,


    Eena

  2. #2
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137
    When you call the addItem method of the list box there is a second parameter you can pass to it. This index parameter marks that item and you can then call the removeItem method of the list box passing it the index of the item you want to remove.

    ie.
    Code:
    Dim zipIndex as integer
    zipIndex = 0 'or any other unique integer
    Private Sub chkZipDrive_Click()
        If chkZipDrive.Value = vbChecked Then
            frmReceipt.lstComponents.AddItem "Zip Drive", zipIndex
        Else
            frmReceipt.lstComponents.RemoveItem zipIndex
        End If
    End Sub
    The only requirement is that each item in the list box has a unique id. So it would be a good idea to assign each component that may possibly feature in the list box a number to use as it's index and then go off that.

    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Hi Matt..

    The code below gave me an error..."Invalid procedure call or argument"...

    Private Sub chkZipDrive_Click()
    Dim zipindex As Integer
    zipindex = 2
    If frmComputerSales.chkZipDrive.Value = 1 Then
    frmReceipt.lstComponents.AddItem "Zip Drive", 2
    intZipDrive = consZipDrive
    Else

    frmReceipt.lstComponents.RemoveItem zipindex
    intZipDrive = 0

    End If
    End Sub


    Also, when checkbox is unchecked... it deleted the wrong item... that happen to be in that index parameter...

    We have to consider that the user is clicking randomly...and will be checking and unchecking (back and forth)until they decide the final selection and
    an affordable total...

    Is there any other code that will work with this scenario??

    Thanks,




    Eena

  4. #4
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137
    Sorry just remembered an important detail that I left out before.

    When assigning index numbers to the items in a list they MUST follow the 0, 1, 2, 3 .... sequence

    You cannot have and item 3 then 2, 7, 0 etc. You must have them numbered from 0 up.

    That will be the cause of the "Invalid procedure call or argument" error as you are trying to make it no 2.

    You could store the items list index number in the controls tag property. eg.

    Code:
    Private Sub chkZipDrive_Click() 
      Dim zipindex As Integer 
      zipindex = frmReceipt.lstComponents.listCount - 1
      If frmComputerSales.chkZipDrive.Value = 1 Then 
        frmReceipt.lstComponents.AddItem "Zip Drive", zipIndex
        chkZipDrive.tag = zipIndex
      Else 
        If chkZipDrive.tag <> - 1 Then 
          frmReceipt.lstComponents.RemoveItem chkZipDrive.tag
          chkZipDrive.tag = -1
        End If
      End If 
    End Sub
    Haven't tested the above code as I don't have VB with me but it should work.

    You'll see that I have also added a check to stop it removing the item if it has already been removed.

    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Hi Matt...

    Copied and pasted the code you typed and it still gave me the same call argument error...

    Any other suggestions?

    Thanks,
    Eena

  6. #6
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137
    Not really

    Just ensure that the item you are adding is next in numerical order.

    If that doesn't work step through the code, line by line and tell me the exact line that it crashes on.

    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

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