The width of Tab (\t) character in silverlight TextBox is not equal to 4 spaces or 8 spaces. It's too short.
Is it possible to change the width of the TAB (\t) character displayed in a silverlight TextBox?
Note that I want to avoid replacing TABs with spaces.
Any ideas on how to do this?
Silverlight does not allow you to change the tab character length in a TextBox.
If your reading in a string (from a file or something) and setting the Text to it then if you look at your Text Property you'll see the escaped tab (\t). Searching for a \t is easy
TabTextBox.Text = TabTextBox.Text.Replace("\t", " ");
So this will replace all tabs with 4 spaces.
Besides pressing tab in a TextBox will not tab the text. It will focus to the next UIElement within the parent UIElement.
Or maybe on tab key press event, append the string literal to the text box. Similar to what MyK is suggesting.
If you're trying to get this to display, write a converter. the syntax is around--just grab an example, gut it, rename to something like 'tabstoptexttospacedtextconverter', add a reference to your local controls in app.xaml, then create an instance of it and give it an x:name to use it. Bind the data for your text box and assign it your new converter.
It'll be a little bit of hassle because you'll have to determine the appropriate width of the final display TB, and then probably use a converterparameter to make that work. But long story short, split on \t, then foreach(string str in splitSourceText) do something like this:
for (int i = 0; i < (str.Length % 8 > 0 ? str.Length % 8 : 8); i++) str+= " ";
You can limit the characters on the split string arrays with the parameter or split on \r\n first.
Related
For multiline TextBox in wpf, i find the length of the chars of the text is not always equal.
For instance, if i put kkk in the first line, "f f" in the 2nd line, they wont align...since f and space take smaller size.
How to stop that and make every char has the same size??
So that my strings can be align?
Set the font to Courier New or another monospace font.
I need the way to render regular space, nonbreaking space and some other formatting characters (like left-to-right mark) like MS Word renders them, when you choose to show non-printable characters.
I tried two approaches:
1) Replace characters with rarely used characters.
It works, but in this case we loose "nonbreaking" behavior of nonbreakable space (and LTR and RTL marks also stop working)
2) Use the custom font
It allows to preserve special behavior of nonbreaking space and LTR/RTL marks, but for some strange reason WPF renders nonbreaking space with usual space glyph.
WinForms RichTextBox renders text with the same font correctly.
This problem could be solved with applying different font with different space glyph for spaces and nonbreaking spaces, but LTR and RTL marks are not rendered at all even if I add glyph for them.
Have you any ideas how I could render that characters with visible glyph preserving their "LTR", "RTL", "nonbreaking" behavior?
I didn't try anything similar until now, but I can think of two options:
Warning -> I didn't try it out
The first method:
Create a subclass of UIElement
Get the Style with ControlTemplate for the Richtextbox and add it to App.xaml
Add an instance of your subclassed UIElement within the inner Panel of the Scrollviewer from the RichTextBox ControlTemplate
Make the RTBox available to a dependency property in your class via DataBinding in the ControlTemplate (if possible) or any other way that does the job
In your UIElement subclass, you iterate through characters of the document
Draw a symbol in your Adorner for each space and LineBreak you encounter
Get the Rect of a character at a specific position with the RichTextBox. Use this rect for placing the symbols.
The advantage of this Method is that you have a clean separation and don't need to subclass the RTFBox, but you won't be able manipulate the width of the spacing to make room for larger symbols. Also, other developers need to know that they need that Style in order to gain that functionality.
The second method:
Create a Custom Adorner
Decorate the RTBox with the custom Adorner
From the Adorner, you should be able to access the Child RTBox
In your UIElement subclass, you iterate through characters of the document
Draw a symbol in your UIElement for each space and LineBreak you encounter
I remember that there is a possibility to get the Rect of a character at a specific position with the RichTextBox. Use this rect for placing the symbols.
It's also without subclassing the RTBox. You also can't adjust the spacing. In contrast to method 1, other developers will immediatly recognize that this functionality has been added. The one disadvantage is that you will have to handle scrolling too.
You can try insert near LTR/RTL visible glyph instead of replace that.
Store all of the values as their special characters. It sounds like your printing functions need to handle a) what kind of output the user wants, b) interpret your data array/massive string of characters and spit out the values with regard to what the user wants to see. You don't give too many details on how your things are stored but this would be a very reasonable way to go about things.
I'm developing an application for a bank, and I need a textbox for entering the money, like
My Idea was to create a textbox that has as a background the image of the grid, and than I just set the text size such that there is only a character in each box. but writing iiiii(5 characters) is as long as wwww (4 characters). Can I set a font or a character spacing such hat i ensure that the characters writen in the textbox will appear in separate boxes.
Ps: there are other similar boxes for name, so I don't inpu only digits.
You could use monospace (fixed width) font, Courier for example. Or you could create custom control with TextBox for each character, but in this case you would have to implement big chunk of custom logic.
Using a Glyphs control, you can set indices for exact spacing of the characters of the UnicodeString property. Check this for details: http://msdn.microsoft.com/en-us/library/bb979862%28VS.95%29.aspx.
You can set the value for the UnicodeString in the code-behind or viewmodel if you are using MVVM.
Hi Guyz I have a WPF TextBlock of fixed width say 100 , If the string doesnt fit in the width the last character is being cutoff always as all the characters are of not the same size. I dont want to cut the character instead I want to skip the text from there and just display the text with no character cutoff.
You have a couple of options to control wrapping and cutting of text:
TextWrapping can be used to make the text flow to the next line
TextTrimming can be used to decide how to cut text that doesn't fit
TextTrimming=None (the default) will mean that text which doesn't fit will be hidden, but it may cut down the middle of a character, which sounds like the problem you describe.
TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid showing half a character, but will append "..." to the end of the text. That will probably look better to users.
If you want to cut off the extra characters without adding the ellipsis, you'd have to use the technique Ed S. described
I suppose that I don't really understand your use case here. My first suggestion would be to simply dynamically size your TextBlock. If that's not possible then you wil have to get the width of the string and manipulate it yourself before you set it in the TextBlock (or use a fixed width font assuming that you can and you know the max length of the string).
If you need to measure the width of the string before it is displayed you can use the FormattedText class to do so.
We are facing problem regarding Text trimming in TextBlock.
We have a Text "My very long label goes here" and we have to show it in following format:
[...g label goes here]
i.e right aligned and text should be trimmed from left (character ellipsis at left side)
We have used FlowDirection of TextBlock, ellipsis is correctly shown on the left side, but text is trimmed from the right side.
FlowDirection = "LeftToRight:"
[My very long lab...]
FlowDirection = "RightToLeft:"
[...My very long lab]
But we need:
[...g label goes here]
Please provide suggestions on this.
You don't mention how the TextBlock gets its string, but if its from a binding, have you considered using a value converter to just trim the string?
I guess this is also only applicable if your requirement is X number of characters before trimming, not if your requirement is Y number of pixels..