zoukankan      html  css  js  c++  java
  • 如何用C#在winform中实现简单的查找功能

    以前写过一个web上的查找的例子,这次写了一个winform下的,当然还是要用正则表达式。一个窗口,上面有一个richTextBox,一个输入框,三个button:查找,前一个,后一个。

    主要代码:

    MatchCollection matches;

    int index = 0;

    private void button1_Click(object sender, EventArgs e)

    {

            this.index = 0;

            this.richTextBox1.HideSelection = false;

            Regex rx = new Regex(this.textBox1.Text,

            RegexOptions.Compiled | RegexOptions.IgnoreCase);

     

            matches = rx.Matches(this.richTextBox1.Text);

     

            btnNext_Click(sender, e);

    }

     

    private void btnNext_Click(object sender, EventArgs e)

    {

            if (matches.Count > 0 && index < matches.Count )

            {

                    Match match = matches[index];

                    int currentPos = match.Index;

                    this.richTextBox1.Select(currentPos, this.textBox1.Text.Length);

                    if (index == matches.Count - 1)

                    {

                            MessageBox.Show("end");

                    }

                    else

                    {

                            index += 1;

                    }

            }

    }

     

    private void btnPrev_Click(object sender, EventArgs e)

    {

            if (matches.Count > 0 && index >= 0 )

            {

                    Match match = matches[index];

                    int currentPos = match.Index;

                    this.richTextBox1.Select(currentPos, this.textBox1.Text.Length);

                    if (index == 0)

                    {

                            MessageBox.Show("start");

                    }

                    else

                    {

                            index -=1;

                    }

            }

    }

  • 相关阅读:
    高效存储过程分页
    c#函数参数
    MonoRail学习:可重复组件ViewComponents的使用
    跨域SSO的实现
    WebSockets基础
    NVelocity用法
    MonoRail MVC应用(2)-构建多层结构的应用程序
    MonoRail学习-入门实例篇
    关于transform属性导致字体模糊的问题
    在小程序中实现收缩展开
  • 原文地址:https://www.cnblogs.com/dahuzizyd/p/CSharp_Winform_Search.html
Copyright © 2011-2022 走看看