Properly compare items in 2 list boxes with different sizes

I’m trying to compare data between two list boxes and highlight text that differs between the two.
It works if each listbox has the same number of elements (rows). But I obviously get an OutOfBounds exception if they have different amount of items. How do I get around that?

I’ve tried storing the values of the cells in 2 separate arrays and that works fine, but then I don’t know how to make that connection back to the listbox to color the text when/if needed.

This is the code for the CellTextPaint event handler:

dim source as Listbox = lstbox1
dim destination as Listbox = lstbox2

if destination.Listcount >= 1 then
  If source.Cell(row, -1) = destination.Cell(row, -1) Then
    g.ForeColor = Color.Green
    g.ForeColor = Color.Red
  end if
End If

And here is a method I found for checking the elements of both arrays:

//contents of a() is listbox1 - contents of b() is listbox2
if ubound(a)=UBound(b) and (UBound(a)+UBound(b))>=0 then
  dim i as integer
  dim k as integer
  dim ok as integer
  //ok counts how many times there are the same elements
  for i=0 to ubound(a)
    for k=0 to UBound(b)
      if a(i)=b(k) then ok=ok +1
  if ok>UBound(a) then 
    MsgBox("All of the files were successfully transfered.")
    return true
    //different dimensions 
    //or one of the 2 arrays is empty
    MsgBox("Some files were not transferred.")
    return false
  end if
end if

I’m okay with going the route of the arrays as that seems to be the most efficient way, but again, I’m unsure of how to connect that back to the CellTextPaint handler.

but that assumes you are looping with a greater number of rows.

Assuming you populate list 2 with ‘what was copied’, and want to highlight in list 1 those rows that didnt get there, what you need to do is discover if list1 item is in list 2

Checking in the CellTextPaint every time might be slow.
Performing one check, and tagging each row is better.

One Suggestion:

1/ Do the copy
2/ Create a dictionary populated with the list of items in list 2
3/ For each row in list 1, if the dictionary.haskey (this thing), change the rowtag to be ‘copied’
4/ In the CellTextPaint, change the color based on the state of the rowtag

I like that idea. I’ll give that a whirl.

I’m liking this workflow. However, probably because my lack of knowledge of the CellTextPaint handler, I’m not sure how to invoke at at the right time.
The code in there runs as soon as I drop files onto the listbox. But I need it to run at a later time.

  1. Files dropped on box 1
  2. Files dropped on box 2
  3. Button is pressed to do the dictionary stuff
  4. Need CellTextPaint to be invoked now.

That needs the cells to require refreshing.
I find one quick way to do that is to turn on and off the Gridlines Horizontal setting

1 Like