Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms: add new line to label if text is too long

Tags:

c#

winforms

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

like image 767
Barryman9000 Avatar asked Sep 09 '10 20:09

Barryman9000


People also ask

How do I add a line break in Label text?

Text = sText; lblDescription. Location = new Point(iX1, iY + 5);

How do I change the Label size in Windows Forms?

Step 2: Drag the Label control from the ToolBox and drop it on the windows form. You are allowed to place a Label control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the Label control to set the Size property of the Label.

How do I change text from labels in Windows Forms?

Windows. Forms namespace. Add a Label control to the form - Click Label in the Toolbox and drag it over the forms Designer and drop it in the desired location. If you want to change the display text of the Label, you have to set a new text to the Text property of Label.

How do you Label multiple lines in C#?

We can also use a Panel control to create a multiline label in C#. We can place the desired label inside a panel and then handle the ClientSizeChanged event for the panel. The ClientSizeChanged event is invoked whenever the size of a control inside the panel changes. We can resize the label with the Label.


1 Answers

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.

like image 192
John Gietzen Avatar answered Oct 14 '22 14:10

John Gietzen