0

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;
            }
        }
1
  • 7
    KeyLogger.txt ??? Commented Jun 6, 2010 at 6:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.