Click here to Skip to main content
15,881,678 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have data in richtextbox.
C#
11 2 3 4 12 23 456 234

i want colorize every symbol. I want split string and add index of begin and end of string in the List of class Elements.
C#
class Elements
   {
       private string _data;
       private string _begin;
       private string _end;

       public string Data { get; set; }
       public string Begin{ get; set; }
       public string End  { get; set; }
}

C#
[11,0,2]  11 is the first element, 0 begin and 1 end of this string
[2,4,5]   2 is the second element, 4 begin and 5 end of this string.


and with this code colorize elements
C#
foreach (var item in List<Element>) // example code
            richTextBox1.SelectionStart = item.Begin;
            richTextBox1.SelectionLength = item.End
            richTextBox1.SelectionColor = Color.DarkBlue;

How insert data in Class Elements. I dont understand how define begin and ending of elements?
Posted
Comments
BillWoodruff 27-Oct-14 5:10am    
Note: see my answer below for code I know works.

Can you assume that only one space character separates each numeric entry ?

The way I see it ... assuming single-space separation ... your data parses like this:

// "11 2 3 4 12 23 456 234";
//data 11, begin: 0, end: 2
//data 2, begin: 3, end: 1
//data 3, begin: 5, end: 1
//data 4, begin: 7, end: 1
//data 12, begin: 9, end: 2
//data 23, begin: 12, end: 2
//data 456, begin: 15, end: 3
//data 234, begin: 19, end: 3

Note the value for 'end I use here is simply the length of the data-item, not its absolute position, because: you can easily select Text in a RichTextBox by:

myRichTextBox.Select(#start-position, #length-to-select);

The below code is one way to do it, but I'm not sure I'm totally clear on your needs. Below I get the value, and then the begin and end indices of that value. So it would be [11,0,1], [2,3,3], ..., because that is the actual start and end index of the value. What are you getting the indices of? If you are getting the index of the whitespace character after the end of the token, then remove the subtraction by one when calling the Element constructor. Oh, and I used a Struct rather than Class because it's a bunch faster using the stack instead of the heap.

 string values = "11 2 3 4 12 23 456 234";
 List<Element> elements = GetElements(values);
 // Render to RichTextBox

// Precondition: All values are single whitespace delimited
public static List<Element> GetElements(string input) {
    List<Element> elements = new List<Element>();
    string value = "";
    for (int beginI = 0, i = 0; i < input.Length; i++)
    {
        if (input[i] == ' ') // end of token
        {
            elements.Add(new Element(value, beginI, i - 1));
            value = "";
            beginI = i + 1;
            continue;
        }
        else if(i == input.Length - 1) // end of input string
            elements.Add(new Element(value +  input[i], beginI, i - 1));
        else
            value += input[i];
    }
    return elements;
}

public struct Element
{
    public string Data;
    public int End;
    public int Begin;
    public Element (string data, int begin, int end){
        Data = data;
        Begin = begin;
        End = end;
    }
}
 
Share this answer
 
v4
Comments
[no name] 27-Oct-14 5:04am    
thank you Robert. Edit code. i - 1 not correctly work) Its not get string end index/
Robert Welliever 27-Oct-14 5:12am    
Yah, I got the end index of the actual token/element, but you needed the index of the following whitespace. I understand now, good luck with your project!
Maciej Los 27-Oct-14 6:57am    
Good job! +5!
BillWoodruff 27-Oct-14 8:22am    
+5 Good answer.
I think you may be closer than you think you are to finding a solution for this.

The code shown here assumes: Each data-item is separated from the other data-items by one space character. i.e., there are no line-breaks in the data being parsed, no tabs, etc. To handle data-items separated by some arbitrary amount of white-space would require a different approach, but could be done.

The code here has the single most important step left for you to figure out.
C#
private class Elements
{
   public string eData { get; set; }
   public int eStart { get; set; }
   public int eLength { get; set; }

   public Elements(string data, int start, int length)
   {
       eData = data;
       eStart = start;
       eLength = length;
   }
}

// you want to store the results of your parsing the data
private List<elements> theElements = new List<elements>();

// for splitting the data string:
// assumes each data item is separated by one space from other data items
private char[] splitChar = new[] { ' ' };

// some test data: assume this is on one line in a RichTextBox
// named 'richTextBox1
private string testData = "11 2 3 4 12 23 456 234";

private void createElements(string data)
{
    theElements.Clear();

    string[] entries = data.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);

    int begin = 0;

    for (int i = 0; i < entries.Length; i++)
    {
        string entry = entries[i];
        int eLen = entry.Length;

        theElements.Add(new Elements(entry, begin, eLen));

        begin += // for you to figure out
    }


    for (int i = 0; i < theElements.Count; i++)
    {
        var element = theElements[i];

        richTextBox1.Select(element.eStart, element.eLength);

        // alternate the colors of the items as a
        // visual test of the code
        richTextBox1.SelectionColor = (i%2 == 0)
            ? Color.Red
            : Color.Blue;
    }
}
 
Share this answer
 
v2
Comments
Maciej Los 27-Oct-14 6:56am    
Very interesting solution, Bill, a 5!

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