本文介绍了为什么不能覆盖RichTextBox.SelectionStart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些关键字中创建了一个DLL文件,例如:#include,#define,int,return......



这是C关键字,但我在谈论C#。



我会告诉你我的完整代码。对不起,我不知道确切的问题在哪里。

这是我的代码:

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:

// 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);
                    }
                }
            }
        }



数据类型,语句,声明,libReference,functionRef是包含关键字的数组字符串。



我检测到的一些问题:

1 /。当显示RichTextBox时,我输入了2行。

第1行是:#includestdio.h<< ==此行显示为真。

Line 2是:#includeconio.h<< == conio.h上面是RoyalBlue,但它仍然是黑色的#include



为什么?



2 /。我无法高速输入文字。因为数据会丢失。

当我尝试时,光标出现在所有已经关键字并删除它。



3 /。当我输入int时,它是蓝色的。没关系。之后,我按Backspace删除它。 in仍为蓝色。

数据未更新。为什么?



你能告诉我为什么以及如何解决这个问题?我正在使用C#Winform和.Net 4.5

对不起我很长的问题。



谢谢!


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!

推荐答案


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;
}

我建议你运行它,观察发生了什么,改变它以实现不同的逻辑;使用它作为试验台来设计你的战略以获得有效的Rtf副本。

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.


这篇关于为什么不能覆盖RichTextBox.SelectionStart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:32
查看更多