Retaining the Checkedstate of a listbox even after the form is reloaded

irfi

Member
Joined
Sep 24, 2009
Messages
16
Programming Experience
1-3
hi everyone,

I am populating a Checkedlistbox "lstExpert" from table "TableA" succesfully.

Whenever an item is checked in the Checkedlistbox "lstExpert", i am saving it into another table "GroupExpert".

For some reason i am using a recordset to save the data into an sql table.

VB.NET:
' For saving data into sql table when an item is checked
        Dim i As Integer
        Dim rsgroup As New ADODB.Recordset
        Dim qry As String

qry = "Select * from GroupExpert where ID=" & Label9.Text

rsgroup.Open(Qry, db, ADODB.CursorTypeEnum.adOpenDynamic,ADODB.LockTypeEnum.adLockOptimistic)

        For i = 0 To lstExpert.Items.Count - 1
                If lstExpert.GetSelected(i) = True Then
                rsgroup.AddNew()
                rsgroup.Fields("ID").Value = Label9.Text
                rsgroup.Fields("ExpertName").Value = lstExpert.Items.Item(i)
                rsgroup.UpdateBatch(ADODB.AffectEnum.adAffectAll)
End If
        Next
               rsgroup.Close()
        Call fillgrouplistbox() ' To show saved names in Table "GroupExpert"  into another listobx called "Listbox1"

Now what i am stuck is HOW TO RETAIN the CHECKSTATE of the Checkedlistbox "lstExpert" meaning even when i close the from and re-open it, the names that were selected and saved in Table "GroupExpert " should remain checked.

I tried the following code but no success so far.

VB.NET:
For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
            lstExpert.SetItemCheckState((lstExpert.Items.IndexOf(ListBox1.SelectedItems.Item(i))), CheckState.Checked)
        Next


Is it the right way to do by comparing index in Checkedlistbox "lstExpert" and "Listbox1"

Is there a way to bind it directly to the table "GroupExpert" meaning if the value of the Checkedlistbox "lstExpert" is present in the "GroupExpert" then the item will be cheked... I tried the following code and nothing seems to work.....

VB.NET:
For i As Integer = 0 To Me.lstExpert.Items.Count - 1
        For j As Integer = 0 To rsgroup.GetRows.Rows.Count - 1
             If Me.lstExpert.Items.Item(i).item(0) = rsgroup.GetRows.Rows(j).Item(0) Then
              Me.lstExpert.SetItemChecked(i, True)
             End If
         Next
        Next

Any suggestions....

Thanx and Cheers
irfi
 
Why not create your listbox outside of the form and pass through on a parameter, or set as a property? Even if the form disposed, the listbox will continue to exist, including its current state.

If you are not comfortable in using a control in that way, then another way is to keep track of the listbox state, say as a datatable, pass through/set as a property and re-populate the listbox from the datatable contents.

In whatever solution you decide, you are going to need something outside of the form to keep track of the listbox state and pass through to the form somehow.
 
I'd use a DataGridView with a DataGridViewCheckBoxColumn and a DataTable instead. And ditch the ADODB.Recordset
 
Back
Top