Is there a wrapping routine in the .NET Framework??

  • Thread starter Thread starter Siv
  • Start date Start date
S

Siv

Hi,
I am trying to find a wrapping routine for text and not having much luck.
The reason I need one is that I am producing a report where the text that
was stored in the database was written into a multi-line text box, so it
contains long strings of text where the user allowed the wrapping of the
text box to wrap the text within its confines and here and there, they will
have entered CRLFs where they have pressed ENTER to push in a break between
paragraphs. This has always caused me problems when writing this stuff out
to a report as I have a certain width on the printed page and usually a
certain amount of depth. There must be a .NET framework function that can
process a piece of text and insert the relevant breaks that doesn't split
words and constrains it within a given rectangle that you specify.

Can anyone help me as I can't find anything??
 
Cor,

I think you may have misunderstood what I am after. I am writing a report
that the user can priny out. The data that I am trying to display in the
report is made up of long strings of text. I then have to write that out to
a printed report but I have to fit it into a rectangular box on that report
that has a certain width and depth.

My problem comes in trying to get that text wrapped to fit the report layout
without losing any of the text or breaking words up as well as honouring any
carriage returns that were entered in the original piece of text.

As a lot of controls like the rich text box and the multi-line text box
intelligently wrap text as you type, there must be a routine (presumably
part of the Dot Net Framwork) that these controls use and I was hopeful I
could use that in my printing routine.

I have tried to roll my own and got a fairly good routine but it seems a bit
sluggish and it sometimes gets confused stuck in an iterative loop that I
haven't been able to fix yet, due to deadlines I decided I would see if
there was a built in function that does the same job, returning the text
formatted with line breaks that allow it to fit on the report within the
specified widths.


--
Siv
Martley, Near Worcester, United Kingdom.


Cor Ligthert said:
Siv,

You don't get word for free, but what you ask looks for me the most at the
RichTextBox.

http://msdn.microsoft.com/library/d...rfsystemwindowsformsrichtextboxclasstopic.asp

I hope this helps,

Cor
 
Cerebrus,

I am pleased to say that you are wrong (I don't mean that in a nasty way),
after hunting around furiously in the MSDN documentation I found the
answer!! Hurrah. The routine I currently use to output the text actually
does it, I just hadn't looked at the overloaded versions of the drawstring
method.

The code that does the magic is:

Dim rect As System.Drawing.RectangleF
Dim str as String

Str = sp.TaskMMDetails 'A piece of text from a class with lots of text
in it with breaks etc.
rect.X = LM + 100 'LM is a var holding my left margin so my
rectangle
rect.Y = y + (n * 100) 'y is a var tracking where I am down the
document incremented by lineheight
rect.Width = LM + 650 'width of my reports rectangle
rect.Height = 100 'Height of my reports rectangle

'Then write it out
e.Graphics.DrawString(Str, fb, Brushes.Black, rect)

The text is output to the report and is wrapped within the rectangle
specified by rect, it works brilliantly.

Additionally there is an additional parameter which deals with string
fromatting which I haven't unravelled yet that allows you to do stuff like
centre align right align etc also there are switches that allow you to
determine whether you want the text that would be cut off by the bottom of
the rectangle to appear or not and also whether it would insrt an elipsis at
the botytom right corner of the rectangle followed by the last word in the
text.

BRILLIANT! Cheers MS it was right under my nose all the time. Teach me to
explore all the overloaded versions.

For anyone interested the article that led me to this on MSDN is as follows:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/printwinforms.asp

Thanks to Cor for putting me in the MSDN library which prompted me to look a
bit harder and find the answer there.
Thanks to Cerberus for responding, I was getting a bit despondent as a
couple of my questions here earlier seemed to be sitting unanswered.
 
Siv said:
BRILLIANT! Cheers MS it was right under my nose all the time. Teach me to
explore all the overloaded versions.

I've noticed that the documentation in MSDN for VS 2005 is vastly
improved for this sort of thing wrt what came before.

:-)

Yeah, MS, that was a compliment! It's good to see a steady improvement
and the new docs now lack only an index into fundamentals documents. Or
the fundamentals documents themselves.

Dr. Gui ought to return with a screaming ten-million-dollar-a-year
vengeance, until the core technologies of the 2.0 Framework are fully
explained.

Rob
 
Rob,

I must confess to being a bit useless with search help, I can search for
hours and not find what I want and other people just seem to instinctively
know what to enter in the search engine to get what they want. It was the
same in exams as a kid, all the kids would read the question and get the
correct meaning, I would read it and assume it was a lot more complicated
than it was. I think my brain must be in backwards?
 
Siv said:
I am trying to find a wrapping routine for text and not having much luck.
The reason I need one is that I am producing a report where the text that
was stored in the database was written into a multi-line text box, so it
contains long strings of text where the user allowed the wrapping of the
text box to wrap the text within its confines and here and there, they
will have entered CRLFs where they have pressed ENTER to push in a break
between paragraphs. This has always caused me problems when writing this
stuff out to a report as I have a certain width on the printed page and
usually a certain amount of depth. There must be a .NET framework
function that can process a piece of text and insert the relevant breaks
that doesn't split words and constrains it within a given rectangle that
you specify.

Check out the overloads of 'Graphics.DrawString' if you want to draw text
with word wrapping turned on.

Additional topics:

Determining the lines as they are being displayed in a textbox control
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=textboxdisplayedlines&lang=en>
 
Herfried,
Thanks, I already found this myself if you check out the other replies to
this question, but thanks anyway as I might not have.
 
Hi Siv,

Thank you very much for that correction. I was thinking in a linear
fashion only(String class members only). A good reminder to me, to
expand my horizon and to "Never say die!".

I'm very glad you found the answer. I will keep it in mind, in case I
need it in the future.

Regards,

Cerebrus.
 
Back
Top