c#中richTextBox的用法

https://blog.csdn.net/yinbucheng/article/details/59560006?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522158927609319725219917811%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=158927609319725219917811&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_v2~rank_v25-4-59560006.nonecase&utm_term=C%23+RichTextBox%E7%9A%84%E7%94%A8%E6%B3%95

RichTextBox是一種可用於顯示、輸入和操作格式文本,除了可以實現TextBox的所有功能,還能提供富文本的顯示功能。 控件除具有TextBox 控件的所有功能外,還能設定文字顏色、字體和段落格式,支持字符串查找功能,支持rtf格式等功能。

下面就其的常用到的功能進行介紹。

一、顯示滾動條

RichTextBox可設置Multiline屬性來控制是否顯示滾動套,true爲是,false爲否。,默認爲true。(此項屬性在TextBox亦可實現)

滾動條分爲兩種:水平(Horizontal)滾動條和垂直(Vertical)滾動條,通過RichTextBox的ScrollBars屬性設置如何顯示滾動條。(此項屬性在TextBox亦可實現)

ScrollBars屬性值:

1、Both:只有當文本超過RichTextBox的寬度或長度時,才顯示水平滾動條或垂直滾動條,或兩個滾動條都顯示。

2、None:從不顯示任何類型的滾動條。

3、Horizontal:只有當文本超過RichTextBox的寬度時,才顯示水平滾動條。必須將WordWrap屬性設置爲false,纔會出現這種情況。(下面將會給出解釋)

4、Vertical:只有檔文本超過RichTextBox的高度時,才顯示垂直滾動條。

5、ForcedHorizontal:當WordWrap屬性設置爲false時,顯示水平滾動條。在文本未超過RichTextBox的寬度時,該滾動條顯示爲淺灰色。

6、ForcedVertical:始終顯示垂直滾動條。在文本未超過RichTextBox的長度時,該滾動條顯示爲淺灰色。

7、ForcedBoth:始終顯示垂直滾動條。當WordWrap屬性設置爲false時,顯示水平滾動條。在文本未超過RichTextBox的寬度或長度時,兩個滾動條均顯示爲灰色。

 

 注:RichTextBox的WordWrap屬性:用於指示多行文本框控件在必要時是否換行到下一行的開始。當屬性爲true時,不論ScrollBars屬性值是什麼,都不會顯示水平滾動條。

下面通過幾個截圖加以描述其區別。(此項屬性TextBox亦可實現)

(1)、當WordWrap爲true,ScrollBars爲Both時:

 

由此可見,WordWrap爲true時,一旦文本超過RichTextBox的寬度時,就會自動換行到下一行,自然不需要用到水平滾動條,也就不顯示出來了。

(2)、當WordWrap爲false,ScrollBars爲Both時:

 

由此可知,WordWrap爲false時,即使文本超過RichTextBox的寬度,也不會自動換行到下一行,只有用戶輸入回車時纔會換行,並且當文本超過RichTextBox的寬度後,纔會顯示水平滾動條。

代碼實現過程:

  1. private void Form1_Load(object sender, EventArgs e) //窗體的Load事件
  2. {
  3.    richTextBox1.Multiline = true ; //將Multiline屬性設置爲true,實現顯示多行
  4. richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //設置ScrollBars屬性實現只顯示垂直滾動
  5. }

 

二、設置字體屬性

 可通過RichTextBox的Font屬性和ForeColor屬性設置(Visual Studio2013社區版找不到SelectionFont和SelectionColor屬性),也可通過代碼實現,如文本字體設置爲楷體,字體大小爲12,字樣是粗體,文本顏色爲紅色:

  1. private void Form1_Load(object sender, EventArgs e) //窗體的Load事件
  2. {
  3.   richTextBox1.Multiline = true ; //將Multiline屬性設爲true,實現顯示多行
  4.   richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //設置ScrollBars屬性實現只顯示垂直滾動條
  5.   richTextBox1.SelectionFont = new Font ("楷體", 12, FontStyle.Bold); //設置SelectionFont屬性實現控件中的文本爲楷體,大小爲12,字樣是粗體
  6.   richTextBox1.SelectionColor = System.Drawing.Color.Red; //設置SelectionColor屬性實現控件中的文本顏色爲紅色
  7. }

將RichTextBox控件顯示爲超鏈接樣式

將以“http://”開頭的Web鏈接地址作爲超鏈接文本時,運行時RichTextBox超鏈接文本會自動變成藍色字體且有下劃線。

此時點擊超鏈接文本不會有任何響應,需要在RichTextBox的LinkClicked事件中編寫代碼實現。

 

  1. private void Form1_Load(object sender, EventArgs e) //窗體的Load事件
  2. {
  3.   richTextBox1.Multiline = true ; //將Multiline屬性設爲true,實現顯示多行
  4.   richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //設置ScrollBars屬性實現只顯示垂直滾動條
  5.   richTextBox1.Text = "http://www.baidu.com百度一下你就知道"; //設置Text屬性
  6. }
  7. private void richTextBox1_LinkClicked(object sender, EventArgs e)
  8. {
  9.   System.Diagnostics.Process.Start(e.LinkText); //在控件LinkClicked事件中編寫如下代碼實現內容中的網址單擊後可以訪問網址
  10. }

 

 

 

三、設置段落格式

可通過設置SelectionBullet屬性將選定的段落設置爲項目符號列表的格 式,也可以使用SelectionIndent屬性和SelectionHangingIndent屬性設置段落相對於控件的左右邊緣進行縮進。下面用代 碼將控件的SelectionBullet屬性設置爲true,使控件中的內容以項目符號列表的格式排列。

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3.   richTextBox1.Multiline = true ;
  4. richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical ;
  5.   richTextBox1.SelectionBullet = true ;
  6. }

以下爲屬性SelectionBullet設爲false和true時的差異(前者爲false後者爲true):

 

通過SelectionIndent屬性設置一個整數,該整數表示控件的左邊緣和文本的左邊緣之間的距離(以像素爲單位)。通過SelectionRightIndent屬性設置一個整數,該整數表示控件的右邊緣與文本的右邊緣之間的距離(以像素爲單位)。

以下通過代碼實現SelectionIndent屬性設置。

 

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3.   richTextBox1.Multiline = true ;
  4.   richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical ;
  5.   richTextBox1.SelectionIndent = 50 ;
  6. }

 

差異如下組圖:

 

SelectionRightIndent屬性與SelectionIndent屬性類似,可類比,當然也可以同時使用。

 

四、常用功能

1.RichTextBox控件的常用屬性

1)SelectedText屬性、SelectionLength屬性、SelectionStart屬性——與TextBox控件的屬性功能相同。
2)SelectionFont:獲取或設置選中的文本或插入點的字體,例如:
richTextBox1.SelectionFont=fontDialog1.Font; //設置爲字體對話框中選中的字體 
3)SelectionColor:獲取或設置選中的文本或插入點的文本顏色。
4)SelectionAlignment:獲取或設置應用到當前選定內容或插入點的對齊方式。
5)Lines屬性——字符串數組。記錄輸入到RichText控件中的所有文本,每按兩次回車鍵之間的字符串是該數組的一個元素。
6)Modifyed屬性——記錄用戶是否已修改控件中的文本內容。若已修改,該屬性值自動設置爲true。
7)HideSelection屬性——設置當焦點離開該控件時,選定的文本是否保持突出顯示。值爲false時突出顯示。

2.RichTextBox控件的常用事件

1)SelectionChange事件——控件中選中的文本發生改變時,觸發該事件。 
2)TextChanged事件——控件中的文本內容發生改變時,觸發該事件。

3.RichTextBox控件的常用方法

1)Clear( )方法——清除RichText控件中用戶輸入的所有內容。 
2)Copy( )、Cut( )、Paste( )方法——實現RichText控件的剪貼板功能;
3)SelectAll( )方法——選中控件中的所有文本。  4)Find( )方法——實現查找功能。
5)SaveFile( )方法、LoadFile( )方法——保存文本和打開文件。
6)Undo( )方法、Redo( )方法——撤銷上一次編輯操作、重做上次撤銷的編輯操作。  
說明:常與CanUndo屬性和CanRedo屬性配合使用。
7)LoadFile()——加載文本文件(*.txt)或RTF文件(*.rtf)。 
8)SaveFile()——保存文本文件(*.txt)或RTF文件(*.rtf)。


 4. 將文件加載到RichTextBox 對象中  

使用LoadFile( )方法.
(1)一般格式
RichTextBox對象名.LoadFile(文件名,文件類型);  
(2)說明
RichTextBox 控件可以顯示純文本、Unicode 純文本或 RTF 格式文件。若要顯示這些文件,可調用 LoadFile 方法。例如,使用打開文件對話框選擇一個文本文件並加載到richTextBox1控件中,代碼如下:  

  1. openFileDialog1.Filter="文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
  2. if(openFileDialog1.ShowDialog()==DialogResult.OK)
  3. {
  4. string fName=openFileDialog1.FileName;
  5. richTextBox1.LoadFile(fName,RichTextBoxStreamType.PlainText );
  6. }

RichTextBoxStreamType.PlainText爲加載的文件類型,其他可選的枚舉值如下:

5. 保存RichTextBox 對象中的文件

用SaveFile( )方法
(1)一般格式
RichTextBox對象名.SaveFile(文件名,文件類型);   
(2)使用說明
同LoadSave( )方法。
  1. //保存RTF格式文件
  2. saveFileDialog1.Filter="RTF文件(*.rtf)|*.rtf"; saveFileDialog1.DefaultExt="rtf";
  3. //默認的文件擴展名
  4. if(saveFileDialog1.ShowDialog()==DialogResult.OK)
  5. richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.RichText );

6. 插入圖片文件

可藉助剪貼板實現.

  1. Clipboard.Clear(); //清空剪貼板
  2. Bitmap bmp = new Bitmap(@"d:\圖片1.jpg"); //創建Bitmap類對象
  3. Clipboard.SetImage(bmp); //將Bitmap類對象寫入剪貼板
  4. richTextBox1.Paste(); //將剪貼板中的對象粘貼到RichTextBox1

 

7. 其它補充內容

TextBox控件用到的所有屬性、事件和方法,RichTextBox控件幾乎都能支持,例如 MaxLength、MultiLine、ScrollBars、SelLength、SelStart 和 SelText。
注意:
TextBoxBase.Undo 方法不可用於 KeyPress 或 TextChanged 事件。
RichTextBox 控件沒有TextBox控件一樣具有64K字符容量的限制。 
RichTextBox 控件提供許多可對控件內任何文本部分應用格式設置的屬性。若要更改文本的格式設置,必須首先選定此文本。只能爲選定的文本分配字符和段落格式設置。對選定 的文本內容進行設置後,在選定內容後輸入的所有文本也用相同的設置進行格式設置,直到更改設置或選定控件文檔的不同部分爲止。SelectionFont 屬性使您得以將文本以粗體或斜體顯示。還可以使用此屬性更改文本的大小和字樣。SelectionColor 屬性使您得以更改文本的顏色。若要創建項目符號列表,可以使用 SelectionBullet 屬性。還可以通過設置 SelectionIndent、SelectionRightIndent 和 SelectionHangingIndent 屬性調整段落格式設置。

 

RichTextBox 控件提供具有打開和保存文件的功能的方法。
LoadFile 方法使您得以將現有的 RTF 或 ASCII 文本文件加載到控件中。還可以從已打開的數據流加載數據。SaveFile 使您得以將文件保存到 RTF 或 ASCII 文本中。與 LoadFile 方法相似,還可以使用 SaveFile 方法保存到開放式數據流。

 
Find 方法被重載,可以同時查找控件文本內的文本字符串以及特定字符。 
也可以將 RichTextBox 控件初始化爲內存中存儲的數據。例如,可以將 Rtf 屬性初始化爲包含要顯示文本的字符串,包括確定如何設置該文本格式的 RTF 代碼。 
可以使用 DetectUrls 屬性適當地顯示控件文本中的鏈接(如到網站的鏈接)。然後可以處理 LinkClicked 事件以執行與該鏈接關聯的任務。 
SelectionProtected 屬性使您得以保護控件內的文本不被用戶操作。當控件中有受保護的文本時,可以處理 Protected 事件以確定用戶何時曾試圖修改受保護的文本,並提醒用戶該文本是受保護的,或向用戶提供標準方式供其操作受保護的文本。
 
 
 
例1
將 RTF 文件加載到控件中並搜索單詞“Text”的第一個實例。然後代碼更改選定文本的字體樣式、字體大小和字體顏色並將更改保存到原始文件。
複製代碼
public void CreateMyRichTextBox()
{
    RichTextBox richTextBox1 = new RichTextBox();
    richTextBox1.Dock = DockStyle.Fill;
richTextBox1.LoadFile(<span style="color:rgb(128,0,0);">"</span><span style="color:rgb(128,0,0);">C:\\MyDocument.rtf</span><span style="color:rgb(128,0,0);">"</span>);
richTextBox1.Find(<span style="color:rgb(128,0,0);">"</span><span style="color:rgb(128,0,0);">Text</span><span style="color:rgb(128,0,0);">"</span>, RichTextBoxFinds.MatchCase);

richTextBox1.SelectionFont = <span style="color:rgb(0,0,255);">new</span> Font(<span style="color:rgb(128,0,0);">"</span><span style="color:rgb(128,0,0);">Verdana</span><span style="color:rgb(128,0,0);">"</span>, <span style="color:rgb(128,0,128);">12</span>, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;

richTextBox1.SaveFile(<span style="color:rgb(128,0,0);">"</span><span style="color:rgb(128,0,0);">C:\\MyDocument.rtf</span><span style="color:rgb(128,0,0);">"</span>, RichTextBoxStreamType.RichText);

<span style="color:rgb(0,0,255);">this</span>.Controls.Add(richTextBox1);

}

複製代碼
部分文字屬性的更改

 

例2

更加負載的多媒體類型的文字段落的錄入。

複製代碼
Demo

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

namespace Win_Test
{
public partial class RichTextBox_Test : Form
{
public RichTextBox_Test()
{
InitializeComponent();
}

    Font oldFont;
    Font newFont;

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">richTextBox1 所選文字加粗</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> button1_Click(<span style="color:rgb(0,0,255);">object</span> sender, EventArgs e)
    {

        oldFont = <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont;
        <span style="color:rgb(0,0,255);">if</span> (oldFont.Bold)
        { newFont = <span style="color:rgb(0,0,255);">new</span> Font(oldFont, oldFont.Style &amp; ~FontStyle.Bold); }
        <span style="color:rgb(0,0,255);">else</span>
            newFont = <span style="color:rgb(0,0,255);">new</span> Font(oldFont, oldFont.Style | FontStyle.Bold);

        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont = newFont;
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.Focus();

    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">richTextBox1 所選文字加下劃線</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> button2_Click(<span style="color:rgb(0,0,255);">object</span> sender, EventArgs e)
    {
        oldFont = <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont;
        <span style="color:rgb(0,0,255);">if</span> (oldFont.Underline)
        { newFont = <span style="color:rgb(0,0,255);">new</span> Font(oldFont, oldFont.Style &amp; ~FontStyle.Underline); }
        <span style="color:rgb(0,0,255);">else</span>
            newFont = <span style="color:rgb(0,0,255);">new</span> Font(oldFont, oldFont.Style | FontStyle.Underline);
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont = newFont;
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.Focus();


    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">richTextBox1 所選文字爲斜體</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> button3_Click(<span style="color:rgb(0,0,255);">object</span> sender, EventArgs e)
    {
        oldFont = <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont;
        <span style="color:rgb(0,0,255);">if</span> (oldFont.Italic)
        { newFont = <span style="color:rgb(0,0,255);">new</span> Font(oldFont, oldFont.Style &amp; ~FontStyle.Italic); }
        <span style="color:rgb(0,0,255);">else</span>

            newFont = <span style="color:rgb(0,0,255);">new</span> Font(oldFont, oldFont.Style | FontStyle.Italic);
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont = newFont;
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.Focus();
    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">richTextBox1 所選文字居中</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> button4_Click(<span style="color:rgb(0,0,255);">object</span> sender, EventArgs e)
    {
        <span style="color:rgb(0,0,255);">if</span> (<span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionAlignment == HorizontalAlignment.Center)
            <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
        <span style="color:rgb(0,0,255);">else</span>
            <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.Focus();
    }


    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);"> 在文本框輸入字體大小</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> textBox1_KeyPress(<span style="color:rgb(0,0,255);">object</span> sender, KeyPressEventArgs e)
    {
        <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">remove all characters that are not numbers,backspace and enter</span>
        <span style="color:rgb(0,0,255);">if</span> ((e.KeyChar &lt; <span style="color:rgb(128,0,128);">48</span> || e.KeyChar &gt; <span style="color:rgb(128,0,128);">57</span>) &amp;&amp; e.KeyChar != <span style="color:rgb(128,0,128);">8</span> &amp;&amp; e.KeyChar != <span style="color:rgb(128,0,128);">13</span>)
        { e.Handled = <span style="color:rgb(0,0,255);">true</span>; }
        <span style="color:rgb(0,0,255);">else</span> <span style="color:rgb(0,0,255);">if</span> (e.KeyChar == <span style="color:rgb(128,0,128);">13</span>)
        {
            TextBox txt = (TextBox)sender;
            <span style="color:rgb(0,0,255);">if</span> (txt.Text.Length &gt; <span style="color:rgb(128,0,128);">0</span>)
                ApplyTextSize(txt.Text);
            e.Handled = <span style="color:rgb(0,0,255);">true</span>;
            <span style="color:rgb(0,0,255);">this</span>.richTextBox1.Focus();
        }
    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">根據textBox1的值設置richTextBox1的字體</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> ApplyTextSize(<span style="color:rgb(0,0,255);">string</span> textSize)
    {
        <span style="color:rgb(0,0,255);">float</span> newSize = Convert.ToSingle(textSize);
        FontFamily currentFontFamily;
        Font newFont;
        currentFontFamily = <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont.FontFamily;
        newFont = <span style="color:rgb(0,0,255);">new</span> Font(currentFontFamily, newSize);
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.SelectionFont = newFont;
    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">在textBox1控件驗證時觸發,設置richTextBox1的字體</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> textBox1_Validating(<span style="color:rgb(0,0,255);">object</span> sender, CancelEventArgs e)
    {
        TextBox txt = (TextBox)sender;
        ApplyTextSize(txt.Text);
        <span style="color:rgb(0,0,255);">this</span>.richTextBox1.Focus();
    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">讓瀏覽器打開超鏈接地址</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> richTextBox1_LinkClicked(<span style="color:rgb(0,0,255);">object</span> sender, LinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.LinkText);
    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">richTextBox1 加載 Test.rtf 文件</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> button5_Click(<span style="color:rgb(0,0,255);">object</span> sender, EventArgs e)
    {
        <span style="color:rgb(0,0,255);">try</span>
        {
            richTextBox1.LoadFile(<span style="color:rgb(128,0,0);">"</span><span style="color:rgb(128,0,0);">Test.rtf</span><span style="color:rgb(128,0,0);">"</span>);
        }
        <span style="color:rgb(0,0,255);">catch</span> (System.IO.FileNotFoundException)
        {
            MessageBox.Show(<span style="color:rgb(128,0,0);">"</span><span style="color:rgb(128,0,0);">No file to be load yet</span><span style="color:rgb(128,0,0);">"</span>);
        }
    }

    <span style="color:rgb(0,128,0);">//</span><span style="color:rgb(0,128,0);">richTextBox1 保存到 Test.rtf 文件</span>
    <span style="color:rgb(0,0,255);">private</span> <span style="color:rgb(0,0,255);">void</span> button6_Click(<span style="color:rgb(0,0,255);">object</span> sender, EventArgs e)
    {
        <span style="color:rgb(0,0,255);">try</span>
        {
            richTextBox1.SaveFile(<span style="color:rgb(128,0,0);">"</span><span style="color:rgb(128,0,0);">Test.rtf</span><span style="color:rgb(128,0,0);">"</span>);
        }
        <span style="color:rgb(0,0,255);">catch</span> (System.Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }

}

}

複製代碼
富文本,多類型的編碼
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章