Text wrap C#

dosser
dosser used Ask the Experts™
on
Hi,

I have a label on a form that has a fixed pixel width and height. The text for that label could be a small one word response, or it could be several lines. What I want to do is check the length of the response, if needs be split it onto two lines (without cutting off the ends of words, so probably using a regex ' ',',' , ':' or something like that).  

I initially tried wrapping the text based on number of characters, which does work. The problem is, even though the following two lines are the same number of characters -

Example 1 - llllllllllllllllllllllllllllll
Example 2 - wwwwwwwwwwwwwwwwwwwwwwwwwwwwww

there is obviously a huge difference in the pixel width. (This is an extreme case but you get my point). The answer is probably TextRenderer.MeasureText but I can't work out how to a) split it without cutting off the ends of words, and b) ensure it goes no further than two lines?

I thought about putting every character into an array and looping through measuring the pixel width, but surely there must be a better way?

Any help would be appreciated!

Thanks in advance

CD
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Todd GerbertIT Consultant
Top Expert 2010

Commented:
If the label control's height is sufficient, it will automatically wrap text to a second line.  Would that not work for you?  Then you could check if the text is wider than the label, double the label control's height, and assign the .Text property.
Todd GerbertIT Consultant
Top Expert 2010

Commented:
So, for example, here are two screen shots of what I'm talking about.  I set the back color of the label so you can see it's size.  The button's click event is below.  With a short string of text it just fits, but with longer text the label's height is adjusted to fit two lines of text and it's wrapped.
private void button1_Click(object sender, EventArgs e)
{
	if (TextRenderer.MeasureText(textBox1.Text, label1.Font).Width > label1.Width)
		label1.Height *= 2;
	label1.Text = textBox1.Text;
}

Open in new window

Untitled.png
Untitled2.png
IT Consultant
Top Expert 2010
Commented:
...notice that the width doesn't change, and their will never be more than two lines of text in the label.  If the text is too long to fit even after being wrapped, it will just get cut off or the label will insert "..." if AutoEllipsis is true.

The alternative, if you wish to do it manually, is to measure each bit of the string manually.  I would split it into words, but that may not work in 100% of the cases (i.e. if the first word is "Supercalifragilisticexpialidocious").
private void button1_Click(object sender, EventArgs e)
{
	if (TextRenderer.MeasureText(textBox1.Text, label1.Font).Width < label1.Width)
		label1.Text = textBox1.Text;
	else
	{
		string[] words = textBox1.Text.Split();
		for (int i = 0; i < words.Length; i++)
		{
			if (TextRenderer.MeasureText(String.Join(" ", words, 0, i), label1.Font).Width > label1.Width)
			{
				label1.Text = String.Join(" ", words, 0, i--);
				label2.Text = String.Join(" ", words, i, words.Length - i);
				break;
			}
		}
	}
}

Open in new window

Untitled.png
Untitled2.png

Author

Commented:
Thanks very much.

CD

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial