【问题标题】:How would I create a "Find Form" Like in Notepad/Notepad++? C#我将如何在记事本/记事本++中创建“查找表单”? C#
【发布时间】:2019-10-20 01:59:47
【问题描述】:

我目前正在制作一个类似记事本的程序,但我很难过。

我需要制作“查找字母/单词”表格。我已经弄清楚了,但我似乎无法让它将RichTextBox SelectionBackColor 恢复为默认值,例如Color.White;

我的当前代码,在这里:

这是Find ButtonForm2.cs

    public static void Find(RichTextBox rtb,String word, Color color)
    {
        if(word=="")
        {
            return;
        }
        int s_start = rtb.SelectionStart, startIndex = 0, index;
        while((index=rtb.Text.IndexOf(word,startIndex))!=-1)
        {
            rtb.Select(index, word.Length);
            rtb.SelectionColor = color;
            startIndex = index + word.Length;
        }
        rtb.SelectionStart = s_start;
        rtb.SelectionLength = 0;
        rtb.SelectionColor = Color.White;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Find(richtext, textBox1.Text, Color.Blue);
    }

不要介意引用(rtb 等)

我的问题是:起初它工作正常,但如果你删除原始的“找到”文本,那么SelectionColor 变成了“找到”文本的SelectionColor 有人有解决办法吗?

【问题讨论】:

  • 您有多种选择。例如: 1) 保护所选内容(请参阅RichTextBox.SelectionProtected),因此无法修改文本(直到您决定可以修改)。 2)当文本被修改(KeyDown、KeyPress、TextChanged 等)时,删除所有当前选择。 3)以上混合。考虑到可以以不同方式考虑和处理 Selection 和 HighLight。 HighLight 可以是非持久的(例如,由TextRenderer.DrawText 生成)。控件失效后立即清除。
  • 嗯... No.2 听起来不错,但仍然...有没有办法只修改我发布的代码,而不是这么长?呵呵,我想我只是在这方面生疏了...... :)
  • 其实2号和1号帮了我很多。如果你把它作为一个答案,我会标记它,:D
  • 这只是一个建议,我不知道你实现了什么。您可以自己发布答案。关于第三点,我的意思是这样的:How to highlight wrapped text in a control using the graphics?(参见与 RichTextBox 相关的部分)或this(用 VB.Net 编写,但重要的是 概念)。
  • 您可能还对此处显示的方法感兴趣:How to justify text in a label。除了标签控件,它还展示了如何设置自定义 RichTextBox,启用文本对齐(从单个段落到整个文本),重新激活本机高级排版选项(.Net RichTextBox 控件实际上可以自行对齐文本,它只需要一个push)。

标签: c#


【解决方案1】:

首先,我想提一下,Richtextbox 默认选择颜色是黑色而不是白色。

其次,你可以试试下面的代码做你想做的事。

  private void button1_Click(object sender, EventArgs e)
        {
            Find(richTextBox1, textBox1.Text, Color.Blue);
        }

    public static void Find(RichTextBox rtb, String word, Color color)
    {
        rtb.SelectionStart = 0;
        rtb.SelectionLength = rtb.TextLength;
        rtb.SelectionColor = Color.Black;
        if (word == "")
        {
            return;
        }
        int s_start = rtb.SelectionStart, startIndex = 0, index;
        while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
        {
            rtb.Select(index, word.Length);
            rtb.SelectionColor = color;
            startIndex = index + word.Length;
        }

    }
    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
        richTextBox1.SelectionColor = Color.Black;
    }

结果:

【讨论】:

  • 这很好,但我有一个问题——这真的能解决问题吗?在我的 original 代码中,每当我“找到”一些文本时,我都会将其删除,然后如果我输入更多文本,它将是蓝色而不是原始颜色。我会测试这个:)
  • 这不起作用 :( 在我最终删除 RichTextBox 中的文本后,它只是蓝色的。有什么解决办法吗?:)
  • 我不确定你现在想要什么。你能举个例子来说明你的意思吗?
  • 我的问题:基本上它很好,但是在我从RichTextBox 中删除搜索/突出显示的文本后,SelectionColor 保持蓝色。
  • 我已经更新了我的代码,你可以看看。您需要使用选择更改事件。
【解决方案2】:

根据你的新描述,我建议你可以再添加一个按钮,让它恢复原来的颜色。

代码:

private void button1_Click(object sender, EventArgs e)
        {
            Find(richTextBox1, textBox1.Text, Color.Blue);
        }

        public static void Find(RichTextBox rtb, String word, Color color)
        {
            rtb.SelectionStart = 0;
            rtb.SelectionLength = rtb.TextLength;
            rtb.SelectionColor = Color.Black;
            if (word == "")
            {
                return;
            }
            int s_start = rtb.SelectionStart, startIndex = 0, index;
            while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
            {
                rtb.Select(index, word.Length);
                rtb.SelectionColor = color;
                startIndex = index + word.Length;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectionStart = 0;
            richTextBox1.SelectionLength = richTextBox1.TextLength;
            richTextBox1.SelectionColor = Color.Black;
        }

像下面这样:

【讨论】:

  • 我可以不使用按钮来执行此操作吗?我正在尝试制作记事本,我需要它以编程方式而不是使用另一个按钮:D 有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多