I have an application where we replace place holding text with other text at run time.
While doing so I have to add character ellipses if the string goes beyond some predefined width.
So I do not have a DrawingContext available nor i have a Graphics.Measure available.
I used FormattedText but I was unable to extract the ellipted text.
I could never find the right way to use a formatted text like this.
Please help.
For WinForms, you can use the TextRenderer.MeasureText function,
and thanks to the comment from vcjones, using the method described at http://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/ for WPF.
Related
Hi I use the Free Text Box in my .net application.
I was wondering if there was a way to prevent someone typing one single huge line of text.
Can you set a wrap boundary on it?
I had a short read of the documentation, and I don't think you'll find a native way to do so.
I guess you could use the FreeTextBox.TextChanged event, and check every line in the FreeTextBox.Text property. Mind the fact that it is HTML, and you will probably have to parse it with another third party tool (doing it manually will only give you headaches).
If any of these lines has more characters than an arbitrary length you'll set, create a new line of text with the surplus character, and move the caret to this line.
I have a problem with the alignment of a few names (changed for this question).
with this code I display the names in a richtextbox where it should be in one row with the "Spalte:" after the Names...but it doesn't. Can anybody help me please?
(only the first for-loop is necessary for my question, the next one does another job)
I added a pic what it looks like and how it should be looking. I know I can do it like its in the commented area (doesn't work in every case), but I need to change the code for a few other things so this needs to work....
Here is what I have:
And here is what I want:
Thanks for helping me :D
You have four options:
Change the code for the RichTextBox to use a fixed-width font. Most fonts have variable widths for each character, meaning you can't line things up neatly based on spacing alone.
Use tabs instead of spaces for the layout (and make sure the tab size is large enough to account for variances in your text).
Use a grid control of some type (DataGrid, GridView, etc)
Use a custom control for each row with labels at specific places (and either calculate positions yourself to place them on the form or use something like a FlowLayoutPanel).
I have a problem with character spacing.
Basically I have something like this which comes from a txt file:
****************
*System Details*
****************
Looks nice and uniform, however, when I open have this go into a RichTextBox this happens:
Irregular character spacing example:
I've tried all different properties to try and stretch it, render it etc. but nothing works.
The data is coming in from code-behind OpenDialogBox that stores all the lines of the file in a string[]. A foreach loop then sends the lines into the RTB. (It needs to be a loop as each line gets checked)
Any help it greatly appreciated!
Many thanks
This is most likely a font choice problem. By default WPF uses Segoe UI on Windows 7 and above which is a non-monospaced font. This means that each character will not necessarily take up the same amount of space as each other character leading to issues if you are trying to align characters between lines. The easiest way to get alignment to work is by changing the font to a monospaced font by setting the FontFamily property on the RichTextBox.
Question A.
Given
A string in rich text format that may have paragraph, tabs, space, line break, indentation, (or even image?)
A width for the word wrapping rich text control/editor
How do I know the height of the content after it have performed all the word wrapping?
Is there something like
int MeasureRichTextHeightAfterWordWrap(string aRichTextContent, int aWidth)?
Otherwise how does those rich text control know how much to autosize?
Do I have to actually place the content on a dummy rich text control, set it's width and get its height with GetPositionFromCharIndex(TextLength-1) afterwards?
Although this does work, it seems to be "wasteful"
Question B.
If I draw plain text onto a plain text memo/control/editor,
and manually draw string with manually calculated indentations, breaks, word wrappings to pretend the RTF.
Is it easier or harder?
Edited to make it look clearer
and sorry if my English looks like a student cause it's not my native language.
Sorry about my bad first attempt at an answer. I DID find an answer for measuring inside a RichTextBox. Apparently you have to use Win32 GDI API calls.
http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.windowsforms.controls/2004-09/0574.html
I found this by changing my search after running across this nugget, which explains why there's not a pure .NET way to do it:
http://www.developmentnow.com/g/38_2005_10_0_0_626243/I-dont-believe-this-code-gives-the-correct-RichTextBox-string-size.htm
I arrive to output a string on multiple lines inside a retangle but haven't find a way to reduce or enlarge the line spacing.
How to do that?
This MSDN should help you. Line spacing is a result of the Font you are using. You may need to break your DrawString commands up into multiple calls if you need custom line spacing.
This Microsoft forum posting may be helpful:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1507414&SiteID=1
This shows how MeasureString can be used to determine how much of your text will fit on each line, then using this to progressively render the entire rectangle's contents line by line. Unfortunately I don't think there's a built-in line spacing property, so you'll have to go for the manual approach. The post's author uses the font's Height * 1.5.
It's also worth researching StringFormatFlags - you'll need to make sure both your DrawString and MeasureString calls use the same StringFormat so the rendering and measurement are consistent:
http://msdn.microsoft.com/en-us/library/system.drawing.stringformatflags.aspx