Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:26:14+00:00 2026-06-10T06:26:14+00:00

Below are three methods I have written for my RichTextBox derived class, which is

  • 0

Below are three methods I have written for my RichTextBox derived class, which is a syntax highlighter. There is a shared copy, lcpy_strLine, of the current line of the richtextbox in all upper case. What these methods do is;

  • ColorInsideTwoChars colors the characters between the two specified characters the specified color. Ex. ColorInsideTwoChar("(", ")", Color.Green) would color all the characters between two parenthesis green for all sets of parenthesis in the current line

  • ColorTilNoNumFromChar colors, from the specified character, all of the characters that are numbers, Ex. ColorTilNoNumFromChar("G", Color.Red) would color all of the numbers after a G is encountered, red (this includes the G)

  • ColorCharIfNotFollowedByLetter colors the specified character if it is not followed by a letter. Ex. ColorCharIfNotFollowedByLetter("x", Color.Orange) would color all the X’s orange that are not followed by a letter

My question is, is there a faster way to do these methods. They look ugly and I think that there are definitely simpler and more aesthetic ways of doing these methods. Any suggestions? I ask because these methods are ran on each line of few thousand line files and are pretty slow. I need to speed them up. I could try and rewrite each of them a different way a few times, or I could have a few smarties try and head me in the right direction.

    private void ColorInsideTwoChars(String car1, String car2, Color clr)
    {
        int indx1 = 0;
        int indx2 = 0;
        while ((indx1 = lcpy_strLine.IndexOf(car1, indx1)) != -1
                && (indx2 = lcpy_strLine.IndexOf(car2, indx2)) != -1
                && indx1 < indx2)
        {
            SelectionStart = m_nLineStart + indx1;
            SelectionLength = (indx2 - indx1) + 1;
            SelectionColor = clr;
            indx1 = ++indx2;
        }
    }
    private void ColorTilNoNumFromChar(String car, Color clr)
    {
        int indx1 = 0;
        while ((indx1 = lcpy_strLine.IndexOf(car, indx1)) != -1)
        {
            int j = 0;
            for (j = indx1 + 1; j < m_nLineLength; j++)
            {
                if (!Char.IsDigit(lcpy_strLine[j]))
                    break;
            }
            SelectionStart = m_nLineStart + indx1;
            SelectionLength = j - indx1;
            SelectionColor = clr;
            indx1 = j;
        }
    }
    private void ColorCharIfNotFollowedByLetter(String car, Color clr)
    {
        int indx1 = 0;
        while ((indx1 = lcpy_strLine.IndexOf(car, indx1)) != -1 &&
            indx1 + 1 < m_nLineLength)
        {
            SelectionStart = m_nLineStart + indx1;
            SelectionLength = 1;
            if (!Char.IsLetter(lcpy_strLine[lcpy_strLine.IndexOf(car) + 1]))
                SelectionColor = clr;
            else
                SelectionColor = Color.Black;
            ++indx1;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:26:16+00:00Added an answer on June 10, 2026 at 6:26 am

    As for ColorInsideTwoChar(), note that you’ll color what’s in between parentheses even if they are nested, resulting in doing it more that once. What if parentheses (or these chars in general) aren’t matched properly? Maybe find opening char, then mathing it closing char and color everything in between? Then start searching from position at which you had closing char, and look for the next opening one.. Also, I would call this only on line change, don’t know what’s your idea 😉 And maybe only on visible part of RichBox first, and then color the rest in the background?

    for (j = indx1 + 1; j < m_nLineLength; j++)
            {
                if (!Char.IsDigit(lcpy_strLine[j]))
                    break;
            }
    

    i would swap for

    while (Char.IsDigit(lcpy_strLine[++j])) ;
    

    although it’s probably more or less the same…

    But I think you should wait for some more professional answer, there’s probably some more elegant way to do this.

    Also, maybe finding all occurences of wanted chars at the beginning of methods, and putting them into an array, would be faster than looking them up again and again?

    It might be also faster to do that for larger chunks of file than single lines…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a sample program below. class Program { static int x =
I have a class Stack, using template, one of its methods is push, which
Is there any difference between the 2 methods below for calculating c ... specifically
Of the below three functions: getc getchar & scanf which is the best one
I know that technically all three ways below are valid, but is there any
I have three tables as shown in below image. Note: Lead column of projectheader
I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator
I have written a custom SessionStoreProvider class that inherits from the SessionStateStoreProviderBase. I have
I have a series of python modules written which are held in the same
I have written a java class where if a method throws an exception, an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.