Rectangularizing a string

  • Thread starter Thread starter Lance
  • Start date Start date
L

Lance

Forgive the goofy subject title, but I don't know what to
call this...

I want to insert NewLines into an arbitrary string so
that the string will fit within a reasonably-sized
rectangle. For example, if a string is a single line of
~300 characters, then I might want to break up the string
into 4 lines that are ~75 characters each. But, of
course you want to keep words in-tact.

Writing my own method wouldn't be too tough, but
does .NET have any tools for doing this type of thing?

Thanks,
Lance
 
Forgive the goofy subject title, but I don't know what to
call this...

I want to insert NewLines into an arbitrary string so
that the string will fit within a reasonably-sized
rectangle. For example, if a string is a single line of
~300 characters, then I might want to break up the string
into 4 lines that are ~75 characters each. But, of
course you want to keep words in-tact.

Writing my own method wouldn't be too tough, but
does .NET have any tools for doing this type of thing?

Thanks,
Lance

Lance,

System.Environment.NewLine comes to mind.

HTH,
Tom Shelton
 
Hi Lance,

There are two ways that I know to do it in .NET.

Firstly there is g.DrawString() which will draw the given text within a
given rectangle according to a set of flags. This will be done into the
Graphics object, g. You can then read the bitmap and use OCR (this'll have to
be a third party dll) to get the characters back out again.

Then there's the RichTextBox which you can set to a given width. Add your
text and turn word-wrap on. Then take the text out from the array of lines.

But, lol, I don't think you want to do either of these, somehow. ;-)

It's roll-your-own time.

Regards,
Fergus
 
Hi Lance,

It seems that .net doesn't provide such a function to split a string as you
wish. You have to write your own code to achieve this.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Content-Class: urn:content-classes:message
| From: "Lance" <[email protected]>
| Sender: "Lance" <[email protected]>
| Subject: Rectangularizing a string
| Date: Mon, 29 Sep 2003 20:23:18 -0700
| Lines: 15
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOHAjGtiGwAmtFPTDWIOdabOOsmOQ==
| Newsgroups: microsoft.public.dotnet.languages.vb
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:142410
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Forgive the goofy subject title, but I don't know what to
| call this...
|
| I want to insert NewLines into an arbitrary string so
| that the string will fit within a reasonably-sized
| rectangle. For example, if a string is a single line of
| ~300 characters, then I might want to break up the string
| into 4 lines that are ~75 characters each. But, of
| course you want to keep words in-tact.
|
| Writing my own method wouldn't be too tough, but
| does .NET have any tools for doing this type of thing?
|
| Thanks,
| Lance
|
 
Lance said:
I want to insert NewLines into an arbitrary string so
that the string will fit within a reasonably-sized
rectangle. For example, if a string is a single line of
~300 characters, then I might want to break up the string
into 4 lines that are ~75 characters each. But, of
course you want to keep words in-tact.

Writing my own method wouldn't be too tough, but
does .NET have any tools for doing this type of thing?

Do you want to breakt the lines after a certain number of characters or
a certain width (for example, in pixels)?
 
The most common implementation would be when displaying a
Tooltip. When displaying a tooltip with a long single-
line string the tooltip ends up being this long, narrow
box that streches across the sceen. This can look pretty
bad, especially if the corresponding control is near the
right-edge of the screen. A similar problem can occur
with a MessageBox.
 
Lance said:
The most common implementation would be when displaying a
Tooltip. When displaying a tooltip with a long single-
line string the tooltip ends up being this long, narrow
box that streches across the sceen. This can look pretty
bad, especially if the corresponding control is near the
right-edge of the screen. A similar problem can occur
with a MessageBox.

If you draw the text using 'DrawString', you can specify a bounding rectangle.
 
Hi Lance,

Despite me joking, as you are doing this in terms of display (ToolTip and
MessageBox) rather than text manipulation, DrawString <is> the way to go (as
every one else is affirming!).

Overloads Public Sub DrawString( _
ByVal s As String, _
ByVal font As Font, _
ByVal brush As Brush, _
ByVal layoutRectangle As RectangleF, _
ByVal format As StringFormat _
)

Regards,
Fergus
 
I utilize the auto-wrapping, string-measuring, text-
painting stuff in the Drawing namespace quite a bit in my
app, but I don't see how DrawString is useful when
working with Tooltips and MessageBoxes. Am I missing
something?

Note that (in this case) I'm trying to modify a string so
that it looks nice when displayed in a Tooltip or a
MessageBox. I'm NOT overriding OnPaint or anything like
that.

Thanks again.
Lance
 
Lance said:
How can I use DrawString with a Tooltip or a MessageBox?

I don't really understand what you want to do. Do you want to create
your own tooltip or messagebox?
 
Hi Lance,

At first I thought (correctly) that you wanted a text routine - hence the
jokey solutions. Then your replies led me to think that you were doing your
own tooltips/message boxes and that misunderstanding led to mention DrawString
as a serious option. Oops, sorry! ;-)

There's nothing in .NET that I am aware of (as a demi-semi-demi expert),
but, as you say, it's not that hard to jump by n characters and backtrack to
the previous word-break. It's a shame we all got the wrong end of the stick -
you'd have had it writtten by now!

Good luck, anyway.

Regards,
Fergus
 
Back
Top