0

Hello,

i have 3 listboxes on my screen..
A listbox with emails from datasource A, a listbox with emails from datasource B and an empty listbox

i want in the empty listbox the emails that are in A but not in B

i've got this but it doesn't put any emails in the listbox...


For k As Integer = lsbSQL.Items.Count - 1 To 0 Step -1
If lsbAccess.Items.Contains(lsbSQL.Items(k)) Then
MessageBox.Show("Item Found")
lsbVerschil.Items.Add(lsbSQL.Items(k))
Else
MessageBox.Show("Item Not Found")
End If
Next

2
Contributors
7
Replies
13
Views
8 Years
Discussion Span
Last Post by Teme64
0

Actually your code does the opposite thing. It adds to third list box items from list box A that do exist in list box B. Otherwise, there's nothing wrong with your code.

Here's your code (list boxes renamed)

ListBox1.Items.Add("first@acme.com")
ListBox1.Items.Add("second@acme.com")
ListBox2.Items.Add("second@acme.com")
ListBox2.Items.Add("third@acme.com")

For k As Integer = ListBox1.Items.Count - 1 To 0 Step -1
  If Not ListBox2.Items.Contains(ListBox1.Items(k)) Then
    MessageBox.Show("Item Found")
    ListBox3.Items.Add(ListBox1.Items(k))
  Else
    MessageBox.Show("Item Not Found")
  End If
Next

Only change is the test If Not ListBox2.Items.Contains(ListBox1.Items(k)) Then The code works just fine...

0

thanks... but the problem is in the 3rd listbox there comes:system.data.datarowview and not the text

0

You mean you get the text "system.data.datarowview" to the third list box?

You could try an explicit conversion of a list box item to a string

ListBox1.Items.Add("first@acme.com")
ListBox1.Items.Add("second@acme.com")
ListBox2.Items.Add("second@acme.com")
ListBox2.Items.Add("third@acme.com")


For k As Integer = ListBox1.Items.Count - 1 To 0 Step -1
  If Not ListBox2.Items.Contains(ListBox1.Items(k)) Then
    MessageBox.Show("Item Found")
    Dim TempStr As String
    TempStr = CStr(ListBox1.Items.Item(k))
    ListBox3.Items.Add(TempStr)
  Else
    MessageBox.Show("Item Not Found")
  End If
Next

Does that help anything?

0

this is what is in my form load...
Dim blnConGelukt As Boolean
blnConGelukt = mycon.funConEmp()
myacc.fstudenten()
Me.lsbAccess.DataSource = myacc.p_mydv.ToString
Me.lsbAccess.DisplayMember = "Email"
Me.lsbAccess.ValueMember = "Id"
Me.lsbSQL.DataSource = mycon.p_datav.ToString
Me.lsbSQL.DisplayMember = "StudentPrivéEmail"
Me.lsbSQL.ValueMember = "StudentId"

now when i click on a button

For k As Integer = lsbSQL.Items.Count - 1 To 0 Step -1

If Not lsbAccess.Items.Contains(lsbSQL.Items(k)) Then

MessageBox.Show("Item Found")

Dim TempStr As String

TempStr = CStr(lsbSQL.Items.Item(k))

lsbVerschil.Items.Add(TempStr)

Else

MessageBox.Show("Item Not Found")

End If

Next

but i get the following error:

De conversie van type DataRowView naar type String is ongeldig.
The conversion from typ Datarowview to type String isn't good

0

Since the list boxes are databound, the comparison can't be done in a straightforward manner. You have to grab first the underlying dataview and compare items in that (and from the right column too).

Here's a rewritten version

Dim k As Integer
Dim x As Integer
Dim bFound As Boolean ' Flag if item exists
Dim TempStr As String

For x = 0 To lsbAccess.Items.Count - 1 ' Loop the first listbox
  bFound = False
  ' Loop the second listbox
  For k = 0 To lsbSQL.Items.Count - 1
    ' Compare from rows x and k the second column value (=email)
    If CType(lsbAccess.DataSource, DataView).Item(x).Item(1).ToString = CType(lsbSQL.DataSource, DataView).Item(k).Item(1).ToString Then
      ' Exists in the second listbox. Exit inner loop
      bFound = True
      Exit For
    End If
  Next k
  If Not bFound Then
    ' Item wasn't found, add item to the third box
    ' Item is taken from the row number x and second column
    TempStr = CType(lsbAccess.DataSource, DataView).Item(x).Item(1).ToString
    lsbVerschil.Items.Add(TempStr)
  End If
Next x

Notice that the comparison is case sensitive. There may be some "easier" way to do comparison but this worked. I also expected that the DataSource property is set to a dataview object. Check and change Me.lsbAccess.DataSource = myacc.p_mydv.ToString and Me.lsbSQL.DataSource = mycon.p_datav.ToString lines so that the DataSource property is a dataview object.

0

thanks, it works, and yes my datasource is linked to a dataview, i copied the wrong datasources

thanks a lot ! was looking for answer for week

the only thing i needed to change was lsbSQL.DataSource, DataView).Item(k).Item(1).ToString to lsbSQL.DataSource, DataView).Item(k).Item("columname").ToString

0

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.