C# 在RichTextBox的TextChange事件中计算日文文本长度时出现性能问题

C# 在RichTextBox的TextChange事件中计算日文文本长度时出现性能问题,c#,winforms,arrays,performance,richtextbox,C#,Winforms,Arrays,Performance,Richtextbox,我用C#2.0编写了自定义richtextbox。用户将在此文本框中输入日语文本。当用户键入文本大小超过300时,它会用黄色突出显示额外的字符。日语文本可以包含半宽和全宽字符,我想将半宽字符计算为大小“0.5”,将全宽字符计算为大小“1” 下面是我的代码 Dictionary<int, float> charSizes = new Dictionary<int, float>(); void HighlightingTextBox_TextChanged(object

我用C#2.0编写了自定义richtextbox。用户将在此文本框中输入日语文本。当用户键入文本大小超过300时,它会用黄色突出显示额外的字符。日语文本可以包含半宽和全宽字符,我想将半宽字符计算为大小“0.5”,将全宽字符计算为大小“1”

下面是我的代码

Dictionary<int, float> charSizes = new Dictionary<int, float>();

void HighlightingTextBox_TextChanged(object sender, EventArgs e)
{
    int index = this.SelectionStart;

    // Reset highlighting and font.
    HighlightText(0, this.BackColor, true);

    // Highlight Text

    float charCount = 0;
    int highlightIndex = 0;

    string currentText = this.Text;

    for (int k = 0; k < currentText.Length; k++)
    {
        int c = currentText[k];

        if (charCount <= CharacterLimit)
        {
            if (charSizes.ContainsKey(c)) // Use already calculated Size
                charCount = charCount + charSizes[c];
            else
            {
                // Check if character is Half width or Full Width
                string charString = currentText[k].ToString();
                string fullChar = Microsoft.VisualBasic.Strings.StrConv(charString, Microsoft.VisualBasic.VbStrConv.Wide, 1041);

                if (c == (int)fullChar[0])
                {
                    // Ascci value matches after converting to full width. So its Full width char.
                    charCount++;
                    charSizes[c] = 1;
                }
                else
                {
                    charCount = charCount + 0.5f;
                    charSizes[c] = 0.5f;
                }
            }

            highlightIndex++;
        }

        // Enforce "Arial" font for English characters in Japanese text
        this.Select(k, 1);
        this.SelectionFont = (c < 128) ? new Font("Arial", 9.5f) : this.Font;
        this.SelectionLength = 0;
    }

    if (charCount > CharacterLimit)        
        HighlightText(highlightIndex, HighlightColor, false);

    this.SelectionStart = index;
}


private void HighlightText(int selectionStart, Color highlightColor, bool enforceFont)
{
    this.Select(selectionStart, this.Text.Length);
    this.SelectionBackColor = highlightColor;

    if (enforceFont)
        this.SelectionFont = this.Font;

    this.SelectionLength = 0;
}
Dictionary charsize=new Dictionary();
void HighlightingTextBox\u TextChanged(对象发送方,事件参数e)
{
int index=this.SelectionStart;
//重置突出显示和字体。
HighlightText(0,this.BackColor,true);
//突出显示文本
浮点数=0;
int highlightIndex=0;
字符串currentText=this.Text;
for(int k=0;k
有什么问题吗?

此代码在前8到10个字符中正常工作。但在这之后,它的工作速度非常慢(在处理文本的“每个”字符时需要很多时间)。如果用户键入的字符很快,则需要几秒钟才能在UI上显示新字符


在这种情况下,如何提高性能?有没有其他方法进行此计算?

在这种例程中获得性能的最简单方法:只计算更改的内容

只需比较新旧文本,查看添加或删除的字符,并根据它们修改总数

你永远不会用这种方式一次计算超过一个

-- 此外,缓存您的Arial 9.5字体,而不是每次创建一个新字体


为了最大限度地减少对用户键入的干扰,只在用户暂停设置的时间间隔时更新

    private Timer _timer = new System.Windows.Forms.Timer();
    private bool _processingText;
    public MyRichTextBox()
    {
        _timer = new Timer();
        _timer.Interval = 1000;
        _timer.Tick += new EventHandler(ProcessText);
    }


    protected override void OnTextChanged(EventArgs e)
    {
        if (_processingText == false)
        {
            _timer.Stop();
            _timer.Start();
        }
    }

    private void ProcessText(object sender, EventArgs e)
    {
        _processingText = true;
        _timer.Stop();

        // Insert your processing logic here

        _processingText = false;
    }

在这种例程中获得性能的最简单方法是:只计算更改的内容

只需比较新旧文本,查看添加或删除的字符,并根据它们修改总数

你永远不会用这种方式一次计算超过一个

-- 此外,缓存您的Arial 9.5字体,而不是每次创建一个新字体


为了最大限度地减少对用户键入的干扰,只在用户暂停设置的时间间隔时更新

    private Timer _timer = new System.Windows.Forms.Timer();
    private bool _processingText;
    public MyRichTextBox()
    {
        _timer = new Timer();
        _timer.Interval = 1000;
        _timer.Tick += new EventHandler(ProcessText);
    }


    protected override void OnTextChanged(EventArgs e)
    {
        if (_processingText == false)
        {
            _timer.Stop();
            _timer.Start();
        }
    }

    private void ProcessText(object sender, EventArgs e)
    {
        _processingText = true;
        _timer.Stop();

        // Insert your processing logic here

        _processingText = false;
    }

好的。但是要计算highlightIndex,我仍然需要遍历每个字符来计算总大小,因为字符的大小不同。好的。但是要计算highlightIndex,我仍然需要遍历每个字符来计算总大小,因为字符的大小不同。