Link to home
Start Free TrialLog in
Avatar of PeterErhard
PeterErhard

asked on

CheckedListBox on Tab Control

I've got another strange problem with the CheckedListBox control which I have showing on a Tab Control. When I check a few items, switch it another tab and switch back the CheckedListBox has reset itself and lost the selections I made.

Isn't the CheckedListBox supported within the Tab Control? Or....?
Avatar of davidrichardson
davidrichardson

if the are bound controles try updating the dataset before switching to another tab
Avatar of PeterErhard

ASKER

Thanks for your response David.

Problem being is that if the user selects a few options on the CheckedListBox I don't want to save until these options to the database until the save button is clicked. Is there anyway around that?
Anyone?
ASKER CERTIFIED SOLUTION
Avatar of davidrichardson
davidrichardson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for that - turns out it's a bug with VB.net - not fixed in the service pack either.

I've got it working with this but now it turns out my saving and loading isn't working because I'm no longer using a dataset.

    Private Function LoadEthnicGroups() As Boolean

        Dim cmd As SqlCommand = cnn.CreateCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "procLoadEthnicGroupOptions"

        Dim reader As SqlDataReader = cmd.ExecuteReader

        While reader.Read
            With reader
                ListBox_EthnicGroup.Items.Add(.GetValue(.GetOrdinal("EthnicGroup")))
                ListBox_EthnicGroup.Items.IndexOf(.GetValue(.GetOrdinal("LookUp_EthnicGroupIDAuto")))
            End With
        End While

        reader.Close()

        LoadEthnicGroups = True

    End Function

I can't believe what a pain in the arse this CheckedListBox control is. Do you know what I can replace this line of the code with - ItemIndex = CType(ListBox_EthnicGroup.Items(Count), DataRowView)("LookUp_EthnicGroupIDAuto") ?

    Private Function LoadClientEthnicGroup() As Boolean

        Dim EthnicGroupCount As Integer, EthnicGroupIDAuto As Integer
        Dim ItemIndex As Object, Count As Integer

        Try
            'Get Number of Ethnic Group Items in ListBox
            EthnicGroupCount = ListBox_EthnicGroup.Items.Count()

            Dim cmd As SqlCommand = cnn.CreateCommand
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "procLoadClientEthnicGroupOptions"

            cmd.Parameters.Add(New SqlParameter("@ClientIDAuto", SqlDbType.Int, 4))
            cmd.Parameters("@ClientIDAuto").Value = 1

            Dim dr As SqlDataReader = cmd.ExecuteReader

            While dr.Read
                With dr
                    EthnicGroupIDAuto = CInt(.GetValue(.GetOrdinal("EthnicGroupIDAuto")))

                    'Find out the Index for the EthnicGroupIDAuto Saved
                    'And check the item
                    For Count = 0 To EthnicGroupCount - 1
                        'ItemIndex = CType(ListBox_EthnicGroup.Items(Count), DataRowView)("LookUp_EthnicGroupIDAuto")
                        'ItemIndex = CType(ListBox_EthnicGroup.Items(Count), CheckedListBox.SelectedIndexCollection)
                        'ItemIndex = CType(ListBox_EthnicGroup.Items(Count), CheckedListBox.SelectedIndexCollection)
                        If CInt(EthnicGroupIDAuto) = CInt(ItemIndex) Then
                            ListBox_EthnicGroup.SetItemChecked(Count, True)
                        End If
                    Next
                End With
            End While
            dr.Close()

            LoadClientEthnicGroup = True

        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try
    End Function

and the same here:

    Private Function SaveClientEthnicGroup() As Boolean()
        Try
            Dim Index As Integer

            Dim cmdDelete As New Data.SqlClient.SqlCommand("Delete from ClientIDAuto_EthnicGroupIDAuto where ClientIDAuto = " & m_ClientNumber, cnn)
            cmdDelete.ExecuteNonQuery()

            Dim cmdInsert As New Data.SqlClient.SqlCommand("Insert into ClientIDAuto_EthnicGroupIDAuto (ClientIDAuto, EthnicGroupIDAuto) Values (@ClientIDAuto, @EthnicGroupIDAuto)", cnn)
            cmdInsert.Parameters.Add("@ClientIDAuto", m_ClientNumber)
            cmdInsert.Parameters.Add("@EthnicGroupIDAuto", SqlDbType.Int)

            For Each Index In ListBox_EthnicGroup.CheckedIndices
                cmdInsert.Parameters("@EthnicGroupIDAuto").Value = CType(ListBox_EthnicGroup.Items(Index), DataRowView)("LookUp_EthnicGroupIDAuto")
                cmdInsert.ExecuteNonQuery()
            Next
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try
    End Function