Click here to Skip to main content
16,018,534 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,
Let suppose I've a line i.e
"Thisssssssssssssssssss Isssssssss Loooooooooong Teeeeeeeeeeeeeeeeeeeeeeext"

I have to place this single line in many lines acc. to the width of the panel. That is when I write some random text given to a label.text (which is placed in panel). then after the size of panel remaining text will transfer to the next line and if remaining text is still larger than panel size than the text will again break and that remaining text will go to the 3rd line

Like:-
Thisssssssssssssssssss Isssssssss
Loooooooooong Teeeeeeeeeee
eeeeeeeeeeext

This is all in windows form.
Code and guidance is highly appreciated.
Posted
Updated 9-Aug-13 8:00am
v2
Comments
PIEBALDconsult 9-Aug-13 14:47pm    
WinForms? WebForms? Other? Version?
binadi007 9-Aug-13 15:15pm    
WinForms

 
Share this answer
 
Comments
Manas Bhardwaj 9-Aug-13 15:22pm    
+5 for your original answer :)
Maciej Los 10-Aug-13 7:47am    
Thank you, Manas ;)
This is not a simple task, not even slightly.

The major problem is that the default font for text boxes is a proportional font: the charcaters are not all the same width. "i" is narrower than "m" for example - this makes working out how many characters you can fit to a "line" much harder than you might think because the width of a word varies depending on what characters it is spelled with.

You can do it: look at System.Drawing.Graphics.MeasureString[^] and then work it out, but the simplest solution is to fill the panel with a docked textbox set to "Multiline" and WordWrap both set to "true" and let it sort it all out! :laugh:
 
Share this answer
 
Comments
binadi007 9-Aug-13 15:06pm    
Yes OriginalGriff u understand the problem.
Then tell me the code relating the first case of urs i.e acc. to MeasureString
As I used initially TextRenderer.MeasureText. But it can only help me to find the width of the string and from that i can able to break the width of the text acc. to width of the panel.
But still I m not able to break the text according to the width.
Can you help me in that
Maciej Los 9-Aug-13 15:08pm    
See my answer ;)
OriginalGriff 9-Aug-13 15:20pm    
The way to do it (if you really must do it yourself) is pretty simple in essence: break your line into words (a regex is best, as you want to handle hyphenated words as well as punctuation without spaces) then start "bolting" words back together and measure each resulting string against your available space. When it doesn't fit anymore then you went too far, so go back to the previous string and use that. Needless to say, it's not quite that simple (you need allow for the case when it's a single "word" that doesn't fit and break it char by char or your loop never exits). You can't just measure each word in isolation (unfortunately) because some words can have what is called "Pair Kerning" which means that they can be fitted more tightly together if the last char of one word slopes the same way as the first char of the next:
"DRAW A WISH" for example can be pair kerned to reduce the space width and make the whole string look tidier when viewed next to words where it doesn't happen. You probably don't notice Windows do it, but you would if it didn't! :laugh:

This is not a "quick" process - MeasureString is not instant - and it has to be done using the Graphics Context that you are drawing to or the measurements go off and it all falls apart. Which means pretty much in the Paint event, so large amounts of text may well slow your UI to a crawl.

If you can, go with the multiline text box - it works!
Maciej Los 9-Aug-13 15:12pm    
OriginalGriff, this is second (almost the same) OP's question about resizing controls. Please, see my solution. I used TextRenderer.MeasureText(c, f) instead of System.Drawing.Graphics.MeasureString ;)
You can do this by wrapping

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

Label1.MaximimSize = new Size(100, 0);
Label1.AutoSize = true;


And Make your container panel Auto Scroll property to true.
 
Share this answer
 
It is very simple pls follow this......
1.Take the panel from toolbox and place in window form....
2.Take the label from toolbox and place in panel.....
3.Go to label properties and set the AutoSize into false....
4.Enlarge the label size by using dragging....what you want size of label.......that is widht and height...............
5.write the formload event like this.....



label1.Width = panel1.Width;
6.you will success........try this
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900