关闭

C# richTextBox 控件使用说明

486人阅读 评论(0) 收藏 举报
分类:


查找richTextBox 字符串:

 该方法是可以实现查询字符串,但多个就不成功了
string s="";
int index = this.richTextBox1.Find(s);
if (index < 0)
{
    MessageBox.Show("查找不到");
    return;
}
this.richTextBox1.SelectionStart = index;
this.richTextBox1.SelectionLength = s.Length;
this.richTextBox1.Focus();


查找richTextBox 支持上下 首尾查询

定义类

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace vjsdn_tester
{
   //操作RichTextBox控件的类. by www.csframework.com 
   public class RichTextBoxTool
   {
      /// <summary> 
      /// 自己定义查找方法.参数content是要查询的内容. 
      /// type是查询条件.如:从文件头部向下查询,从文件尾部向上查询. 
      /// 如type= RichTextBoxFinds.None 表示从文件头部向下查询. 
      /// </summary> 
      public static void FindText(RichTextBox rich, string content, RichTextBoxFinds options)
      {
         int startIndex;
         int endIndex;
         
         if ((options & RichTextBoxFinds.Reverse) == RichTextBoxFinds.Reverse)
         {
            startIndex = 0;
            endIndex = rich.SelectionStart;
         }
         else
         {
            startIndex = rich.SelectionStart + rich.SelectionLength;
            endIndex = rich.Text.Length;
         }
         
         int index = rich.Find(content, startIndex, endIndex, options);
         
         if (index >= 0) //如果找到
         ShowSelection(rich, index, content.Length);
         else
         MessageBox.Show("Not found!");
      }
      
      //查找第一个 
      public static void FindFirst(RichTextBox rich, string content)
      {
         int index = rich.Find(content, 0);
         if (index >= 0) ShowSelection(rich, index, content.Length);
      }
      
      //查找最后一个 
      public static void FindLast(RichTextBox rich, string content)
      {
         int index = rich.Find(content, rich.Text.Length, RichTextBoxFinds.Reverse);
         if (index >= 0) ShowSelection(rich, index, content.Length);
      }
      
      //选择搜索到的文本 
      private static void ShowSelection(RichTextBox rich, int index, int length)
      {
         rich.SelectionStart = index;
         rich.SelectionLength = length;
         rich.SelectionColor = Color.Red;
         rich.Focus();
      }
      
   }
}

执行代码


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

namespace vjsdn_tester
{
   public partial class frmRichboxSearher : Form
   {
      public frmRichboxSearher()
      {
         InitializeComponent();
      }
      
      //查找下一个单词
      private void btnFindNext_Click(object sender, EventArgs e)
      {
         string content = txtContent.Text;
         RichTextBoxTool.FindText(this.richTextBox1, content, RichTextBoxFinds.None);
      }
      
      //查找第一个单词
      private void btnFindFirst_Click(object sender, EventArgs e)
      {
         string content = txtContent.Text;
         RichTextBoxTool.FindFirst(this.richTextBox1, content);
      }
      
      //查找上一个单词
      private void btnFindPrior_Click(object sender, EventArgs e)
      {
         string content = txtContent.Text;
         RichTextBoxTool.FindText(this.richTextBox1, content, RichTextBoxFinds.Reverse);
      }
      
      //查找最后一个单词
      private void btnFindLast_Click(object sender, EventArgs e)
      {
         string content = txtContent.Text;
         RichTextBoxTool.FindLast(this.richTextBox1, content);
      }
   }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11668次
    • 积分:212
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:11篇
    • 译文:0篇
    • 评论:0条
    文章分类