Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Stupid Question... (Rich Text Box / C#)

Back to Forum: Tech Off
  • User profile image
    Manip

    The below code is fairly simple .. in theory it SHOULD change every instance of any 'keyword' to blue, leaving everything else black. Except it turns the keywords + 7 characters blue for no good reason. Using a rich text control or some other control is there an easier way of doing this?

    [The site breaks the tabs .. ]

    private
    void sourceEditor_TextChanged(object sender, System.EventArgs e)

    {

       int selOrg = sourceEditor.SelectionStart;

       for(int y = 1; y < keywords.Length; y++)

       {

          for(int x = 1; x < sourceEditor.Text.Length; x++)

          {

          if(keywords[y].Length + x >= sourceEditor.Text.Length) break;

          if(sourceEditor.Text.Substring(x, keywords[y].Length) == keywords[y])

          {

             sourceEditor.SelectionStart = x;

             sourceEditor.SelectionLength = x + keywords[y].Length - 1;

             sourceEditor.SelectionColor = Color.Blue;

         }

       }

    }

    sourceEditor.SelectionStart = selOrg;

    sourceEditor.SelectionLength = selOrg + 1;

    sourceEditor.SelectionColor = Color.Black;

    sourceEditor.Focus();

    }

  • User profile image
    Manip

    Forget it ..

    sourceEditor.SelectionLength = x + keywords[y].Length - 1;

    This is wrong .. should read

    sourceEditor.SelectionLength = keywords[y].Length + 1;

    My bad Smiley

  • User profile image
    Rossj

    Manip wrote:

          if(sourceEditor.Text.Substring(x, keywords[y].Length) == keywords[y])



    Why this style of string comparison still annoys me I'll never know.

  • User profile image
    amotif

    Rossj wrote:
    Manip wrote:

          if(sourceEditor.Text.Substring(x, keywords[y].Length) == keywords[y])



    Why this style of string comparison still annoys me I'll never know.


    By that you mean how == is defined for String to mean Equals?  It seems pretty natural for me, coming from C++, but I forget I can do it that way (coming from Java...).

  • User profile image
    Rossj

    amotif wrote:
    Rossj wrote:
    Manip wrote:

          if(sourceEditor.Text.Substring(x, keywords[y].Length) == keywords[y])



    Why this style of string comparison still annoys me I'll never know.


    By that you mean how == is defined for String to mean Equals?  It seems pretty natural for me, coming from C++, but I forget I can do it that way (coming from Java...).


    Exactly, all that Java does tend to skew your mind a little Wink

  • User profile image
    amotif

    Rossj wrote:

    Exactly, all that Java does tend to skew your mind a little Wink


    After pondering the issue, I've decided that it's not that I forget that one can use == for String comparison in C#, but that I prefer the more explicit .Equals().  Yeah, that's the ticket... Wink

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.