Mesa
Mesa

Reputation: 1

C# string syntax error

I'm reading in data from a file and trying to write only the word immediately before 'back' in red text. For some reason it is displaying the word and then the word again backwords. Please help. Thank you.

private void Form1_Load(object sender, EventArgs e)
        {
            Regex r = new Regex(" ");
            StreamReader sr = new StreamReader("KeyLogger.txt");
            string[] tokens = r.Split(sr.ReadToEnd());
            int index = 0;
            for(int i = 0; i <= tokens.Length; i++)
            {
                if (tokens[i].Equals("back"))
                {
                    //richTextBox1.Text+="TRUE";
                    richTextBox1.SelectionColor = Color.Red;
                    string myText;
                    if (tokens[i - 1].Equals("back"))
                        myText = "";
                    else
                        myText = tokens[i - 1];
                    richTextBox1.SelectedText = myText;
                    richTextBox1.Text += myText;
                }
                else
                {
                    //richTextBox1.Text += "NOOOO";
                }

                //index++;
                //richTextBox1.Text += index;
            }
        }

Upvotes: 0

Views: 185

Answers (1)

Chris Taylor
Chris Taylor

Reputation: 53699

The problem is with the line

richTextBox1.SelectedText = myText; 

this is adding myText to the beginning of the text of the RichTextBox, if you remove this line you will get just the words preceeding back appended to the control in the order they appear.

Upvotes: 1

Related Questions