2

In my C#4.0 Windows Forms Based Application, i having one Label box.Now i want to dynamically set the text of the label box.My text looks like paragraph's.So, Whenever i displayed my text, only part of the text are shown in the form with one line.So, I want to wrap the text to next line whenever it reaches the forms right end...

how i do it.Please Guide me to get out of this issue...

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Saravanan
  • 11,372
  • 43
  • 143
  • 213

3 Answers3

5

All you need to do is set the label control's AutoSize property to False. You can do this either in the designer via the Properties window, or through code: myLabel.AutoSize = false

It is turned on by default when you add the control in the designer (though the default value is false when you instantiate the control through code). With this property on, the control tries to automatically adjust its width (and not its height!) to display its entire contents. This doesn't work out so well with multiple paragraphs, as it's impossible to fit the entire contents in a single line on the screen.

By turning this property off, you can manually resize the control to accommodate your text.

Everything else is handled automatically. The text will automatically wrap to a new line when it reaches the edge of the control's border. For example:

   The asker's original question, displayed on a label control with AutoSize = false.

If you don't want to manually set the size of the label control, you can take advantage of the Dock and Anchor properties, which will automatically resize the control to its parent container. This is convenient, say, if you want the label to fill the entire form or panel that you've placed it inside.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
3

Make sure that you set label.AutoSize = true; and set a fixed with for your label. It will automatically word-wrap for you:

label.MaximumSize = new Size(100, 0);

This is the easiest solution that I know. Just tested it and it works.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
1

In the Label's properties, unset the AutoSize property (set to False), then anchor the Label as required. This should have the effect you're looking for.

Alex J
  • 9,905
  • 6
  • 36
  • 46