Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a DLL file within some keywords, like this: "#include ", "#define ", "int ", "return "...

It's C keywords, but I'm talking about C#.

I will show you my full code. Sorry, I don't know exactly where is the problem.
Here is my code:
C#
// Create "Virtual RichTextBox" to set color, copy and paste string
RichTextBox rtb = new RichTextBox();
        object[] total;

        private void CallingFirst()
        {
            kwlib.kwlib kw = new kwlib.kwlib();

            string[] datatypes = kw.DataTypes;
            string[] statements = kw.Statements;
            string[] declaration = kw.Declaration;
            string[] libReference = kw.LibReference;
            string[] functionRef = kw.FunctionRef;

            object[] o = new object[] 
            { declaration, libReference, datatypes, statements, functionRef };

            this.total = o;
        }

        private void SettingColor(string[] temp, int i)
        {
            int index = WritingRTB.SelectionStart;
            Color colour = WritingRTB.SelectionColor;

            // Wanted string position
            int start = WritingRTB.Text.IndexOf(temp[i]);
            // Select it
            WritingRTB.Select(start, temp[i].Length);

            if ((temp != total[0]) && (temp != total[1]))
            {
                rtb.ForeColor = Color.Blue;
            }
            else // Use this color for #include #define stdio.h conio.h
            {
                rtb.ForeColor = Color.RoyalBlue;
            }

            this.FormatingTB();

            WritingRTB.Focus();
            WritingRTB.SelectionStart = start; // Setting cursor position to paste
            WritingRTB.Paste();
            
            WritingRTB.SelectionStart = index; // Cancel selecting keyword
            WritingRTB.SelectionColor = colour; // Cancel drawing color
        }
        
        private void FormatingTB() 
        {
            rtb.Font = new System.Drawing.Font("Times New Roman", 12);
            rtb.Text = WritingRTB.SelectedText; // Copy the keyword into rtb
            WritingRTB.SelectedText = String.Empty;
            rtb.SelectAll();
            rtb.Copy();
        }
        
        private void CheckingKeywords()
        {
            object[] ob = this.total;

            foreach (string[] temp in ob)
            {
                for (int i = 0; i < temp.Length; i++)
                {
                    if (WritingRTB.Text.Contains(temp[i]))
                    {
                        this.SettingColor(temp, i);
                    }
                }
            }
        }

datatypes, statements, declaration, libReference, functionRef are array strings which contain keywords.

Some problems I had detected:
1/. When RichTextBox was shown, I typed 2 lines.
Line 1 was: #include "stdio.h" <<== This line was colored true.
Line 2 was: #include "conio.h" <<== conio.h was colored RoyalBlue, but it's still Black for #include

Why?

2/. I can't type text with high speed. Because the data will be lost.
When I tried doing, the cursor was appeared at "all already keywords" and deleted it.

3/. When I typed "int ", it was blue. That's ok. After that, I press Backspace to delete it. "in" was still blue.
Data was not updated. Why?

Can you tell me why and how to fix it? I'm using C# Winform with .Net 4.5
Sorry for my very long question.

Thank you!
Posted
Comments
user8x86 13-Dec-14 16:15pm    
I put it in TextChanged event.

Your taking a very naive approach to this as it doesn't scale well. The longer the document you type, the slower your coloring code becomes because it has to scan over more and more text.

Read this[^].
 
Share this answer
 
There is no need to over-ride any method in (or sub-class) the RichTextBox here that I can see.

In addition to the information that Dave gave you here, I'd like to add:

0. as Dave pointed out: you want to process the entire file, and then write the Rtf.

1. there is a better alternative to using the Clipboard

I suggest you set up an experiment like this:

1. put two RichTextBox Controls on a WinForm

2. set the initial Text Property of 'richTextBox1 to:

"Which color do you like best: red, or blue ?"

3. put a Button on the Form, 'button1, and wire-it up to this Click EventHandler:
C#
private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.SelectionStart = 6;
    richTextBox1.SelectionLength = 5;
    richTextBox1.SelectionColor = Color.DeepSkyBlue;

    richTextBox2.SelectionStart = richTextBox1.SelectionStart;

    // copy the entire Rtf
    richTextBox2.SelectedRtf = richTextBox1.Rtf;

    richTextBox1.SelectionStart = 24;
    richTextBox1.SelectionLength = 4;
    richTextBox1.SelectionColor = Color.Red;

    richTextBox2.SelectionStart = richTextBox1.SelectionStart;
    richTextBox2.SelectionLength = richTextBox1.SelectionLength;

    // copy only the selected Rtf
    richTextBox2.SelectedRtf = richTextBox1.SelectedRtf;
}
I suggest you run this, observe what happens, change it to implement a different logic; use it as a "test-bed" to design your strategy for an efficient copy of Rtf.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900