473,800 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DrawString, multiline string but full width

Hello.
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
For example I'm using a fixed-width font, and the width of the layout
rectangle is that of 12 characters, the result I want is,
This is a sa
mple text.
not,
This is a
sample text.

I set the StringFormatFla gs.Wrap = false, but it disabled multiline
drawing that wasn't what I wanted.

Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?
Jun 27 '08 #1
4 6174
On Fri, 25 Apr 2008 21:51:04 -0700, Sin Jeong-hun <ty*******@gmai l.com>
wrote:
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
[...]
Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?
Off the top of my head, I think maybe you want to set the
StringFormat.Tr imming property to Character.

If I'm mistaken about that, you may be able to use the TextRenderer class
instead to do your drawing, using the TextFormatFlags .WordBreak flag (or
rather, making sure that flag is _not_ set) to have the text wrapped on
whatever character reaches the very end of the line.

Sorry I'm being so vague...I don't have something right in front of me
that would let me test the code to make sure my recollection is right.

Pete
Jun 27 '08 #2
On Apr 26, 3:29*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Fri, 25 Apr 2008 21:51:04 -0700, Sin Jeong-hun <typing...@gmai l.com*
wrote:
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
[...]
Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?

Off the top of my head, I think maybe you want to set the *
StringFormat.Tr imming property to Character.

If I'm mistaken about that, you may be able to use the TextRenderer class *
instead to do your drawing, using the TextFormatFlags .WordBreak flag (or *
rather, making sure that flag is _not_ set) to have the text wrapped on *
whatever character reaches the very end of the line.

Sorry I'm being so vague...I don't have something right in front of me *
that would let me test the code to make sure my recollection is right.

Pete
Thank you for your reply. But, DrawString() with Trimming property set
to Character didn't work. It was the same as when I didn't even set
the Trimming property. Secondly, TextRender.Draw Text draws multiline
text if and only if the TextFormatFlag has WordBreak. Setting
WorkBreak gave the same result as when I used DrawString().
Jun 27 '08 #3
On Apr 26, 5:27*pm, Sin Jeong-hun <typing...@gmai l.comwrote:
On Apr 26, 3:29*pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Fri, 25 Apr 2008 21:51:04 -0700, Sin Jeong-hun <typing...@gmai l.com*
wrote:
It seems like the default behavior of DrawString is trying to keep the
whole word if possible. I'd like the string to be drawn in multilines,
but filling the entire line width.
[...]
Is what I'm trying to do achievable with any formatting options or do
I have to draw each character manually with my own calculation?
Off the top of my head, I think maybe you want to set the *
StringFormat.Tr imming property to Character.
If I'm mistaken about that, you may be able to use the TextRenderer class *
instead to do your drawing, using the TextFormatFlags .WordBreak flag (or*
rather, making sure that flag is _not_ set) to have the text wrapped on *
whatever character reaches the very end of the line.
Sorry I'm being so vague...I don't have something right in front of me *
that would let me test the code to make sure my recollection is right.
Pete

Thank you for your reply. But, DrawString() with Trimming property set
to Character didn't work. It was the same as when I didn't even set
the Trimming property. Secondly, TextRender.Draw Text draws multiline
text if and only if the TextFormatFlag has WordBreak. Setting
WorkBreak gave the same result as when I used DrawString().
Please just let me know if what I'm trying to do cannot be achieved
with some flags. Then I can start writing manual line breaking codes.
Thank you.

Jun 27 '08 #4
On Sat, 26 Apr 2008 18:29:12 -0700, Sin Jeong-hun <ty*******@gmai l.com>
wrote:
Please just let me know if what I'm trying to do cannot be achieved
with some flags. Then I can start writing manual line breaking codes.
For what it's worth, it's very difficult with many esoteric operations for
a person to say for sure that you can't do it easily in .NET. .NET is
incredibly broad in many areas. If a person knows for sure that there
_is_ a way to do it, that's one thing. But to prove that you can't is
much harder.

In other words, you may not find anyone who is confident that their
knowledge is encyclopedic enough about .NET to state for sure that there's
no option that will break a line within a word rather than between words.
And this may be true even if it turns out that you actually can't.

I'm actually surprised that neither of the options I mentioned address the
issue. Just as I can't say for sure that what you want isn't in .NET, I
also can't say for sure that you have successfully applied the options I
mentioned in a way that would perform as you want. It's possible that
they only work in that specific way when used in combination with other
aspects of text formatting and that you haven't tried that particular
combination yet.

But, the fact is that Graphics.DrawSt ring() and TextRenderer.Dr awText()
are the two primary ways to draw text in .NET. If you're sure you've
explored all of the viable combinations of settings and overloads for each
of those methods and none do what you want, I suppose you might as well
start working on a more explicit implementation instead.

Pete
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14874
by: Mark Keats | last post by:
Hi there, I have some code for an ASP.Net page that produces an image of an e-mail address to prevent those programs that spider the net for e-mail addresses on pages grabbing it. The code is as follows (but on my version it gets the e-mail address from a database rather than QueryString). How can I crop the image to the exact size of the text as if the e-mail
1
6071
by: timtos | last post by:
I am working on a custom treeView and have some problems with a) node.Bounds.Width b) concatenate strings drawn with DrawString(...) a) I want to highlight a selected node and used the node.Bounds dimensions to draw a rectangle around the node but the node.Bounds.Width property seems to be wrong. Sometimes the rectangle is too large and sometimes it is too small; something like this
1
8854
by: Drew | last post by:
I posted this to framework.drawing, but did not get any responses... Maybe here?? I am drawing some text using DrawString() and using a bounding rectangle so that when the string is too long it wraps automatically. I want to decrease the amount of space between the lines. How would I go about changing the LineSpacing? The FontFamily has a read only property. Any ideas?
1
9367
by: John | last post by:
I'm trying to use the DrawText() method to draw some very long string text on the Panel with AutoScroll enabled. However, for some unknown reasons, I could not trigger the ScrollBar to show up. Here is the simplicied section of drawing code: private void panel_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { panel.AutoScroll = true;
3
2054
by: Jonah Olsson | last post by:
Hi guys! I'm trying to use Swedish characters with DrawString but no luck. Do I need to set the Charset somewhere?? I've been searching Google Groups for hours now, but I can't find any help or tutorial. Below is my code. Thanks for any kind of help! Regards, Jonah Olsson
7
4052
by: Joel Finkel | last post by:
Folks, I have a form that has several TextBoxes, some of which have the TextMode set to MultiLine. Each is pre-loaded with data from a database. The user is allowed to modify each entry. The problem is that only the modified data from the SingleLine TextBoxes are returned. The original data are returned for the MultiLine TextBoxes. Also, if the page is sent to the server (for validation, for instance), only the modified data for the...
1
267
by: johnb41 | last post by:
I need to print out a string of text and obviously i'm using the DrawString command. But the string must be placed AFTER some "programmatically generated text" (also printed using DrawString). That text would range from a single line, to maybe up to 10 lines. So i cannot hard code the position for the Drawstring command... it must come AFTER the generated text, where ever that might be. Is there any way to determine the position...
2
2544
by: ChrisNightingale | last post by:
Hi everybody, I have an odd issue which I'm not sure how to resolve. I'm basically implementing a print mechanism which takes a series of controls and reproduces them on a print document. So what I'm trying to do is print out a label so what I basically did is use Graphics.DrawString passing in the text of the label, the font of the label and the rectangle which represents the label (because it's set to autosize this should be the right...
6
2683
by: Dilip | last post by:
Howdy Folks I have a display where the Graphics.DrawString function is called to display something. Since the text seems to be larger than its bounding rectangle, the call basically splits the string into multiple lines. Is there a way to find out into how many lines the string was split? I am asking because this string is being displayed inside an owner
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.