Highlighting text in a textbox without using RichTextBox control - wpf

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.

Related

create custom control which include a text and a image

I want to show a lot of (hundreds of or more) controls in a window, the control include a text and a image and have 4 different appearances (different text color and image), when user click the control, its appearance will switch among the 4.
In order to improve performance, I want to create a custom control and try to choose a good lightweight base class for it, after googling, seems UIElement is the most lightweight, which involving call DrawText and DrawImage on DrawingContext object, however, here indicate the DrawText is expensive.
Another choise is control such as ContentControl, TextBlock or Button, but they are more heavy than UIElement, does anybody have any advice? many thanks.

How to put ellipsis (...) in RichTextBox

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.

.NET WinForms Localization - How to make control position based on length of 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.

Passing a TextBox value to an IValueConverter used by another databound control in Silverlight 3?

I have a Silverlight 3 control where I am using an ItemsControl to display a list of items. I have implemented a "filter" or "search" textbox that allows the user to enter a search term in a textbox on the control that will limit the items displayed in the ItemsControl to ones that contain the string entered in the textbox. I have been able to implement this functionality, but I'd like to enhance it by changing the color of the text of the search term in the text that is displayed in the items control. For example, if the user types "foo" in the search textbox, I would like to filter the items in the ItemsControl (which I am already doing) to items that contain "foo", and change the background color of the substring "foo" where it occurs in the ItemsControl.
Ideally, I would implement an IValueConverter and pass the value in the search textbox as the parameter to the converter. The converter could then search through the value that is being converted and change the background color of all "foo" substrings (not sure how I'm going to do this yet, something with Inlines maybe?). It seems parameter values passed to value converters must be static resources. Is there any way I can pass a non-static value to the value converter to accomplish what I'm trying to do? Is there another way I should be going about this that does not use an IValueConverter?
Mind you I haven't worked this out completely yet, but I do something sort of similar in my app using Interaction.Behaviors. This is a little more flexible than a straight converter solution. In my solution, I use it to highlight cells and/or text in a grid based on various conditions (e.g. is this a critical item? Make it red).
Take a look at this blog entry, it explains the process fairly well, and should apply to more than just a Telerik RadGridView. (FYI, I think you'll need to add a reference to System.Windows.Interactivity)
Blog

Programmatically Change Individual Cell Borders in a WinForms DataGridView

I need to programmatically change the border of individual cells in a WinForms DataGridView. When searching on the 'net, I found this link (http://bytes.com/groups/net-vb/501128-changing-datagridview-cell-borders-runtime) which is the same thing that I am trying to do; however, there isn't a code example there of the solution, which is
"So you can inherit from the
DataGridViewCell class and overrides AdjustCellBorderStyle method to get a
customized version of DataGridViewCell. Then you can use this customized
DataGridViewCell in your DataGridView.
Note:
In your customized DataGridViewCell, you should expose a
DataGridViewAdvancedBorderStyle public member so that DataGridView code can
set this member border style information to the cell. Then in the
AdjustCellBorderStyle implementation, you should check the this
DataGridViewAdvancedBorderStyle public member and return corresponding
DataGridViewAdvancedBorderStyle. Then DataGridView PaintCells can use it to
paint your cell.".
I'm having a hard time understanding implementing this solution. Could someone please translate the above into working VB.Net code and provide an example of calling it to change an individual cell's borders?
Here is a ready made example which does what you need, just hidden amongst the extra functionality of setting the background colour.
http://www.codeproject.com/KB/grid/hulihui_CustomDataGridVie.aspx
Look for the lines
// must draw border for grid scrolling horizontally
e.Graphics.DrawRectangle(gridPenColor, rect1);
That line draws a cells border, so to change an individual cells border change the Event args (CellBackColorEventArgs class) to include whatever properties you want to describe the border. Then in the DrawCellBackColor method draw the border based on these passed in properties (and whatever else you want to draw in the cell)

Resources