请教C#类似于VS的查找功能

茗香淡然 2014-08-22 08:22:03
突然心血来潮,想弄一下类似 VS 的查找功能.....
才发现搞来搞去却把自己绕进去了.....各位大大帮着优化一下

就是下面的 private static void fileFindDown(RichTextBox txt,TextBox txtb, string FindString) 方法

注意: 这个方法不是从上到下查找, 而是可以从中间找到结尾,然后再从开始找到原来的位置, 也可以从开始找到结尾
控件: RichTextBox Name = txtInput
查找 TextBox Name = txt_Find 查找 button Name = but_Find


#region 全局变量 
/// <summary> 记录原始光标位置 </summary>
static int s = -1;
/// <summary> 字符长度添量 </summary>
static int Index = 0;
/// <summary> 过度字符串变量 </summary>
static string str = string.Empty;
/// <summary> 判断字符串是否更改 </summary>
static string strE = string.Empty;
/// <summary> 计算数量 </summary>
static int count = 0;
/// <summary> 实例化富文本 </summary>
static RichTextBox r = new RichTextBox();
#endregion

 
//窗口加载
private void Form1_Load(object sender, EventArgs e)
{
txtInput.ReadOnly = true;
string filePath = "myfile.txt";
FetchFile(filePath);
FindDown.Checked = true;
}


//加载文件 myfile.txt 我放在程序目录 (随意打的几个字母)
private void FetchFile(string path)
{
FileStream fs = new FileStream(path,
FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, Encoding.Default);
txtInput.Text = sr.ReadToEnd();
txtInput.Focus();
fs.Close();
sr.Close();
}


  //查找 button
private void but_Find_Click(object sender, EventArgs e)
{
if (FindDown.Checked)
//fileFindDown(txtInput, txt_Count,txt_Find.Text);
fileFindDowns();
else
fileFindUp(txtInput, txt_Find.Text);
}

上面的FindDown 是两个 radioButton (上下查找方向)

下面这是重点

/// <summary> 类似 VS 的查找方法(不区分大小写向下查找) </summary>
/// <param name="txt">RichTextBox</param>
/// <param name="txtb">TextBox</param>
/// <param name="FindString">查找字符串</param>
private static void fileFindDown(RichTextBox txt,TextBox txtb, string FindString)
{
if (s < 0)
{
s = txt.SelectionStart;
if (s > 10 && s + FindString.Length <= txt.Text.Length)
{
str = txt.Text.Substring(0, s + FindString.Length - 1);
Index = txt.SelectionStart;
r.Text = str;
}
else
{
str = txt.Text;
r.Text = txt.Text;
Index = txt.SelectionStart;
}
}
if (Index < str.Length - (FindString.Length - 1) && Index >= 0)
{
Index = r.Find(FindString, Index, RichTextBoxFinds.None);
if (Index < 0 || Index > r.Text.Length)
{
seeks(FindString);
count = 0;
Index = 0;
s = -1;
}
else
{
txt.Select(Index, FindString.Length);
txt.SelectionColor = Color.Red;
txt.Focus();
count++;
txtb.Text = count.ToString();//

Index += FindString.Length;
if (Index > str.Length - FindString.Length)
{
seeks(FindString);
//txt.SelectionStart = 0;
//txt.Select(txt.SelectionStart, 0);
s = -1;
Index = 0;
count = 0;
}
}
}
else if (Index < txt.Text.Length && Index >= 0)
{
Index = txt.Find(FindString, Index, RichTextBoxFinds.None);
if (Index == -1)//(Index >= txt.Text.Length && Index <= 0)
{
if (r.Text != "")
{
Index = 0; Index = r.Find(FindString, Index, RichTextBoxFinds.None);
if (Index < 0 || Index > r.Text.Length)
{
seeks(FindString);
count = 0;
Index = 0;
s = -1;
}
else
{
txt.Select(Index, FindString.Length);
txt.SelectionColor = Color.Red;
txt.Focus();
Index += FindString.Length;
count++;
txtb.Text = count.ToString();

if (Index > str.Length - FindString.Length)
{
seeks(FindString);
//txt.SelectionStart = 0;
//txt.Select(txt.SelectionStart, 0);
s = -1;
Index = 0;
count = 0;
}
}
}
else
{
seeks(FindString);
s = -1;
Index = 0;
count = 0;
}
}
else
{
txt.Select(Index, FindString.Length);//在RichTextBox控件中搜索关键字
Index += FindString.Length;//递增标识查询关键字的初始长度
txt.SelectionColor = Color.Red;//设定关键字为红色
txt.Focus();
count++;
txtb.Text = count.ToString();
}
}
else
{
Index = 0; Index = r.Find(FindString, Index, RichTextBoxFinds.None);
if (Index < r.Text.Length - FindString.Length && Index >= 0)
{
r.Select(Index, FindString.Length);
txt.Select(Index, FindString.Length);
txt.SelectionColor = Color.Red;//设定关键字为红色
Index += FindString.Length;//递增标识查询关键字的初始长度
txt.Focus();
count++;
txtb.Text = count.ToString();
}
else
{
seeks(FindString);
s = -1;
Index = 0;
count = 0;
}
}
}

相对于上面的方法,下面这个简单得无法直视

/* 简单查找方法,从上而下一直查找. */
private void fileFindDowns()
{
string FindStr = txt_Find.Text;
Index = txtInput.Find(FindStr, Index, RichTextBoxFinds.None);
if (Index < 0 || Index > txtInput.Text.Length)
{
seeks(FindStr);
start = 0;
Index = 0;
}
else
{
txtInput.Select(Index, FindStr.Length);
txtInput.SelectionBackColor = Color.Red;
Index += FindStr.Length;
txtInput.Focus();
count++;
}
}

...全文
686 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
茗香淡然 2014-08-26
  • 打赏
  • 举报
回复
引用 8 楼 resigner 的回复:
Mark 一下。。。正对我有用
引用 10 楼 u014362090 的回复:
以前也想做一个这种功能的,最后因复杂没做下去,Mark一下
这功能只是草草写出来的,还没有完善,后面可以自己完善一下, Index += FindStr.Length; //查找方法中的这个可以去掉.


        public static void replaces(RichTextBox rtxt, string FindString, string ReplString)
        {
            //string txtSt = rtxt.Text.Substring(Index,FindString.Length);
            string txtSt = rtxt.SelectedText;
            if (FindString.Equals(txtSt))
            {
                int fs = FindString.Length;
                int rs = ReplString.Length;
                int cs = fs - rs;
                rtxt.SelectionLength = FindString.Length;
                rtxt.SelectionColor = Color.Coral;
                rtxt.SelectedText = ReplString;//textBox2中放要替换的字符  
                rtxt.Focus();
                MIndex = -1;
                rtxt.SelectionStart += cs;
            }
             replFind(rtxt, FindString, ReplString);
        }
jiawaziaixialing 2014-08-26
  • 打赏
  • 举报
回复
以前也想做一个这种功能的,最后因复杂没做下去,Mark一下
漫天雪飞 2014-08-26
  • 打赏
  • 举报
回复
好多if else 看着就头疼
没花鹿 2014-08-26
  • 打赏
  • 举报
回复
Mark 一下。。。正对我有用
茗香淡然 2014-08-26
  • 打赏
  • 举报
回复
引用 6 楼 zhi_ai_yaya 的回复:
[quote=引用 1 楼 caozhy 的回复:] 大致看了下,思路正确,没什么可以优化的。 代码虽然多,但是都是辅助功能的代码,要精简它们,除非这些功能你不想要了。
达不到斑竹的境界,大概看一下就可以看清楚思路 我只是知道有个经典的KMP字符串模式匹配算法[/quote] KMP算法看得太挠头....有机会再细细学习一下.
我叫小菜菜 2014-08-25
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
大致看了下,思路正确,没什么可以优化的。 代码虽然多,但是都是辅助功能的代码,要精简它们,除非这些功能你不想要了。
达不到斑竹的境界,大概看一下就可以看清楚思路 我只是知道有个经典的KMP字符串模式匹配算法
茗香淡然 2014-08-25
  • 打赏
  • 举报
回复
加入这个完善解决,太笨了....早应该想到了....

                int fs = FindString.Length;
                int rs = ReplString.Length;
                int cs = fs - rs;

                rtxt.SelectionStart += cs;

        public static void replaces(RichTextBox rtxt, string FindString, string ReplString)
        {
            //string txtSt = rtxt.Text.Substring(Index,FindString.Length);
            string txtSt = rtxt.SelectedText;
            if (FindString.Equals(txtSt))
            {
                int fs = FindString.Length;
                int rs = ReplString.Length;
                int cs = fs - rs;
                rtxt.SelectionLength = FindString.Length;
                rtxt.SelectionColor = Color.Coral;
                rtxt.SelectedText = ReplString;//textBox2中放要替换的字符  
                rtxt.Focus();
                MIndex = -1;
                rtxt.SelectionStart += cs;
                replFind(rtxt, FindString, ReplString);
            }
            else replFind(rtxt, FindString, ReplString);
        }
虽然解决了,太还是达不到 VS 那种替换效果..........
茗香淡然 2014-08-25
  • 打赏
  • 举报
回复
茗香淡然 2014-08-25
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
大致看了下,思路正确,没什么可以优化的。 代码虽然多,但是都是辅助功能的代码,要精简它们,除非这些功能你不想要了。
引用 2 楼 xiangxinzijiwonen 的回复:
8错

        //单次替换
        public static void replaces(RichTextBox rtxt, string FindString, string ReplString)
        {
            string txtSt = rtxt.SelectedText;
            if (FindString.Equals(txtSt))
            {
                rtxt.SelectionLength = FindString.Length;
                rtxt.SelectionColor = Color.Coral;
                rtxt.SelectedText = ReplString;//textBox2中放要替换的字符  
                rtxt.Focus();
                s = -1;
                fileFindDown(rtxt, FindString);   //查找(那个TextBox 不要了,不是必须的)
            }
            else fileFindDown(rtxt, FindString); //查找
        }
替换会遇到一个问题, 如: 查找(F) 替换(Ho~) 如果2个 F 之间不超出替换字符的 Length ,那么它就会跳过第二个F 查找第三个F,然后找到结尾再找第二个F.现在头脑有点乱乱的,想不出来了...请帮帮忙
ZhongGuanYao 2014-08-23
  • 打赏
  • 举报
回复
8错
threenewbee 2014-08-22
  • 打赏
  • 举报
回复
大致看了下,思路正确,没什么可以优化的。 代码虽然多,但是都是辅助功能的代码,要精简它们,除非这些功能你不想要了。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧