zoukankan      html  css  js  c++  java
  • C# RichTextBox的使用方法

    RichTextBox.Find方法
    RichTextBox控件不仅允许输入和编辑文本,同时还提供了标准 TextBox 控件未具有的、更高级的指定格式的许多功能。
    语法:RichTextBox
    说明:
    RichTextBox 提供了一些属性,对于本控件文本的任何部分,用这些属性都可以指定格式。为了改变文本的格式,首先要选定它。只有选定的文本才能赋予字符和段落格式。使用这些属性,可把文本改为粗体或斜体,或改变其颜色,以及创建上标和下标。通过设置左右缩进和悬挂式缩进,可调整段落的格式。
    RichTextBox 控件能以 rtf 格式和普通 ASCII 文本格式这两种形式打开和保存文件。可以使用控件的方法(LoadFile 和 SaveFile)直接读写文件,或使用与 Visual Basic 文件输入/输出语句联结的、诸如 SelRTF 和 TextRTF 之类的控件属性打开和保存文件。
    通过使用 OLEObjects 集合,RichTextBox 控件支持对象的嵌入。插入到控件中的每个对象,都代表 OLEObject 对象。用这样的控件,就可以创建包含其它文档或对象的文档。例如,可创建这样的文档,它有一个嵌入的 Microsoft Excel 电子数据表格、或 Microsoft Word 文档、或其它已在系统中注册的 OLE 对象。为了把一个对象插入到 RichTextBox 控件中,只需简单地拖动一个文件(例如在Windows 95“资源管理器”中的拖动),或拖动的是另一应用程序(如 Microsoft Word)所用文件的一个突出显示的区域,然后将所拖内容直接放入控件。
    RichTextBox 控件支持 OLE 对象的剪贴板和 OLE 拖/放操作。从剪贴板中粘贴进一个对象时,它被插在当前插入点处。一个对象被拖放到控件时,插入点将跟踪着鼠标光标的移动,直至鼠标按钮释放时该对象即被插入。这种行为和 Microsoft Word 的一样。
    使用 SelPrint 方法,可以打印 RichTextBox 控件的全部或部分文本。
    因为 RichTextBox 是一个数据绑定控件,通过 Data 控件可以把它绑定到 Microsoft Access 数据库的 Binary 或 Memo 字段上,也可把它绑定到具有相同容量的其它数据库字段上(例如 SQL 服务器中的 TEXT 数据类型的字段)。
    标准 TextBox 控件用到的所有属性、事件和方法,RichTextBox 控件几乎都能支持,例如 MaxLength、 MultiLine、 ScrollBars、 SelLength、 SelStart 和 SelText。对于那些可以使用 TextBox 控件的应用程序,也可以很容易地使用 RichTextBox 控件。而且,RichTextBox 控件并没有和标准 TextBox 控件一样具有 64K 字符容量的限制。
    发行注意 为了能在应用程序中使用 RichTextBox 控件,必须把Richtx32.ocx 文件添加到工程中。因此,在应用程序发行时,Richtx32.ocx 文件就应安装在 Microsoft Windows 的 SYSTEM 目录内。
    例子:
             private void 打开图形文件ToolStripMenuItem_Click(object sender, EventArgs e)
             {
                 string NameFile;
                 if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                 {
                     NameFile = this.openFileDialog1.FileName;
                     if (NameFile != "")
                     {
                         this.pictureBox1.Image = Image.FromFile(NameFile);
                     }
                 }
             }
           private void 打开文本文件ToolStripMenuItem_Click(object sender, EventArgs e)
             {
                 string Filename;
                 pictureBox1.Visible = false;
                 if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                 {
                     Filename = openFileDialog1.FileName;
                     if (Filename != "")
                     {
                         this.textBox1.Text = Filename;
                         this.richTextBox1.LoadFile(@Filename, RichTextBoxStreamType.PlainText);
                     }
                 }
             }

    //构造函数
                   this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
                   this.textBox1.Validating += new CancelEventHandler(textBox1_Validating);
                   this.richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked);
             //取消或置为粗体
             private void button2_Click(object sender, System.EventArgs e)
             {
                   Font oldFont = this.richTextBox1.SelectionFont;
                   Font newFont;
                   if (oldFont.Bold)
                       newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Bold);
                   else
                       newFont = new Font(oldFont,oldFont.Style | FontStyle.Bold);
                   this.richTextBox1.SelectionFont = newFont;
                   this.richTextBox1.Focus();
             }
             //取消或置为斜体
             private void button7_Click(object sender, System.EventArgs e)
             {
                   Font oldFont = this.richTextBox1.SelectionFont;
                   Font newFont;
                   if (oldFont.Italic)
                       newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Italic);
                   else
                       newFont = new Font(oldFont,oldFont.Style | FontStyle.Italic);
                   this.richTextBox1.SelectionFont = newFont;
                   this.richTextBox1.Focus();
             }
             //取消或加上下划线
             private void button8_Click(object sender, System.EventArgs e)
             {
                   Font oldFont = this.richTextBox1.SelectionFont;
                   Font newFont;
                   if (oldFont.Underline)
                       newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Underline);
                   else
                       newFont = new Font(oldFont,oldFont.Style | FontStyle.Underline);
                   this.richTextBox1.SelectionFont = newFont;
                   this.richTextBox1
                       .Focus();
             }
             //取消或置为居中
             private void button5_Click(object sender, System.EventArgs e)
             {
                   if (this.richTextBox1.SelectionAlignment == HorizontalAlignment.Center)
                       this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
                   else
                       this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
                   this.richTextBox1.Focus();
             }
             private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
             {
                   if((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar !=13)
                   {
                       e.Handled = true;
                   }
                   else if(e.KeyChar == 13)
                   {
                       TextBox txt = (TextBox)sender;
                       if(txt.Text.Length > 0)
                           ApplyTextSize(txt.Text);
                       e.Handled = true;
                       this.richTextBox1.Focus();
                   }
             }
             private void textBox1_Validating(object sender, CancelEventArgs e)
             {
                   TextBox txt = (TextBox)sender;
                   ApplyTextSize(txt.Text);
                   this.richTextBox1.Focus();
               }
             //改变字体大小
             private void ApplyTextSize(string textSize)
             {
                   float newSize = Convert.ToSingle(textSize);
                   FontFamily currentFontFamily;
                   Font newFont;
                   currentFontFamily = this.richTextBox1.SelectionFont.FontFamily;
                   newFont = new Font(currentFontFamily, newSize);
                   this.richTextBox1.SelectionFont = newFont;
             }
             //打开网页
             private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
             {
                   System.Diagnostics.Process.Start(e.LinkText);
             }
             //打开文件
             private void button1_Click(object sender, System.EventArgs e)
             {
                   try
                   {
                       this.richTextBox1.LoadFile(@"..\..\test.txt");
                   }
                   catch(System.IO.FileNotFoundException)
                   {
                       MessageBox.Show("File not found!");
                   }
             }
             //保存文件
             private void button6_Click(object sender, System.EventArgs e)
             {
                   try
                   {
                       this.richTextBox1.SaveFile(@"..\..\test.txt");
                   }
                   catch(System.Exception err)
                   {
                       MessageBox.Show(err.Message);
                   }
             }


    在 RichTextBox 的内容内搜索文本:
    重载列表
    在 RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例
    [C#] public int Find(char[]);
    示例
    [Visual Basic, C#, C++] 下面的示例在 RichTextBox 的内容中搜索在 text 参数中传递到方法的字符。如果在 RichTextBox 中找到了 text 数组的内容,则该方法返回所找到值的索引;否则,它将返回 -1。该示例假定此方法位于 Form 的类中,该窗体包含一个名为 richTextBox1 的 RichTextBox 控件和一个连接到该示例中定义的单击事件处理方法的 Button 控件(名为 button1)。
    [C#]
    private void button1_Click(object sender, System.EventArgs e)
    {
         MessageBox.Show(FindMyText(new char[]{'D','e','l','t','a'}).ToString());
    }
    public int FindMyText(char[] text)
    {
         // Initialize the return value to false by default.
         int returnValue = -1;
         // Ensure that a search string has been specified and a valid start point.
         if (text.Length > 0)
         {
             // Obtain the location of the first character found in the control
             // that matches any of the characters in the char array.
             int indexToText = richTextBox1.Find(text);
             // Determine whether the text was found in richTextBox1.
             if(indexToText >= 0)
             {
                 // Return the location of the character.
                 returnValue = indexToText;
             }
         }
         return returnValue;
    }

    在 RichTextBox 控件的文本中搜索字符串。
    [C#] public int Find(string);
    从特定的起始点开始,在 RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。
    [C#] public int Find(char[], int);
    在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索字符串。
    [C#] public int Find(string, RichTextBoxFinds);
    示例
    [Visual Basic, C#] 下面的示例在 RichTextBox 的整个内容中搜索传递到此方法文本参数中的搜索字符串的第一个实例。如果在 RichTextBox 中找到搜索字符串,此方法将返回 true 值并突出显示文本;否则返回 false。本示例还在搜索中指定匹配指定搜索字符串的大小写的选项。此示例假定此方法放置在 Form 的类中,并且该类包含一个名为 richTextBox1 的 RichTextBox。
    [C#]
    public bool FindMyText(string text)
    {
       // Initialize the return value to false by default.
       bool returnValue = false;
       // Ensure a search string has been specified.
       if (text.Length > 0)
       {
           // Obtain the location of the search string in richTextBox1.
           int indexToText = richTextBox1.Find(text, RichTextBoxFinds.MatchCase);
           // Determine if the text was found in richTextBox1.
           if(indexToText >= 0)
           {
             returnValue = true;
           }
       }
       return returnValue;
    }

    在 RichTextBox 控件的某个文本范围中搜索字符列表的某个字符的第一个实例。
    [C#] public int Find(char[], int, int);
    在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索位于控件内特定位置的字符串。
    [C#] public int Find(string, int, RichTextBoxFinds);
    在对搜索应用特定选项的情况下,在 RichTextBox 控件文本中搜索控件内某个文本范围内的字符串。

     
  • 相关阅读:
    【转载】Unity 合理安排增量更新(热更新)
    COCOS2D 释放资源的最佳时机
    【转载】利用Unity自带的合图切割功能将合图切割成子图
    用GL画出人物的移动路径
    使用行为树(Behavior Tree)实现游戏AI
    C#学习笔记
    题目:给定一数组 例如:a = [1,2,3,5,2,1] 现用户提供一个数字 请返回用户所提供的数字的所有下标
    算法: 归并排序
    题目:给定两个有序数组,对其进行合并
    数据结构 顺序表实现优先队列 回顾练习
  • 原文地址:https://www.cnblogs.com/zgz_dpl/p/1876679.html
Copyright © 2011-2022 走看看