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

早过忘川 提交于 2019-12-08 05:13:31

问题


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:


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.



来源:https://stackoverflow.com/questions/3117766/how-to-do-text-formating-in-c-sharp-for-adjusting-text-in-some-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!