Results 1 to 3 of 3

Thread: Display CheckedListbox values into Listbox2 and retain CheckedState

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Display CheckedListbox values into Listbox2 and retain CheckedState

    Hi everyone,
    With help from "dbasnett" i am able to show Checkedlistbox1 items into listbox2.

    Now what i am trying to achieve is that if listbox2 contains checkedlistbox1 items then checkedlistbox1 should retain checked state even after the from is closed and re-opened again.

    Since i am saving the checkedlistbox1 values into a Table - is it possible to bind those to retain the checked state even after the from is closed and re-opened again - or it can be done comparing the values from both listboxes - any ideas!!


    I tried this but unsuccessful so far.

    Any help

    Cheers
    IrFi


    Code:
    '-------------- This code from "dbasnett" is working FINE
    
    Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, _
                                              ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
                                          Handles CheckedListBox1.ItemCheck
            If e.NewValue = CheckState.Checked AndAlso _
                Not CheckedListBox2.Items.Contains(CheckedListBox1.Items(e.Index)) Then
                CheckedListBox2.Items.Add(CheckedListBox1.Items(e.Index))
    
    Call AddRecordToTable() ' Made a function to save selection into Table in SQL
    
            ElseIf e.NewValue = CheckState.Unchecked AndAlso _
                 CheckedListBox2.Items.Contains(CheckedListBox1.Items(e.Index)) Then
                CheckedListBox2.Items.Remove(CheckedListBox1.Items(e.Index))
            End If
        End Sub

    Code:
    ' --------Having trouble with this code 
    
    For i As Integer = 0 To Me.Checkedlistbox1.Items.Count - 1
                If ListBox2.Items.Contains(Checkedlistbox1.Items(i)) Then
                    Checkedlistbox1.CheckedItems(i) = True
                End If
            Next

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

    Re: Display CheckedListbox values into Listbox2 and retain CheckedState

    Instead of "Contains" (which will check for equal references), try using FindByName or Value etc.
    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
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Re: Display CheckedListbox values into Listbox2 and retain CheckedState

    Thanx for your reply. I changed the following and now its working fine!

    I guess its the right way to do...

    Cheers
    IrFi

    Code:
    For i As Integer = 0 To Me.Checkedlistbox1.Items.Count - 1
                If ListBox2.Items.Contains(Checkedlistbox1.Items(i)) Then
                    Checkedlistbox1.SetItemChecked(i, True) 
                End If
            Next

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