简体   繁体   English

当离开richtextbox时,所有文本颜色更改为第一个字符颜色

[英]when leave richtextbox all text color changed to the first character color

在我的C#WinForm项目上,当我离开richtextbox并输入另一个组件时,richtextbox颜色的所有文本都更改为第一个字符颜色。

谢谢所有人,但我绑定了富文本框并使用数据绑定填充了它,然后我在设置颜色后使用此空隙v_srarch_WithOutTashkilAll为某些单词设置了颜色,如果我使用鼠标单击将富文本框控件留给其他控件,则所有文本都以富色使用第一个字符重新着色的文本框

this void created to find all word in rich text box named rtb and change word color to red it's work right but if i leaved rich text box control to other control all character color be the first character color this is my proplem 创建此空缺以在名为rtb的富文本框中找到所有单词并将单词颜色更改为红色,这是正确的,但是如果我将富文本框控件留给其他控件,则所有字符颜色均为第一个字符颜色,这是我的建议

public void v_srarch_WithOutTashkilAll(string word, RichTextBox rtb) { 公共无效v_srarch_WithOutTashkilAll(字符串,RichTextBox rtb){

        try
        {

            if (startSelection >= 0)
            {
                startSelection = rtb.Find(word, startSelection, rtb.TextLength, RichTextBoxFinds.None);
            }

            if (startSelection != -1)
            {
                char[] arr = new char[] { 'ّ', 'َ', 'ً', 'ُ', 'ٌ', 'ِ', 'ٍ', 'ْ' };


                int index = 0;
                string CurrentWord = "";
                char charForTest;
                try
                {
                    while (CurrentWord != word)
                    {
                        rtb.Select(startSelection + index, 1);
                        charForTest = Convert.ToChar(rtb.SelectedText);
                        if (arr.Contains(charForTest))
                        {
                            index++;

                        }
                        else
                        {
                            CurrentWord += charForTest.ToString();
                            index++;
                        }
                    }
                    rtb.Select(startSelection, index);
                    rtb.SelectionColor = System.Drawing.Color.Red;
                    startSelection += index;
                    v_srarch_WithOutTashkilAll(word, rtb);
                }
                catch (Exception)
                { }

            }
        }
        catch (Exception)
        { }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM