HI Guys.

I'm trying to make my own lite program that can highlight specified chosen words (Syntax highlighting in short).

I have a working method using Regex, but it is TOO SLOW.

Here is what is currently:

My Regex Statements:

public Regex SettingsAndComments = new Regex("--settings|-|", RegexOptions.IgnoreCase);
        public Regex Numbers = new Regex("0|1|2|3|4|5|6|7|8|9|");
        public Regex TTrue = new Regex("true|", RegexOptions.IgnoreCase);
        public Regex FFalse = new Regex("false|", RegexOptions.IgnoreCase);
        public Regex MainTitles = new Regex("Logging & Displaying|Char Builds|GAME DELAYS|POTION & CHICKEN SETTINGS|MERC POTION & RESURRECT SETTINGS|BOT ACTIONS & BEHAVIORS|IN TOWN SETTINGS|" +
                                            "CUBE SETTINGS|GAME CONFIGURATION & OPTIONS|GRUSH SETTINGS|DIA CLONE HUNT SETTINGS|IMMUNITY SETTINGS|AURA SKIP FUNCTIONS|INVENTORY LOCK|BAAL SETTINGS|" +
                                            "BAAL MESSAGES & SETTINGS|WAVE-BASED AURA CHANGES|ADVANCED BAAL OPTIONS|DON'T MODIFY UNLESS YOU KNOW WHAT TO DO|PRECAST LOCATION|THRONE TELEPORT LOCATION|" +
                                            "THRONE TOWN PORTAL LOCATION|DIABLO SETTINGS|CHAOS-DIABLO-GRUSH MESSAGES|SEAL ORDER - TP LOCATION|MEPHISTO SETTINGS|SHRINE PICKING|PICKIT REVOLUTION|" +
                                            "Pickit Settings|Pickit Version|", RegexOptions.IgnoreCase);
        // Potions
        public Regex PRed = new Regex("PotionType.Red|", RegexOptions.IgnoreCase);
        public Regex PBlue = new Regex("PotionType.Blue|", RegexOptions.IgnoreCase);
        public Regex PPurple = new Regex("PotionType.Purple|", RegexOptions.IgnoreCase);

The "Colourize function" that makes them all work:

public void Colorize()
        {
            MainText.BackColor = Color.White;

            int selPos = MainText.SelectionStart;
            // Colours

            # region Basic "Settings" Syntax Highlighting

            foreach (Match keyWordMatch in SettingsAndComments.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.MidnightBlue;
                MainText.SelectionStart = selPos;
            }
            foreach (Match keyWordMatch in Numbers.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.DeepPink;
                MainText.SelectionStart = selPos;
            }
            foreach (Match keyWordMatch in TTrue.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.Green;
                MainText.SelectionStart = selPos;
            }
            foreach (Match keyWordMatch in FFalse.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.Red;
                MainText.SelectionStart = selPos;
            }
            foreach (Match keyWordMatch in MainTitles.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.DarkOrange;
                MainText.SelectionStart = selPos;
            }

            #region Potions
            foreach (Match keyWordMatch in PPurple.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.DarkViolet;
                MainText.SelectionStart = selPos;
            }
            foreach (Match keyWordMatch in PRed.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.DarkRed;
                MainText.SelectionStart = selPos;
            }
            foreach (Match keyWordMatch in PBlue.Matches(MainText.Text))
            {

                MainText.Select(keyWordMatch.Index, keyWordMatch.Length);
                MainText.SelectionColor = Color.DarkBlue;
                MainText.SelectionStart = selPos;
            }
            #endregion

            #endregion

            MainText.SelectionColor = Color.Black;

        }

And the Private void that brings everything together.

private void MainText_TextChanged(object sender, EventArgs e)
        {
            Colorize();
        }

Does anyone know how I can speed this up? or a BETTER alternitive?

When it runs, (IE if I load a document into here) it freezes my program, and takes forever checking them.
I would like this too not take 10 minutes to open a 63 kb lua file.

:P

Thanks!

Edit: Sorry, forgot to mention it is in a Rich Text Box!

92 viewers.
Anyone have any suggestions or should I post this on another site?

Note: waited 24 hours before "post bump"

I'm glad you got it helpful. Please mark this thread as solved if you have found an answer to your question. If you want to ask question, start a new thread.

At the bottom of last post, a link Mark as Solved.

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.