开发者

How to do text formating in C# for adjusting text in some control

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                                 |
-----------------------------------------------|


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜