0

Why doesn't this code work?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (Match match = new Regex(@"<(/|)[^(>|\s)]*").Match(richTextBox1.Text); match.Success; match = match.NextMatch())
            {
                richTextBox1.Select(match.Index + 1, match.Value.Length - 1);
                richTextBox1.SelectionColor = Color.Brown;
                richTextBox1.DeselectAll();
                richTextBox1.SelectionColor = Color.Black;
            }

            for (Match match2 = new Regex("123").Match(richTextBox1.Text); match2.Success; match2.NextMatch())
            {
                richTextBox1.Select(match2.Index, match2.Value.Length);
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.DeselectAll();
                richTextBox1.SelectionColor = Color.Black;
            }
        }
    }
}

Each time I write "123" in the richtextbox does the program stop working.
Why does the program stop working when I write "123" and what code should I use instead?

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by FullBjarne
0

Hi, You made a small mistake

Change for (Match match2 = new Regex("123").Match(richTextBox1.Text); match2.Success; [B]match2.NextMatch()[/B]) as for (Match match2 = new Regex("123").Match(richTextBox1.Text); match2.Success; [B]match2 = match2.NextMatch()[/B]) ie
match2 = match2.NextMatch()
Not
match2.NextMatch()

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.