16

I'm using C#. Sometimes the text returned from a web service (which I display in a label) is too long and gets cut off on the edge of the form. Is there an easy way to add a newline to the label if it's not going to fit on the form?

Thanks

kbrimington
  • 25,142
  • 5
  • 62
  • 74
Duderino9000
  • 2,535
  • 4
  • 31
  • 37
  • Closely related to http://stackoverflow.com/questions/1204804/word-wrap-for-label-in-winforms. – kbrimington Sep 09 '10 at 20:59
  • 1
    possible duplicate of [How to do text formating in C# for adjusting text in some control.](http://stackoverflow.com/questions/3117766/how-to-do-text-formating-in-c-for-adjusting-text-in-some-control) – John Gietzen Sep 09 '10 at 21:10
  • Yeah... the "text formatting in C# for adjusting text" post did it. I didn't see that when I searched around. Thanks. – Duderino9000 Sep 09 '10 at 21:52

2 Answers2

43

If you set the label to autosize, it will automatically grow with whatever text you put in it.

In order to make it word wrap at a particular width, you can set the MaximumSize property.

myLabel.MaximumSize = new Size(100, 0);
myLabel.AutoSize = true;

Tested and works.

If you always want to be able to see the data, you can set the Label's container's AutoScroll property to true.

Community
  • 1
  • 1
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
0

If the label control fails you, you can use a scrollable textbox instead.

Beth
  • 9,531
  • 1
  • 24
  • 43