簡體   English   中英

C#如何編程查找功能

[英]C# How to program find function

我有一個利用find函數的C#程序,但是它能夠找到單詞,但是不會在richTextBox中突出顯示找到的單詞。

有人可以給我建議的代碼嗎?

謝謝。

查找功能類形式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Syscrawl
{
public partial class Find_Form : Form
{
    FTK_Menu_Browsing_History fmbh = new FTK_Menu_Browsing_History();

    public Find_Form()
    {
        InitializeComponent();
    }

    public void searchButton_Click(object sender, EventArgs e)
    {
        string s1 = fmbh.getSearchBrowsing().ToLower();
        string s2 = textBoxSearch.Text.ToLower();

        if (s1.Contains(s2))
        {
            MessageBox.Show("Word found!");

            this.fmbh.richTextBoxBrowsing.Find(s2);
            this.fmbh.richTextBoxBrowsing.SelectionLength = s2.Length;
            this.fmbh.richTextBoxBrowsing.SelectionColor = Color.Red;
            this.Close();
        }
        else
        {
            MessageBox.Show("Word not found!");
        }
    }
}
}

您需要先選擇要查找的內容。 這個:

int offset = s1.IndexOf(s2);
richTextBox1.Select(offset, s2.Length);

之后,您可以進行整個突出顯示。 另一個提示,為防止選擇過程中出現閃爍,請在您的表單中使用以下代碼:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0) {
        if (!_doPaint)
            return;
    }

    base.WndProc(ref m);
} 

在選擇任何內容之前,將_doPaint設置為false,然后在選擇之后將其設置為true。

希望我能幫忙!

您需要調用s1.IndexOf(s2, StringComparison.CurrentCultureIgnoreCase)來查找匹配項的位置。

另外,看起來您的“查找”表單創建了自己的“歷史記錄”表單實例; 它不使用現有實例。
您應該考慮接受構造函數參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM