为什么不能覆盖RichTextBox.SelectionStart? [英] Why can't RichTextBox.SelectionStart be overridden?

查看:65
本文介绍了为什么不能覆盖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!

推荐答案

你采取了一种非常天真的方法,因为它不能很好地扩展。您键入的文档越长,着色代码就越慢,因为它必须扫描越来越多的文本。



阅读这个 [ ^ ]。
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[^].


没有必要覆盖任何方法在(或子类)这里我可以看到RichTextBox。



除了Dave在这里给你的信息,我想补充一下:



0.正如Dave指出的那样:你想要处理整个文件,然后写入Rtf。



1.有一个更好的替代方式使用剪贴板



我建议你设置一个这样的实验:



1.在WinForm上放置两个RichTextBox控件



2.设置初始文本属性o f'richTextBox1 to:



你最喜欢哪种颜色:红色还是蓝色?



3.在表单上放置一个按钮,'button1,并将其连接到此Click EventHandler:
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:
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆