Control location changed after Add to a container

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
I have notice that a control's location sometime changes after adding to a container, why is that?

VB.NET:
            Dim panel As New Panel
            panel.Size = New Size(flowAVPanel.Width - 31, 40)
            flowAVPanel.Controls.Add(panel)


            Dim tb As New TextBox
            With tb
                .Multiline = True
                .Size = New Size(200, 40)
                .Location = New Point(0, 0)
                .TextAlign = HorizontalAlignment.Center
                .BackColor = Color.SeaGreen

                .Font = New Font(tb.Font.FontFamily, 20)
                .Enabled = False
                .Text = "ABC"
            End With
msgbox(tb.location)'this one at 0,0
            panel.Controls.Add(tb)
msgbox(tb.location)'this one at 0,-87
it doesnt happen all the time, just sometime. Thats why its weird. What happen?
 
I can only guess that it has something to do with the fact that the Panel you're adding it to is itself within a FlowLayoutPanel. I've never seen that happen myself but I've also never used a FLP.
 
Yes In fact it is inside a flowlayoutpanel. I have a loop that create lots of panel and groupbox in the flp. If I create 100 controls, it will happen maybe half way thru it and continue onward. I had to correct the location after adding the control.
 
Seems odd and might be a bug but the workaround in this case is easy: just move the line that sets the Location to after the Add call. By the way, it is preferred to use Point.Empty rather than creating a new Point(0, 0).
 
Back
Top