word wrap - How to detect a string-overflow from a line in C#? -


i wanted know how word-wrap in c# when came across solution problem on word-wrapping text in line in url. unfortunately, not have enough reputations ask op directly one specific problem having(and people dealing having indefinitely)

problem description:
can word-wrap string if needs word-wrapped code:

graphics paddress = e.graphics;  sizef paddresslength = paddress.measurestring("residential address: " + raddresstextbox.text, new font(fontface, fontsize, fontstyle.regular),700);  paddress.drawstring("residential address: "+paddresslength + raddresstextbox.text, new font(fontface, fontsize, fontstyle.regular), brushes.black, new rectanglef(new point(pagemarginx, newline()),paddresslength),stringformat.generictypographic); 

however, not find place receive trigger whenever word-length overflows single line.

for example:
in line-2 of code, whenever wordlength exceeds 700px, moves next line. following rectanglef wordwrap. doing automatically, problem since makes difficult know whether has crossed 700px or not.

this format in information displayed whenever tried print paddresslength:

{width=633.1881, height=47.14897}

i thinking if can extract value of width using paddresslength.width ,then can partially solve problem. that, need calculate if remaining space(i.e 700px - 633.1881px ) accommodate next word or not(if there one)

breaking down problem:

  • i already know how word-wrap when there string longer specify using graphics.measurestring given in this solution in question.
  • but that^ process happens automatically, want know how detect if word-wrap has occured(and how may lines has wrapped each line being 700px width maximum)
  • i need know number of lines have been wrapped in order know number of times execute newline() function wrote, gives appropriate line spacing upon executing each time.

additionally, (bonus question; may or maynot solve) there way extract value 633.1881 , calculate whether next word fits in ( 700 - 633.1881 )px space or not?

there overload measurestring returns number of lines used in out parameter: https://msdn.microsoft.com/en-us/library/957webty%28v=vs.110%29.aspx


Comments