Word Wrap for a Label in Windows Forms

Word wrap for a label in Windows Forms

The quick answer: switch off AutoSize.

The big problem here is that the label will not change its height automatically (only width). To get this right you will need to subclass the label and include vertical resize logic.

Basically what you need to do in OnPaint is:

  1. Measure the height of the text (Graphics.MeasureString).
  2. If the label height is not equal to the height of the text set the height and return.
  3. Draw the text.

You will also need to set the ResizeRedraw style flag in the constructor.

Label word wrapping

Refer to Automatically Wrap Text in Label. It describes how to create your own growing label.

Here is the full source taken from the above reference:

using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

public class GrowLabel : Label {
private bool mGrowing;
public GrowLabel() {
this.AutoSize = false;
}
private void resizeLabel() {
if (mGrowing) return;
try {
mGrowing = true;
Size sz = new Size(this.Width, Int32.MaxValue);
sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);
this.Height = sz.Height;
}
finally {
mGrowing = false;
}
}
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e);
resizeLabel();
}
protected override void OnFontChanged(EventArgs e) {
base.OnFontChanged(e);
resizeLabel();
}
protected override void OnSizeChanged(EventArgs e) {
base.OnSizeChanged(e);
resizeLabel();
}
}

Set WordWrap = false for a Label

I found a solution:

this.label.AutoEllipsis = true;
this.label.AutoSize = true;

In the panel's event handler for Resize:

...
textHeight = this.label.Font.SizeInPoints; // Take in pixels, not points
...
Size newMaxSize = new Size(this.Width,
textHeight + label.Padding.Top + label.Padding.Bottom);
this.label.MaximumSize = newMaxSize;
...

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

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.

Multiline text as the button label in Windows Forms

Set the label text on form load and add Environment.Newline as the newline string, like this:

btnOK.Text = "OK" + Environment.NewLine + "true";

How to set a text of label box dynamically in C#4.0 with word Wrap functionality?

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.

How can I make the text of checkbox wraps automatically with changing form width?

I tried many approaches but at last this one worked after a lot of researches:-

simply I used two events to detect size change of the panel contains the controls then I adjusted the controls accordingly..

first event is LayoutEventHandler,
second event for detecting resolution change

in these events:-

1- get the panel width considering the resolution (lower accepted resolution is 1024x768)

  Rectangle resolution = Screen.PrimaryScreen.Bounds;
int panelWidth *= (int)Math.Floor((double)resolution.Width / 1024);

2- loop on all controls and adjust the control width to fit the panel width (i subtracted 10 pixels for vertical scroll width), then I got the control height from MeasureString function which takes the control text, the font and control width, and return the control size.

(i.e. I multiplied the height with a roughly factor "1.25" to overcome line height and padding)

  foreach (var control in controls)
{
if (control is RadioButton || control is CheckBox)
{
control.Width = panelWidth - 10;

Font fontUsed = control.Font;
using (Graphics g = control.CreateGraphics())
{
SizeF size = g.MeasureString(control.Text, fontUsed, control.Width);
control.Height = (int)Math.Ceiling(size.Height * 1.25);
}
}
}

Dynamically change font of a label in windows forms

two things is important ,check that the font is installed on system and check that the name of the font is written correctly.

Sample Image

Prevent word wrap in label with AutoSize=false

The only way I can think to do it is to create a custom control that inherits Label and override the OnPaint method.

public class CustomLabel : Label
{
public CustomLabel()
{
}

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), 0, 0);
}
}

The downside is that if you want to take into account TextAlign, Padding and Margin you'll need to calculate it yourself in the paint method.



Related Topics



Leave a reply



Submit