'Generate Panels based on dynamic location

I have a big panel and a function which generates panels which hold two labels. Each panel is generated under the previous one, in a message-based system.

The function looks like this:

Function GenerateMessage(ByVal author As String, ByVal message As String)

    Dim user_name As String = author
    Dim user_id As String = message

    Dim Lbl As New Panel
    AddHandler Lbl.DoubleClick, AddressOf DoubleC
    AddHandler Lbl.MouseDown, AddressOf MouseDownme
    AddHandler Lbl.MouseEnter, AddressOf Hoverme
    AddHandler Lbl.MouseLeave, AddressOf Leaveme
    Lbl.Name = "messagePanel"
    If lastSavedControl Is Nothing Then
        Lbl.Location = New Point(My.Settings.LastMessageX, My.Settings.LastMessageY + 70)
    Else
        Lbl.Location = New Point(lastSavedControl.Location.X, lastSavedControl.Location.Y + 70)
    End If
    Lbl.Size = New System.Drawing.Size(containerReceived.Width, containerReceived.Height)
    Lbl.Font = New Font("Leelawadee UI", 10, FontStyle.Regular)
    Lbl.BorderStyle = BorderStyle.None
    Lbl.Cursor = Cursors.Hand
    Lbl.Visible = True
    Lbl.BackColor = Color.Transparent
    Lbl.ForeColor = Color.Silver
    Lbl.Anchor = AnchorStyles.Left + AnchorStyles.Top
    chatPanel.Controls.Add(Lbl)

    Debug.WriteLine(Lbl.Location.X)
    Debug.WriteLine(Lbl.Location.Y)

    My.Settings.LastMessageX = Lbl.Location.X
    My.Settings.LastMessageY = Lbl.Location.Y

    Dim newAuthor As New Label
    AddHandler newAuthor.DoubleClick, AddressOf LabelDoubleC
    AddHandler newAuthor.MouseDown, AddressOf LabelMouseDown
    AddHandler newAuthor.MouseEnter, AddressOf LabelHover
    AddHandler newAuthor.MouseLeave, AddressOf LabelMouseLeave
    newAuthor.Name = "authorLbl"
    newAuthor.AutoSize = True
    newAuthor.Location = New Point(messageReceived.Location.X, 6) ' 9, 5
    newAuthor.Font = New Font("Leelawadee UI", 12, FontStyle.Regular)
    newAuthor.Cursor = Cursors.Hand
    newAuthor.Anchor = AnchorStyles.Top + AnchorStyles.Left
    newAuthor.Visible = True
    newAuthor.Text = $"@{user_name}"
    newAuthor.Tag = "userLbl"
    newAuthor.BackColor = Color.Transparent
    newAuthor.ForeColor = Color.Black
    Lbl.Controls.Add(newAuthor)
    newAuthor.Parent = Lbl

    Dim newMessage As New Label
    AddHandler newMessage.DoubleClick, AddressOf LabelDoubleC
    AddHandler newMessage.MouseDown, AddressOf LabelMouseDown
    AddHandler newMessage.MouseEnter, AddressOf LabelHover
    AddHandler newMessage.MouseLeave, AddressOf LabelMouseLeave
    newMessage.Name = "messageLbl"
    newMessage.Location = New Point(messageReceived.Location.X, 35) ' 9, 5
    For Each lineofText In chatBox.Lines
        newMessage.Size = New Point(messageReceived.Width, 12 * lineofText)
    Next
    newMessage.Font = New Font("Leelawadee UI", 9, FontStyle.Regular)
    newMessage.Cursor = Cursors.Hand
    newAuthor.Anchor = AnchorStyles.Top + AnchorStyles.Left
    newMessage.Visible = True
    newMessage.Text = user_id
    newMessage.Tag = "idLbl"
    newMessage.BackColor = Color.Transparent
    newMessage.ForeColor = Color.Black
    Lbl.Controls.Add(newMessage)
    newMessage.Parent = Lbl

End Function

My.Settings.LastMessageX and My.Settings.LastMessageY are two integers defined which are saved on each panel generation. lastSavedControl is also a variable, defined at run-time, which holds the entire control. But using the Y location from lastSavedControl doesn't seem to make a difference.

I also have AutoScroll enabled on the big panel to help with scrolling when there are too many messages to fit.

When the scrollbar is enabled and scrolled all the way down, the Y (height) property on the generated panel seems to skip. It doubles every time I generate a new panel. I suspect it's because the Y value of the big panel changes when the scrollbar is used. I don't know how to get around this.

Example image of panels skipping

I have unsuccessfully tried to save the location of the previously generated panel, and the bug seems to bypass that, likely because of the Y value of the big panel changing when scrolling.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source