简体   繁体   中英

CheckListBox - Check items based on Array value - VB.NET

OK... After many many days of trying to figure it out, I have no option but to ask for help. I have Googled my little heart out and now it has led me here. PLEASE HELP.

I get a string from my MySQL database server. I then split the string and put it into an array.

Than I go through my checklistbox and see if any of the entries in there are equal to an array value. The problem is, it doesn't work and for the life of me I can't get it to work.

This is somewhat of a template I have and if anyone can help me out with this problem, I would be more than thankful.

    Using connection As New MySqlConnection("datasource = " + IPADDRESS + "; username = '" + USERNAME + "'; password='" + PASSWORD + "' ; database = '" + DBASE + "'")
        Using Command As New MySqlCommand("SELECT * FROM hazinc WHERE title = '" + ListBox1.Text + "'", connection)

            connection.Open()

            'Command.ExecuteReader()
            Using reader As MySqlDataReader = Command.ExecuteReader()
                While reader.Read()
                    'TextBox2.Text = reader("title")
                    STRINGRR = reader("involved")

                End While

                Dim NEWSTRINGRR As String() = STRINGRR.Split(",")

                Dim CC As Integer

                CC = 0

                For Each X In NEWSTRINGRR
                    For I = 0 To clbEmployees.Items.Count - 1
                        Try
                            If clbEmployees.Items(I).ToString() = NEWSTRINGRR(I).ToString() Then
                                SetItemChecked(I)
                            End If
                        Catch ex As Exception
                            ' MsgBox(ex.ToString)
                        End Try

                    Next
                Next



            End Using

            connection.Close()

        End Using
    End Using

Anyone? Please...?

I don't know for sure with so little code, but why are you using the same index for your If condition? Then what good does X does?

I think it should be like :

For Each X In NEWSTRINGRR
    For I = 0 To clbEmployees.Items.Count - 1
        If clbEmployees.Items(I).ToString() = X.ToString() Then
           SetItemChecked(I)
        End If
    Next
Next

CMIIW.

You do know STRINGRR is only going to have the value of the last row. Add yes you need to post all your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM