views:

43

answers:

1

I have created a user control on winform where i want to display some text on Label Control at runtime. Here I used textFormatFlag as WordBreak and then displays it in the next line. Size of label is fixed in width while variable in height. Now the problem here is how to break a string if there is no space between i.e. no WordBreak Present in the string. I dont want to do calculations on font size and accordingly modify string. Is it something hidden in .net that can do this work for me.

    sizeCategory = TextRenderer.MeasureText(Source["Parent_Name"].ToString()
                  , lbldbCategory.Font
                  , sizeCategory
                  , TextFormatFlags.WordBreak);

Consider lines as a boundary of my label. width height. Height being variable. Consider this eg.

My text string being

"salkdjasldjkslakdjlsakjdlsakjdkajhk sdjahksajd" //see word break between the string
-----------------------------------------------|
salkdjasldjkslakdjlsakjdlsakjdkajhk            |
sdjahksajd                                     |
-----------------------------------------------|

What I did and currently getting is this.

due to word break

-----------------------------------------------|
salkdjasldjkslakdjlsakjdlsakjd                 |
kajhksdjahksajdh                               | 
-----------------------------------------------|

What is happening is when string has no space i.e. no word break

-----------------------------------------------|
salkdjasldjkslakdjlsakjdlsakjdkajhksdjahksajdhasdasdasdsadasd|
-----------------------------------------------|

What I want is

-----------------------------------------------|
salkdjasldjkslakdjlsakjdlsakjdkajhksdjahksajdha|
sdasdasdsadasd                                 |
-----------------------------------------------|
+1  A: 

Since you are using a label control, leave AutoSize set to true, and set the MaximumSize property to something like 150, 0.

Just tested it. It works.

John Gietzen
@John: I didn't find any MaxSize Property, I cannot do it autosize otherwise it will start growing horizontally which i can't. I am free to make it growable in height only
Shantanu Gupta
My fault, it is `MaximumSize`. And, if you do set the max width, it will not grow horizontally.
John Gietzen
@John: Will it also work if i set autosize to false
Shantanu Gupta
@John: You made my work sudden easy. I did all the work from my calculations and all that's too without MaximumSize in 3-4 days. Now i m changing it in to be done with this property and did 60% in less than 30 min. 100 upvote worthfull thanks to you
Shantanu Gupta