C# Windows Forms glitch while creating a control

  anonymous, c++, forms, object, windows

I somehow noticed, that while I’m creating a control in my application, it first appears as weird rectangle and then it "shrinks" to it’s correct form.

First thing you’ll see after creation

Correct created object

My question is, why I first see the rectangle in top left corner and then it suddenly changes to it’s correct form. It’s like split second thing (I had to use Thread.Sleep ‘cos it’s impossible to screenshot it), but my eyes can still see this and I’m really triggered when it’s happening.

This is the code, where I’m creating the control:

var label = new Label
{
    AutoSize = true,
    TextAlign = ContentAlignment.MiddleLeft,
    Font = new Font("Courier New", 9F, FontStyle.Regular,
        GraphicsUnit.Point, 238),
    Text = keyword,
    Margin = new Padding(0, 6, 25, 3),
    Padding = new Padding(0, 3, 0, 0)
};

var button = new Button
{
    BackgroundImage = Image.FromFile("../../../Images/cross_200x200.png"),
    BackgroundImageLayout = ImageLayout.Stretch,
    Dock = DockStyle.Right,
    Width = 21,
    Height = 21,
    FlatStyle = FlatStyle.Flat,
};
button.FlatAppearance.BorderSize = 0;

var pan = new Panel
{
    AutoSize = true,
    Padding = new Padding(0, 0, 1, 1),
    BackColor = Color.PowderBlue,
    BorderStyle = BorderStyle.FixedSingle,
    Tag = keyword
};

button.Click += delegate
{
    _keywords.Remove(pan.Controls.OfType<Label>().First().Text);
    pan.Dispose();
    StatusLabel.Text = [email protected]"Removed {keyword}";
};

pan.Controls.Add(label);
pan.Controls.Add(button);

FlowLayoutPanelMain.Controls.Add(pan);

Everytime a "keyword" is added to FlowLayoutPanel control, at first it’s a rectangle in top left corner and immediately after that it’s fine.

Source: StackOverflow

LEAVE A COMMENT