I need to be able to highlight multiple blocks of plain text given a start index and length inside an editable control. Now I know that it's not possible to do this with a regular TextBox so am looking at using a RichTextBox instead. The problem I'm having is that I can't seem to find what the clear text index would be from the selected text inside a RichTextBox.
I can add a background colour easily enough to a selection, but I need to be able to get at the clear text index of where the selection is and the length of the selection (which seems easy enough) as this is what is getting stored in a database.
Any help is greatly appreciated - this is driving me nuts!
Related
I want to put an ellipsis (..) in the rich text box.
In normal TextBlock, TextTrimming="WordEllipsis" has a property that limits the length to allow ellipsis representation, but rich text boxes do not. And it should only be implemented as a rich textbox now. Text blocks are not allowed.
I'd like to trim by two or three lines and add an ellipsis (..) option. Is there any good way?
I want to show you how I'm using RichTextBox, but the reputation is low.
You don't
The ellipsis concept is, as stated by grek40, something that only works when displaying text.
As an example, say the ellipsis is displayed, and the user tries to partially select some text in your RichTextBox, including the ellipsis, what would the selected result be? You can't tell.
Maybe
Since an ellipsis is usually a replacement for a Scrollbar, hiding text instead of allowing you to access it by scrolling, you might be able to fake it by using a WPF Style.
Create a Style that displays an icon/picture of an ellipsis (placement is up to you) whenever the Scrollbar visibility trigger is triggered. You would need to disable the Scrollbar once the ellipsis is visible.
This obviously requires more effort than simply setting a property, and it can easily become a User Experience nightmare if not carefully implemented, so be warned.
Note: Another comment (by Walt Ritscher) linked to a similar question, the solution there is similar to this one.
Alternative Maybe
Another faked ellipsis could be achieved by using two different RichTextBox controls.
The first RichTextBox would have set ReadOnly to true. Trim your text to a maximum allowed length, and append an ellipsis and display it inside this first RichTextBox.
When the user need to edit the text hide the first RichTextBox and display a second RichTextBox that contains your entire text.
OK, I admit this is an unusual case! I need a masked TextBox in which groups of different characters are different sizes. The text has to be selectable as a single block for copy / paste / delete purposes.
I'm not sure how I'd go about this in WPF. Any ideas?
You want to change from TextBox to RichTextBox. In a richtextbox, you can control format of parts of the text.
I am working on localizing a WinForms application through satellite assemblies. The problem I am facing is that when I switch to some other language such as Japanese from English, the labels overlap (or go behind) the combo boxes because of the change in text. The change in text causes Label width to increase making them jump over to other controls.
Is there a way (maybe through Anchoring) to overcome this so that the combo boxes decrease their width automatically to compensate for the Label size increase? Something like anchoring WRT a control instead of a Form.
UPDATE
OK. I think I've found a solution to this one. The Form has a Language property which is used to set language specific properties for a Form. If I change it to Japanese and make changes to Form layout and Control positions then these language specific items are stored in a new .RESX file for each Form. The Satellite assemblies will still be used to provide the localized strings but these RESX files will resize the Form elements accordingly. Is this the right way?
The Form has a Language property which is used to set language specific properties for a Form. If I change it to Japanese and make changes to Form layout and Control positions then these language specific items are stored in a new .RESX file for each Form. The Satellite assemblies will still be used to provide the localized strings but these RESX files will resize the Form elements accordingly.
I don't know anything that can resolve satisfactorily your problem.
There is the property AutoEllipsis=True (with AutoSize=False) that can alleviate the situation.
If set to True this property insert three dot to the right of the label when the text exceed the label dimensions. Positioning the mouse over the label will dispaly a tooltip with the full text.
I have a WPF textbox (not a RichTextBox) and inside this textbox, I want to highlight search results (like in web browsers)
For example if I search for "abc", all occurences of "abc" should be highlighted (for example, with some red background or font)
I want to know if this is possible without using RichTextBox control, or not really?
It's possible but it's much more easy to use a RichTextBox so you may consider to use that instead, moreover you cannot change font size but only color (background and/or foreground) and effects.
First you have to derive your own class from TextBox because you'll override its render method. Now override the OnRender() method, here you'll use the DrawingContext.DrawText() method to draw the text (place everything inside a FormattedText object, primary you'll have to adjust its properties to make it similar to a standard TextBox).
Now what you have is a plain TextBox where in addition you draw your text. From this starting point you can choose to:
Completely override TextBox text drawing: set TextBox.Foreground property to Brushes.Transparent. User will interact with "real" text but he'll see the text your draw. Please note that to make this works you have to mimic exactly how text is drawn (if you change font size, for example, then they'll be unaligned) in the original TextBox.
Add the highlight feature you need keeping the base TextBox text drawing: calculate where the text you want to highlight is and then draw the proper background.
References
This (simplified!) algorithm comes from CodeBox2, it was originally designed to extend a TextBox with some simple editor-like features.
There is no built-in functionality for this. Also, the TextBox only supports a single fontstyle for the entire text.
If the text should be read-only, you could use a flow or fixed document and format the text in Run Elements.
I am trying to render a WPF window that has lots of labels, textboxes, textblocks which are binding to data.
I want to be able to select by mouse all the data or part of the data on the window.
Is there a simple way in WPF to do that?
Should I use page instead of window?
Thanks
It is not possible to select text across multiple TextBox, Labels and TextBlocks in WPF.
Each control will hold selection specifically to itself so you must use another approach that will enable you to select the text.
I can think of one rudimentary way to achieve this is to have a very small button beside each TextBox,TextBlock, etc. that you wish to select and this button will perform a string concatenation of the associated text to the clipboard.
The result is that you have all the strings you wish to select in your clipboard ready to paste to wherever you like.
Good luck.