For my coursework I am making a booking system in Visual Basic that also stores data regarding outstanding payments, I'm using a list box to display names of registered customers saved in an array using the index of the Listbox item in conjunction with the primary key of the array, e.g:
Array:
| User ID | Amount Owed |
|---|---|
| 0 | 100 |
| 1 | 0 |
| 2 | 200 |
This is then added into a Listbox, the customer data such as name and contact information is stored in a separate array to the invoice data, linked via the User ID as the primary key.
Listbox:
| Index | Name |
|---|---|
| 0 | John Doe |
| 1 | Jane Doe |
| 2 | Joe Bloggs |
This system all works fine until I want to add a check box that filters out people who do not have an outstanding payment Filtered Listbox:
| Index | Name | User ID |
|---|---|---|
| 0 | John Doe | 0 |
| 1 | Joe Bloggs | 2 |
As you can see the Index of the Listbox is now no longer the same as the User ID, which in turn breaks my other operations that involve the use of the .SelectedIndex function.
Here is the code involved in this process
Private Sub SortOpenInvoice_CheckedChanged(sender as Object, e as EventArgs) _
Handles CheckBox1.CheckedChanged
Dim HasOpenInvoice As Boolean
HasOpenInvoice = False
If SortOpenInvoice.Checked = True Then
For i = 0 To numofCustomers - 1
For j = 0 To numofInvoices - 1
If invoices(j).AmountOwed > 0 And invoices(j).CustomerID = customers(i).CustomerID Then 'Searches for an open invoice (outstanding payment) linked to a customers ID
HasOpenInvoice = True 'Flag for if there is an open invoice (outstanding payment)
End If
Next
If HasOpenInvoice = False Then
lbxMemberList.Items.RemoveAt(Convert.ToInt32(customers(i).CustomerID)) 'Removes index if no open invoice is found
Else
HasOpenInvoice = False
End If
Next
...
End Sub
What can I do to preserve the Index value after particular fields have been filtered out? Or are there any other alternative solutions that you could suggest?
Thanks!