Rien
Rien

Reputation: 458

c# dynamically created label size not wide enough

When dynamically creating a label a part of it's text is missing. This is because the size of the label is not wide enough, it cuts of a part of the the actual string.

How do i make that simply stop? I dont want to nor do i remember setting a size to the label. Should it not just continue untill the string is empty?

Example: value = "Berserker's Iron Axe", it only displays "Berserker's Iron", because i have no size set it cust off a part of the string.

    PictureBox finalResult_pBox = new PictureBox {
                                    Name = "finalResult_pBox" + i,
                                    Size = new Size(64, 64),
                                    Padding = new Padding(0),
                                    BorderStyle = BorderStyle.FixedSingle,

                                    ImageLocation = spidyApi_idByName_result.results[i].img.ToString(),
                                };
                              MessageBox.Show(spidyApi_idByName_result.results[i].name.ToString());

                                Label finalResult_itemName_label = new Label{
                                    Name = "finalResult_itemName_label" + i,
                                    Text = spidyApi_idByName_result.results[i].name,
                                };

                                FlowLayoutPanel finalResult_panel = new FlowLayoutPanel{
                                    FlowDirection = FlowDirection.TopDown,
                                    BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle,
                                    Name = "result_flowLayoutPanel" + i,
                                    Size = new System.Drawing.Size(790, 64),
                                    TabIndex = i,
                                };



finalResult_panel.Controls.Add(finalResult_pBox);
finalResult_panel.Controls.Add(finalResult_itemName_label);
result_flowLayoutPanel.Controls.Add(finalResult_panel);

Why is this happening and how do i fix this?

Upvotes: 1

Views: 1845

Answers (1)

TaW
TaW

Reputation: 54433

As long as you keep Label.AutoSize = true, which is the default it should size itself automatically.

You may want to do some tests to see the actual dimensions, like

  • setting a BorderStyle
  • or a BackColor
  • or inserting a space in the Text, so it gets a chance to go to a second line..

  • Of course you could measure the Text (with e.g. TextRenderer.MeasureText(text, Label.Font); but that will certainly not be necessary for the text simply to show.. It is useful for more demanding layout control, though.

So the first thing is to make sure that AutoSize is either true or that you measure and set the Size of the Label..

Upvotes: 3

Related Questions