所以我在做一个文本框,我想如果当用户输入某些单词时,它们会突出显示,那将是很酷的. 我用RichTextBox做了一个函数,但它的 colored颜色 只有它应该显示的文本的一半.有时它甚至不起作用.

是一种给单词的每一个出现点缀色彩的方法吗?

我的代码是:

private void ColorLines(RichTextBox text)
{
    string[] words = { "example","hi" };

    foreach (string s in text.Text.Split(' '))
    {
        int startIndex = text.Text.IndexOf(s);
        int length = s.Length;

        if (words.Contains(s))
        {
            text.Select(startIndex, length);
            text.SelectionColor = Color.Red;
        }
    }
}

我把上面的 playbook 重写了好几遍,都比另一个更糟糕. 我看过其他帖子有同样的问题,但它是或在VB或在WPF,或者它是不同的东西. 我正在寻找一种有效的方法来使用WinForms.

推荐答案

This还会在其他单词中显示某个单词的实例.因此,我不知道这是否适用于您需要它的目的,但除此之外,它应该能实现您想要的效果.

//I don't usually work with WinForms so idk if this is the correct way
//to do this or if you can use "sender" or "e" instead of "richTextBox1"
private void richTextBox_TextChanged(object sender, EventArgs e)
{
    int tempint = richTextBox1.SelectionStart;
    richTextBox1.Select(0, richTextBox1.TextLength);
    richTextBox1.SelectionColor = Color.Black;
    richTextBox1.Select(tempint, 0);
    ColorLines(richTextBox1);
}

private void ColorLines(RichTextBox text)
{
    string[] words = { "example", "hi"};

    foreach (string word in words)
    {
        if (text.Text.Contains(word))
        {
            int index = -1;
            int curselected = text.SelectionStart;

            while ((index = text.Text.IndexOf(word, (index + 1))) != -1)
            {
                text.Select(index, word.Length);
                text.SelectionColor = Color.Red;
                text.Select(curselected, 0);
                text.SelectionColor = Color.Black;
            }
        }
    }
}

灵感来自this legends answer

enter image description here

这是另一个版本,它不会在其他单词中显示单词的实例,但当单词前面或后面有符号时,如","或"",则不起作用.没有空位.

    private void richTextBox_TextChanged(object sender, EventArgs e)
    {
        int tempint = richTextBox1.SelectionStart;
        richTextBox1.Select(0, richTextBox1.TextLength);
        richTextBox1.SelectionColor = Color.Black;
        richTextBox1.Select(tempint, 0);
        ColorLines(richTextBox1);
    }

    private void ColorLines(RichTextBox text)
    {
        string[] words = { "example", "hi" };
        int startindex = 0;

        foreach (string word2 in text.Text.Split(' ').ToArray())
        {
            foreach (string word in words)
            {
                if (word == word2)
                {
                    int curselected = text.SelectionStart;
                    int length = word2.Length;

                    text.Select(startindex, length);
                    text.SelectionColor = Color.Red;
                    text.Select(curselected, 0);
                    text.SelectionColor = Color.Black;
                }
            }
            startindex += word2.Length + 1;
        }
    }

enter image description here

Csharp相关问答推荐

EF Core:看不到任何查询日志(log)?

为什么xslWriter不总是按照xslWriterSet中指定的格式格式化该文档?

在. net毛伊岛窗口的深度链接已经创建""

实体框架核心上是否支持使用NPGSQL的字符串聚合?

为什么将鼠标悬停在DateTimeOffset上只显示Hour值?

如何返回具有泛型的类?

如何在同一成员上组合[JsonPropertyName]和[ObservableProperty]?

使用ASP.NET MVC for Lemon Squeezy X-Signature创建散列

匿名类型的AbstractValidator

此异步方法在重写方法中缺少等待运算符警告

为什么我在使用有效令牌的情况下仍未获授权?

如何将 colored颜色 转换为KnownColor名称?

为什么我的属性即使没有显式地设置任何[必需]属性,也会显示验证?

如何将行添加到DataGrid以立即显示它?

我可以阻止类型上的Object.ToString()吗?

为什么Visual Studio 2022建议IDE0251将我的方法设置为只读?

为什么我的UserControl没有加载到我的主窗口中?

我什么时候不应该在Dispose中调用EgSuppressFinalize(This)?

C#Web服务转换为 node /Express不工作

在SQL中删除少于24小时的令牌