Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a List of MyClass.

Here is a pseudo definition of MyClass
{
string text;
int Attributes;
}

I want to display them in a (single) TextBox / RichTextBox in order separated by space. The user should not be able to edit the text in the text box.
If the list forms the sentence "My Name is Joe"(each word in a different instance of MyClass and is in order in the List), if the cursor is clicked somewhere between a and m in name, the entire word 'name' should get selected. on using right arrow we should select the next word. vice versa for left arrow. If the user uses shift and then taps on right key multiple times, multiple words should get selected. I need to know which words are selected from the list so that I can fire an event regarding the same.

Additionally, each word should be coloured differently depending on the Attributes. I have the colour scheme predefined. For example, 1 = red, 2 = blue etc.
Any help would be appreciated.
Posted
Updated 2-Jun-13 23:25pm
v3
Comments
bbirajdar 31-May-13 9:10am    
I can help.. I will provide you with the HTML color codes for 1 = red, 2 = blue etc..
rohith naik 31-May-13 9:13am    
Thanks but i have a predefined set for this already.
bbirajdar 31-May-13 9:16am    
I can also help you make the textbox readonly..
rohith naik 31-May-13 9:19am    
How so ?

What your asking is possible, but not the easiest. I would look up other richTextBoxes, they are easier to work with and can provide other feutures like error underlining. Just a heads up.

Well, from my understanding you said you don't want it to editable. Simply just set the Read-Only property to true.

The coloring part is difficult, because I know that you can only change the color of a desired text part by Selecting it, Coloring it, then unselecting it. Which can be really ugly. I'm wondering if it would be easier to make your own custom control with multiple RichTextBoxes so you can just set the color of each one separately without selecting the text.

Back to the selecting part, if you were to keep one richtextbox, you're going to want to keep information for each MyClass containing a StartIndex and Length, so when the user clicks you can go through your list of MyClass's and see if it contains the cursor. This algorithm could also you used to select the whole word, that actually seems like a easy concept.

That roughly covers, what I thought you said, except for the shift-selecting which probably would be easy to figure out if you got the other concepts working first. Here are some articles explaining further in depth on RichTextBox coloring seperate texts.

Multiple Colored Texts in RichTextBox using C#[^]

How to Change Font Color on RichTextBox after USING SPLIT[^]

Hope this helps.
 
Share this answer
 
Comments
rohith naik 3-Jun-13 3:48am    
Hi, both the links you provided are for richtextBoxes in WinForms. I am working in WPF. Thanks. BTW I am not really specific to RichTextBoxes. Any other control will work just fine for me as long as it is a WPF control.
I would suggest looking at this:

http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-richtextbox/[^]

He does formatting manipulation in both code and in xaml.
 
Share this answer
 
Comments
rohith naik 3-Jun-13 6:58am    
The example project is not opening on my computer :(
Pheonyx 3-Jun-13 7:00am    
You should be able to open the individual files and browse the code.
I didn't even open the project, just read the article to see what he was doing.
rohith naik 3-Jun-13 7:07am    
Any idea how I can color individual words in a paragraph?? He only gives an example of coloring the whole paragraph.
Pheonyx 3-Jun-13 7:08am    
Yes, read the article. He does similar in a code snippet. Use that as a base and experiment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900