1

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?

Rien
  • 398
  • 2
  • 12
  • 37
  • @BillWoodger I did now find out why it only shows a part of the value, its because its size is not big enough. Now the question, how do i set the size to be just as large as the text? im editing my question – Rien Apr 04 '15 at 13:54
  • Is `finalResult_panel` wide enough to show the full Text? – TaW Apr 04 '15 at 14:01
  • @TaW yes it can easily hold the text. When I set a label size in the loop it displays it without problem, but I can not do that since i do not know how long each label is. – Rien Apr 04 '15 at 14:02
  • 1
    As long as you keep `Label.AutoSize = true`, which is the default it should size itself automatically. You may want to do some tests, like setting a border or a BackColor to see the actualy dimension or inserting a space in the Text, so it gets a chance to go to a second line.. - Of course you __could mesaure__ the Text (with e.g. `TextRenderer.MeasureText(text, Label.Font);` but that __will certainly not be necessary__ for the text simply to show... – TaW Apr 04 '15 at 14:05
  • @TaW That did the trick.. for some odd reason i thought that autoSize only applied for scrollbars etc... feeling very dumb now. Could you post this as a answer so i can mark the question as solved? Thank you for helping! – Rien Apr 04 '15 at 14:11

1 Answers1

3

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..

TaW
  • 53,122
  • 8
  • 69
  • 111