2

I am trying to make a Label in C#, which would wrap text the way it would have a fixed width, and variable height. I am using this code:

Label newMsg = new Label();
newMsg.AutoSize = true;
newMsg.MaximumSize = new Size(350, 400); //setting fixed width, max height to avoid being too high for container
newMsg.MinimumSize = new Size(350, 0); //fixed width, height doesn't matter
newMsg.Text = msg;
newMsg.Location = new Point(4, 7);
newMsg.BackColor = Color.Red; //red to see the actual size in contrast to its container

but it doesn't work. I tried to do some research why, and regardless if the text is one word or a whole paragraph, the newMsg.Height is always 23. Does anyone know why? Thanks

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
Marek Buchtela
  • 973
  • 3
  • 19
  • 42
  • 2
    if you need custom size - > `newMsg.AutoSize = false;` – huMpty duMpty Jun 19 '14 at 10:39
  • I think it's the `autosize`. Maybe set to false? Anyway, the height may be wrapping around the text font size. **EDIT**: Let me see if I got it: you want the label height to grow if the text inside doesn't fit the width? Like multiline? I'm sorry, but the question isn't very clear – chiapa Jun 19 '14 at 10:39
  • Tried few more things and I discovered the problem is quite different that I thought, sorry for that. The label resizes correctly, it stretches and wraps the text to another lines. The problem is, that the Label.Height always returns 23, even when that is not true – Marek Buchtela Jun 19 '14 at 10:57

2 Answers2

2

Just like others have said already, change AutoSize property to false,

newMsg.AutoSize = false;
chiapa
  • 4,362
  • 11
  • 66
  • 106
Hjalmar Z
  • 1,591
  • 1
  • 18
  • 36
1

If the font is taller than the height of the Label and AutoEllipsis is true, you must set AutoSize to false for text to be drawn. from msdn

vjalex
  • 63
  • 2
  • 13