Results 1 to 9 of 9

Thread: [RESOLVED] Remove item from listbox if exists in another listbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    24

    Resolved [RESOLVED] Remove item from listbox if exists in another listbox

    I have 2 listboxes on my form.

    Listbox 1 - populated with "available" field names
    Listbox 2 - populated with "selected" field names.

    There cannot be the same field name in both listboxes - it's either one of the other.

    The logic I was going to apply was to populate Listbox 1 with every field name ... and then as I populated Listbox with the field names which the user has already selected, remove the corresponding item from Listbox 1.

    However I cannot for the life of me get the remove to work!

    How do I remove based on the text string of the item - rather than the index position??

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Remove item from listbox if exists in another listbox

    This would move the selected item from ListBox1 to ListBox2. Is this what you are looking for?
    vb.net Code:
    1. Dim li As Object = ListBox1.SelectedItem
    2. ListBox2.Items.Add(li)
    3. ListBox1.Items.Remove(li)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    24

    Re: Remove item from listbox if exists in another listbox

    Not quite ... my use of the word "selected" may have misled.

    Both listboxes are populated based on data pulled from an MS Access backend.

    tbl_Fields contains a list of all fields available.
    tbl_User_Fields contains a list of all fields a user has 'selected/chosen'.

    So, upon clicking a command button - I need:

    Listbox 1 to populate with all fields from tbl_Fields
    Listbox 2 to populate with all fields the user has chosen (stored in tbl_User_Fields)
    Remove each corresponding item in Listbox 1 where it appears in Listbox 2

    I guess the logic works both ways ... and you could populate either Listbox first. However, its removing an item from the other listbox using the Item text that I can't work out :-s

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Remove item from listbox if exists in another listbox

    Maybe like this:
    vb.net Code:
    1. Dim items = From li In ListBox1.Items Where ListBox2.Items.Contains(li)
    2. For Each item In items.ToList
    3.     ListBox1.Items.Remove(item)
    4. Next

    But why are you doing this way anyways?? You populate a listbox and then remove some of the items. So why add them in the first place? Can't you just remove from list before populating the listbox? That should be faster performancewise too.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    24

    Re: Remove item from listbox if exists in another listbox

    I guess that would mean a different SQL query. My code so far is as follows:

    Code:
                ' populate available fields listbox
                oConnect("SELECT * FROM tbl_Fields ORDER BY Field_Name", "Read")
                Do While oRS.Read()
                    Me.lst_Mails_Fields_Available.Items.Add(New ValueDescriptionPair(oRS("Field_ID"), oRS("Field_Name")))
                Loop
                oDisconnect()
    
                ' populate selected fields listbox
                oConnect("SELECT tbl_Mail_Fields.*, tbl_Fields.Field_Name, * FROM tbl_Fields INNER JOIN tbl_Mail_Fields ON tbl_Fields.Field_ID = tbl_Mail_Fields.Field_ID WHERE Mail_ID = " & CType(Me.cbo_Mails_Menu_Edit.SelectedItem, ValueDescriptionPair).Value & " ORDER BY Field_Name", "Read")
                Do While oRS.Read()
                    Me.lst_Mails_Fields_Selected.Items.Add(New ValueDescriptionPair(oRS("tbl_Fields.Field_ID"), oRS("Field_Name")))
                    Me.lst_Mails_Fields_Available.Items.Remove(oRS("tbl_Fields.Field_ID"))
                Loop
                oDisconnect()
    Its this line which doesn't work:
    Me.lst_Mails_Fields_Available.Items.Remove(oRS("tbl_Fields.Field_ID"))

    I guess what you are suggesting is amending the first SQL statement from:
    SELECT * FROM tbl_Fields ORDER BY Field_Name

    to somesort of unmatched duplicates query?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    24

    Re: Remove item from listbox if exists in another listbox

    [QUOTE=Pradeep1210;3718244]Maybe like this:
    vb.net Code:
    1. Dim items = From li In ListBox1.Items Where ListBox2.Items.Contains(li)
    2. For Each item In items.ToList
    3.     ListBox1.Items.Remove(item)
    4. Next

    This looks nice but doesn't work in .NET 2.0 (sorry, forgot to mention - unavoidable) as it seems to require Linq. Is there another way to acheive the same?

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Remove item from listbox if exists in another listbox

    [QUOTE=TUSSFC;3718270]
    Quote Originally Posted by Pradeep1210 View Post
    Maybe like this:
    vb.net Code:
    1. Dim items = From li In ListBox1.Items Where ListBox2.Items.Contains(li)
    2. For Each item In items.ToList
    3.     ListBox1.Items.Remove(item)
    4. Next

    This looks nice but doesn't work in .NET 2.0 (sorry, forgot to mention - unavoidable) as it seems to require Linq. Is there another way to acheive the same?
    Something like this. I have .NET 3.5, but it should work in 2.0:

    vb.net Code:
    1. Dim item As Object  ' Don't remember if I can narrow the type here
    2.  
    3. For Each item In ListBox1.Items
    4.     If ListBox2.Contains(item) Then
    5.         ListBox1.Items.Remove(item)
    6.     End If
    7. Next

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Remove item from listbox if exists in another listbox

    Use this query:

    sql Code:
    1. SELECT * FROM tbl_Mail_Fields
    2. LEFT JOIN tbl_Fields ON tbl_Fields.Field_ID = tbl_Mail_Fields.Field_ID

    When you want to populate all fields box, don't set any filter.
    When you want to populate the selected fields set filter tbl_Fields.Field_ID Is Null
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    24

    Re: Remove item from listbox if exists in another listbox

    Thanks chaps ... marking this as resolved. I actually went with something totally different and made use of an array.

    Code:
                ' count how many mail fields there are for the selected mail
                Dim strSelFieldCount As Integer
                oConnect("SELECT * FROM tbl_Mail_Fields WHERE Mail_ID = " & CType(Me.cbo_Mails_Menu_Edit.SelectedItem, ValueDescriptionPair).Value & "", "Update")
                oCmd.CommandText = "SELECT COUNT(*) FROM tbl_Mail_Fields WHERE Mail_ID = " & CType(Me.cbo_Mails_Menu_Edit.SelectedItem, ValueDescriptionPair).Value & ""
                strSelFieldCount = oCmd.ExecuteScalar() - 1
                oDisconnect()
    
                ' populate selected fields listbox
                ' store selected field IDs in an array
                Dim ar_Sel(strSelFieldCount) As Integer
                Dim i_Sel As Integer = 0
                oConnect("SELECT tbl_Mail_Fields.*, tbl_Fields.Field_Name, * FROM tbl_Fields INNER JOIN tbl_Mail_Fields ON tbl_Fields.Field_ID = tbl_Mail_Fields.Field_ID WHERE Mail_ID = " & CType(Me.cbo_Mails_Menu_Edit.SelectedItem, ValueDescriptionPair).Value & " ORDER BY Field_Name", "Read")
                Do While oRS.Read()
                    Me.lst_Mails_Fields_Selected.Items.Add(New ValueDescriptionPair(oRS("tbl_Fields.Field_ID"), oRS("Field_Name")))
                    ar_Sel(i_Sel) = oRS("tbl_Fields.Field_ID")
                    i_Sel = i_Sel + 1
                Loop
                oDisconnect()
    
                ' populate available fields listbox if not found in array
                oConnect("SELECT * FROM tbl_Fields ORDER BY Field_Name", "Read")
                Do While oRS.Read()
                    ' only add to listbox if doesn't exist in array
                    If IsInArray(oRS("Field_ID"), ar_Sel) = False Then
                        Me.lst_Mails_Fields_Available.Items.Add(New ValueDescriptionPair(oRS("Field_ID"), oRS("Field_Name")))
                    End If
                Loop
                oDisconnect()
    May be a little messy, but I get the desired results and as the recordsets I'm doing to be dealing with are quite small - performance is OK.

Tags for this Thread

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