I have a DesktopListbox that has names (column 0) and email addresses (column 1). I’m trying to remove the duplicate entries by stepping through the list in a for/next loop and using .RemoveRowAt
to get rid of duplicates. In cases where the name field is empty in one duplicate entry but not in the other, I want to delete the empty one, so we have a name.
When I put breaks into my code and run it in the debugger, it’s evaluating correctly (for example, when the name on the previous email is empty but the current name is not, it’s finding that and breaking in that bock. However, I don’t seem to be able to remove the row. The resulting listbox is showing all the duplicates. I haven’t done too much with Listboxes, so maybe I’m not getting how they work, but this all seems to be working except for the part where I delete the rows! Any ideas?
// Sort all of the entries based on the email address.
ParsedAddressList.ColumnSortDirectionAt(1) = DesktopListBox.SortDirections.Ascending
ParsedAddressList.SortingColumn = 1
ParsedAddressList.Sort
// Walk through the listBox to remove duplicates
For row As Integer = 0 to ParsedAddressList.RowCount-1
if row > 0 then //avoid OutOfBoundsException
//previous row's email addresses and names
var prevName as string = ParsedAddressList.CellTextAt(row-1,0)
var currName as string = ParsedAddressList.CellTextAt(row,0)
var prevEmail as string = ParsedAddressList.CellTextAt(row-1,1)
var currEmail as string = ParsedAddressList.CellTextAt(row,1)
if prevEmail = currEmail then
//We would prefer both name and email if possible. In some cases, we may have duplicate email
//addresses, but no name. So, we want to delete the empty name row and keep the one with a name
if prevName <> currName AND prevName = "" then
//previous name is empty, so delete that row
ParsedAddressList.RemoveRowAt(row -1)
break //this is breaking when the current name is there but previous is not
elseif prevName <> currName AND currName = "" then
//previous has a name, but current does not, delete current row
ParsedAddressList.RemoveRowAt(row)
break //this is breaking when the previous name is there but current is not
elseif prevName = currName then
//these are a match, so delete the current row
ParsedAddressList.RemoveRowAt(row)
break //this is breaking when the names are equal
end if
end if
end if
Next
//re-sort the listbox (maybe not necessary?)
//and refresh it
ParsedAddressList.ColumnSortDirectionAt(1) = DesktopListBox.SortDirections.Ascending
ParsedAddressList.SortingColumn = 1
ParsedAddressList.Sort
ParsedAddressList.Refresh
What I get is a sorted list, with all the duplicates still there