How to highlight portion of a Rich TextBox WPF control if there are more than 2 spaces? - wpf

I have a richtextbox control in my WPF 4.0 application. Now suppose I have a text like
"hello how [space][space] r u? [space][space] I am fine"
As can be noticed that there are two gaps between how and r as well as between ? and I.
When this will happen then the portion will be highlighted with green e.g. how ..r and from ? to I will be highlighted with green color.
i.e. if the space between two words are more than 2 then that will be highlighted with green.
Is it possible to do in WPF RichTextbox control? If so, please help me in writing so. As of now what I have done is that I have only able to find out the distance of spaces between two words.
Note:~ [Space] means the white spaces. Since the gap was not prominent(as it was not coming in the editor), that's the reason I made it like so.
Thanks
WPF4.0/ C#4.0

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7c9a622a-4e3b-451e-bc4c-ab9d011447e0
Here is a progressive scenario featuring RichTextBox highlighting multiple ranges, it may lead you on the right path.
He ends up getting it to work, but you will have to modify it to recognize white space perhaps.

Related

Texteffects in wpf

I would google this question, because I'm certain that it has been answered, but I just don't know how to describe it in words.
I want to make buttons in wpf, like the ones in the picture below. Step 1 would be to just make a white block and make it transparent. Step 2 would be to have a text that "delete/mask/clip/blends" the area that it "touches" on the white box. Where the text touches the white box I want the white box to have 100% opacity. I don't know how to google step 2, thus I came here.
Is this even possible to do in wpf, and if not, any other ideas?
Picture of the graphical mock up
Edit: sorry for bad title

rtf discrepency in italic (wordpad/msword)

i'm trying to figure this out for a few weeks, and i think i may have boiled down the problem
for now the issue is only in Hebrew text (though ill use Latin chars for simplicity)
if i have in a wpf richtextbox
p*q* (the q is italic)
and then copy it to ms word, i see both chars as non italic
now heres whats funny:
in ms word both chars show up as normal
when i click on the "q" the "I" (italics button) in words ribbon light up to indicate that this character is italics, its just not evident in the main window
in wordpad it shows up just fine
so next logical step was to disseminate the rtf, so heres the original rtf emitted to the clipboard by wpf
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\rtlpar\fs24\f2\cf0 \cf0\qr{\f2 \rtlpar{\lang1037\rtlch \u1488?}{\lang1037\i\rtlch \u1489?}\li0\ri0\sa0\sb0\fi0\qr\sl454\slmult0\par}}}
opening this up in msword gives the problematic result
making one small change fixes the issue, if i remove the first "rtlch",like this:
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\rtlpar\fs24\f2\cf0 \cf0\qr{\f2 \rtlpar{\lang1037 \u1488?}{\lang1037\i\rtlch \u1489?}\li0\ri0\sa0\sb0\fi0\qr\sl454\slmult0\par}}}
so whats going on here? and more importantly, how do i go about fixing it? can i just manually remove all the "rtlch" from the rtf? im not sure of the implications
thank you !!

Is it possible to add color formatting to a Text File to Be programmatically moved into a RichTextBox?

I have Some Text files that my program will parse through looking for specific lines as requested by the user to be displayed into a RichTextBox during runtime. There are certain comments in each line that I would like displayed in Red not the normal ForeColor, only one line "Scripture" will ever be displayed in the RichTextBox at any given time. Is there a way to add formatting in a Text File that when displaying a specific line into a RichTextBox it will display the comment you want with color?
ie... I have a Bible Scripture:
Then Jesus turned, and saw them following, and saith unto them, \What seek ye?\ They said unto him, Rabbi, (which is to say, being interpreted, Master,) where dwellest thou?
I want the words "What seek ye?" to be in red when it is displayed in the RichTextBox. The problem of course is that the program does not know which comments need to be displayed in red and the user will not have any control over that, so it needs to be something that can be handled from the File being read in.
The Format of the Text Files are basically:
(John 2:2) And both Jesus was called, and his disciples, to the marriage.
(John 2:3) And when they wanted wine, the mother of Jesus saith unto him, They have no wine.
(John 2:4) Jesus saith unto her, Woman, what have I to do with thee? mine hour is not yet come.
(John 2:5) His mother saith unto the servants, Whatsoever he saith unto you, do it.
Does anyone have any Ideas how I can get this accomplished? In the above Text example if the 3rd line (John 2:4) were to be displayed into the RichTextBox I would want the comments "Woman, what have I to do with thee? mine hour is not yet come." to be displayed in Red.
basically Im trying to create a random scripture generator to show a random scripture when the program runs and I'd like to give it Red Letter Formatting for the words of Jesus.
Here is an example of how the RTF property of a richtextbox will look for your text:
{\rtf1{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
{\colortbl ;\red255\green0\blue0;}
\f0\fs24 Then Jesus turned, and saw them following, and saith unto them, \cf1 What seek ye?\cf0 They said unto him, Rabbi, (which is to say, being interpreted, Master,) where dwellest thou?\par
}
Note the color defined in the color table (colortbl) which is red at index 1. I has the tag \cf1. Default \cf0 is black.
So use the same header I gave (you can change the font).
You can use a regular expression to replace \...\ with \cf1 ...\cf0.
As an alternative, you can use {\cf1 ...}
Then assign the result (whole rtf) to the rtf property of a richtextbox.
If you need help with any of this let me know.
By the way, it would be better if you can tag the red letters with some different tags, for example <Red>...<red> or <R>...<r> instead of \...\ to avoid problems.

Line spacing for different FontSizes in WPF RichtextBox

I want my RichtextBox to enable line spacing chosen by user (multiplier of 1, 1.5 or 2). I tried to programatically change LineHeight of paragraphs, setting it to FontSize*multiplier. That works fine, but it can't be used for different fontSizes in the same paragraph, because then I have to choose one of the sizes for the calculation of LineHeight.
I would like to have it similar like in WordPad or OpenOffice, where the actual LineHeight is calculated for each line using the biggest fontSize used on the line.
Is there any possibility to change the LineHeight for lines or to tell the RichTextBox to just use the multiplier? Or any other solution?
I think you are looking for the LineStackingStrategy="MaxHeight" for the Blocks. See this page: http://msdn.microsoft.com/en-us/library/system.windows.documents.block.linestackingstrategy.aspx

Silverlight 3 - Control over wrapping in TextBox

Ok I have the following problem in Silverlight. I have a control with 2 columns. On the left is a stack panel with line numbers and on the right is a textBox.
So when I write in textBox without wrapping turned on I can simply create the right count of numbers on the left, because I'm searching for '\r' in text.
But when I turn on wrapping I have no control over the count of lines in textBox.
Is there a way to get to that count? Or a way to know which line in textBox is going to wrap?
I hope you can understand what I'm trying to do.
There's one way to do this. You can simulate the word wrap operation in the background using a TextBlock. Here is a good link of the complete solution to this problem.
Extended TextBox control with MaxLines property
Is it not possible to create your items in code before they are passed to the view. This would enable you to bind a list of items to a listview and style them as you wish.
You need to user a value converter to count the number of char / lines and then trim that number if you wish to. Unless you use fixed width, you can't really count or calculte in advancet the size, since each application might be displayed differently (due to different sizing option).
There are two great sample chapters on Windows Phone and Silverlight for Windows Phone on the LearningWindosPhone.com site. There is great Windows Phone Trainng material , and dont forget the Windows Phone Develoeprs Blog
Yes there is a way to get the number of lines occupied by the text in the textbox. It's not that simple though 'coz you have to simulate the behavior of the word wrap in order to count/predict the number of lines generated as a result of a word wrap. I have the solution described here in detail.

Resources