0

I am trying to show some data from my mysql database in the flowlayoutpanel. I have created an UserControl with 3 diferent labels witch I need, Title, Message and Time. And i want to pass these 3 informations from my mysql query to the UserControl and then for each entry in my database create a seperate UserControl and output it in the FlowLAyoutPanel.

My UserControl Code:

Public Class ChatControl

    Public _title As String
    Public _message As String
    Public _time As String

    Public Property Title As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Public Property Message As String
        Get
            Return _message
        End Get
        Set(ByVal value As String)
            _message = value
        End Set
    End Property

    Public Property Time As String
        Get
            Return _time
        End Get
        Set(ByVal value As String)
            _time = value
        End Set
    End Property

End Class

and this is my Form code:

Private Sub ChromeButton2_Click(sender As Object, e As EventArgs) Handles ChromeButton2.Click
    Dim SDA As New MySqlDataAdapter
    Dim dbDataSet As New Data.DataTable
    Dim bSource As New BindingSource
    Dim mysqlconn As New MySqlConnection(ConnectionString)
    mysqlconn.Open()
    Dim sqlcomand As String = "SELECT   Text,
                                        User,
                                        Time
                                       FROM     call_center.DB_Script_Chat;"
    command = New MySqlCommand(sqlcomand, mysqlconn)
    SDA.SelectCommand = command
    SDA.Fill(dbDataSet)
    bSource.DataSource = dbDataSet
    SDA.Update(dbDataSet)
    For Each row As DataRow In dbDataSet.Rows
        Dim myChat As New ChatControl()
        myChat.Title = row.Item("User").ToString
        myChat.Message = row.Item("Text").ToString
        myChat.Time = row("Time").ToString
        If FlowLayoutPanelChat.Controls.Count < 0 Then
            FlowLayoutPanelChat.Controls.Clear()
        Else
            FlowLayoutPanelChat.Controls.Add(myChat)
        End If
    Next
End Sub

So the FlowLayoutPanel shows 3 entrys (becouse i have in my test database just 3 rows of data) but the data from the cells are not filled in the Title, Message and Time. It shows allways the defoult value i have set in the UserControl.

Please help. Thx

1
  • Have you tried moving "FlowLayoutPanelChat.Controls.Clear()" before starting (outside) of the loop? Then you don't need the If/Else in the loop. My guess is, "FlowLayoutPanelChat.Controls.Clear()" is never called? Commented Jul 14, 2020 at 13:58

1 Answer 1

1

Firstly, this code is wrong:

For Each row As DataRow In dbDataSet.Rows
    Dim myChat As New ChatControl()
    myChat.Title = row.Item("User").ToString
    myChat.Message = row.Item("Text").ToString
    myChat.Time = row("Time").ToString
    If FlowLayoutPanelChat.Controls.Count < 0 Then
        FlowLayoutPanelChat.Controls.Clear()
    Else
        FlowLayoutPanelChat.Controls.Add(myChat)
    End If
Next

How can the number of controls in the panel be less than zero? I'm guessing that what you actually want to do is clear any existing controls first, then add all the new ones. In that case, the call to Clear should be outside the loops. There's no If statement required because Clear will have no effect if there are no controls:

FlowLayoutPanelChat.Controls.Clear()

For Each row As DataRow In dbDataSet.Rows
    Dim myChat As New ChatControl()
    myChat.Title = row.Item("User").ToString
    myChat.Message = row.Item("Text").ToString
    myChat.Time = row("Time").ToString
    FlowLayoutPanelChat.Controls.Add(myChat)
Next

For the record, that code can be simplified quite a bit too:

FlowLayoutPanelChat.Controls.Clear()

For Each row As DataRow In dbDataSet.Rows
    FlowLayoutPanelChat.Controls.Add(New ChatControl With {.Title = row("User").ToString(),
                                                           .Message = row("Text").ToString(),
                                                           .Time = row("Time").ToString()})
Next

As for the issue, look at your ChatControl class and show me where there is any reference to Labels in that code. How can you expect your Labels to display the text you pass in if you have no code to do that? Get rid of those fields and just have the properties pass through directly to the Text properties of the Labels, e.g.

Public Property Title As String
    Get
        Return titleLabel.Text
    End Get
    Set
        titleLabel.Text = value
    End Set
End Property
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.