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?

Recommended Answers

All 2 Replies

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()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.