Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,299 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,464 people online right now. Registration is fast and FREE... Join Now!




richtextbox coloring all my text, ack...

 
Reply to this topicStart new topic

richtextbox coloring all my text, ack...

maffelu
22 Aug, 2008 - 12:45 PM
Post #1

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
Allright, I am just for fun making a CSS highlighter of my own, and I want my richtextbox to highlight all Idīs (like #top or #container) it contains, so I have this code when a new Id is entered to turn it red:

CODE
private void brMain_TextChanged(object sender, EventArgs e)
        {
            //brMain being the richTextBox
            string MainText = brMain.Text;
            int Starter = 0;
            int midler;
            int Ender;

            

            do
            {
                Starter = MainText.IndexOf("#",Starter);
                if (Starter < 0)
                { return; }

                Ender = MainText.IndexOf("{", Starter);
                //Get a length
                midler = Ender - Starter;
                brMain.SelectionStart = Starter;
                brMain.SelectionLength = midler;
                brMain.SelectionColor = Color.Red;
                //To get a continous flow =)
                Starter = Ender;
                brMain.SelectionStart = brMain.SelectionStart;
                brMain.SelectionLength = brMain.SelectionLength;
            }
            while (Starter > 0);

        }


Now, this, for some reason, colors all the text red.
If I just enter on Id, say "#top { }" it colors only #top, and that's great, but if I add anotherone, say #left, then everything turns red. Somewhere when it loops it decides to select all text and turn it red, and I can't seem to understand WHERE is does this or why. Now, I'm quite new to RTB, so I might have missed something very important.

Anyone had this experiance?
User is offlineProfile CardPM
+Quote Post

skyHigh
RE: Richtextbox Coloring All My Text, Ack...
22 Aug, 2008 - 02:33 PM
Post #2

D.I.C Head
**

Joined: 1 Oct, 2007
Posts: 87


My Contributions
QUOTE(maffelu @ 22 Aug, 2008 - 01:45 PM) *

Allright, I am just for fun making a CSS highlighter of my own, and I want my richtextbox to highlight all Idīs (like #top or #container) it contains, so I have this code when a new Id is entered to turn it red:

CODE
private void brMain_TextChanged(object sender, EventArgs e)
        {
            //brMain being the richTextBox
            string MainText = brMain.Text;
            int Starter = 0;
            int midler;
            int Ender;

            

            do
            {
                Starter = MainText.IndexOf("#",Starter);
                if (Starter < 0)
                { return; }

                Ender = MainText.IndexOf("{", Starter);
                //Get a length
                midler = Ender - Starter;
                brMain.SelectionStart = Starter;
                brMain.SelectionLength = midler;
                brMain.SelectionColor = Color.Red;
                //To get a continous flow =)
                Starter = Ender;
                brMain.SelectionStart = brMain.SelectionStart;
                brMain.SelectionLength = brMain.SelectionLength;
            }
            while (Starter > 0);

        }


Now, this, for some reason, colors all the text red.
If I just enter on Id, say "#top { }" it colors only #top, and that's great, but if I add anotherone, say #left, then everything turns red. Somewhere when it loops it decides to select all text and turn it red, and I can't seem to understand WHERE is does this or why. Now, I'm quite new to RTB, so I might have missed something very important.

Anyone had this experiance?

I"m just scaning through your code although doesn't really know the context of it, but I know little bit about txt coloring. Since you assign the text to red with the following statement:
brMain.SelectionColor = Color.Red;
For that reason, all the text will be red. If you want to undo the red color, you have to assign like you did previously to color you want.

Hope that helps.

This post has been edited by skyHigh: 22 Aug, 2008 - 02:34 PM
User is offlineProfile CardPM
+Quote Post

jacobjordan
RE: Richtextbox Coloring All My Text, Ack...
22 Aug, 2008 - 04:04 PM
Post #3

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,163



Thanked: 32 times
Dream Kudos: 1625
My Contributions
Check out this article on CodeProject
User is offlineProfile CardPM
+Quote Post

maffelu
RE: Richtextbox Coloring All My Text, Ack...
22 Aug, 2008 - 08:45 PM
Post #4

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
Well, here is my problem, should have explained further:

In normal highlighters you highlight upond keystroke. In this program, you push buttons, and the buttons add pre-made code. When you enter new lines, like #top, you don't write each letter, the program enters it at once, and my program checks the text from top to toe again.

The strange thing is, if I only have one element, it works, when I add more, it fails.
User is offlineProfile CardPM
+Quote Post

maffelu
RE: Richtextbox Coloring All My Text, Ack...
22 Aug, 2008 - 09:31 PM
Post #5

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 37


My Contributions
[SOLVED...]

Ok, so my problem was that I had to reblacken all the text before every loop.
Also, I now put all the keywords in an arraylist before going through it.

CODE
private void brMain_TextChanged(object sender, EventArgs e)
        {

            ArrayList a1 = new ArrayList();
            string UseWord;
            int Starter = 0;
            int midler = 0;
            int Ender = 0;


            do
            {
                

                Starter = brMain.ToString().IndexOf("#", Starter);
                
                if (Starter < 0)
                { break; }

                Ender = brMain.ToString().IndexOf("{", Starter);
                midler = Ender - Starter;
                UseWord = brMain.ToString().Substring(Starter, midler);
                a1.Add(UseWord);
                Starter = Ender;
            }
            while (Starter > 0);

            brMain.SelectionStart = 0;
            brMain.SelectionLength = brMain.ToString().Length;
            brMain.SelectionColor = Color.Black;

            foreach (string itemz in a1)
            {
                if(brMain.ToString().IndexOf(itemz)>0)
                {
                    int myPos = brMain.Find(itemz);
                    brMain.SelectionStart = myPos;
                    brMain.SelectionLength = itemz.Length;
                    brMain.SelectionColor = Color.Red;
                }
            }

        }


Thanks for the support!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 06:20AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month