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的寬度後,纔會顯示水平滾動條。
代碼實現過程:
- private void Form1_Load(object sender, EventArgs e) //窗體的Load事件
- {
-
- richTextBox1.Multiline = true ; //將Multiline屬性設置爲true,實現顯示多行
- richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //設置ScrollBars屬性實現只顯示垂直滾動
-
- }
二、設置字體屬性
可通過RichTextBox的Font屬性和ForeColor屬性設置(Visual Studio2013社區版找不到SelectionFont和SelectionColor屬性),也可通過代碼實現,如文本字體設置爲楷體,字體大小爲12,字樣是粗體,文本顏色爲紅色:
- private void Form1_Load(object sender, EventArgs e) //窗體的Load事件
- {
- richTextBox1.Multiline = true ; //將Multiline屬性設爲true,實現顯示多行
- richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //設置ScrollBars屬性實現只顯示垂直滾動條
- richTextBox1.SelectionFont = new Font ("楷體", 12, FontStyle.Bold); //設置SelectionFont屬性實現控件中的文本爲楷體,大小爲12,字樣是粗體
- richTextBox1.SelectionColor = System.Drawing.Color.Red; //設置SelectionColor屬性實現控件中的文本顏色爲紅色
- }
將RichTextBox控件顯示爲超鏈接樣式
將以“http://”開頭的Web鏈接地址作爲超鏈接文本時,運行時RichTextBox超鏈接文本會自動變成藍色字體且有下劃線。
此時點擊超鏈接文本不會有任何響應,需要在RichTextBox的LinkClicked事件中編寫代碼實現。
- private void Form1_Load(object sender, EventArgs e) //窗體的Load事件
- {
- richTextBox1.Multiline = true ; //將Multiline屬性設爲true,實現顯示多行
- richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //設置ScrollBars屬性實現只顯示垂直滾動條
- richTextBox1.Text = "http://www.baidu.com百度一下你就知道"; //設置Text屬性
- }
-
- private void richTextBox1_LinkClicked(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start(e.LinkText); //在控件LinkClicked事件中編寫如下代碼實現內容中的網址單擊後可以訪問網址
- }
三、設置段落格式
可通過設置SelectionBullet屬性將選定的段落設置爲項目符號列表的格 式,也可以使用SelectionIndent屬性和SelectionHangingIndent屬性設置段落相對於控件的左右邊緣進行縮進。下面用代 碼將控件的SelectionBullet屬性設置爲true,使控件中的內容以項目符號列表的格式排列。
- private void Form1_Load(object sender, EventArgs e)
- {
- richTextBox1.Multiline = true ;
- richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical ;
- richTextBox1.SelectionBullet = true ;
- }
以下爲屬性SelectionBullet設爲false和true時的差異(前者爲false後者爲true):
通過SelectionIndent屬性設置一個整數,該整數表示控件的左邊緣和文本的左邊緣之間的距離(以像素爲單位)。通過SelectionRightIndent屬性設置一個整數,該整數表示控件的右邊緣與文本的右邊緣之間的距離(以像素爲單位)。
以下通過代碼實現SelectionIndent屬性設置。
- private void Form1_Load(object sender, EventArgs e)
- {
- richTextBox1.Multiline = true ;
- richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical ;
- richTextBox1.SelectionIndent = 50 ;
- }
差異如下組圖:
SelectionRightIndent屬性與SelectionIndent屬性類似,可類比,當然也可以同時使用。
四、常用功能
1.RichTextBox控件的常用屬性
1)SelectedText屬性、SelectionLength屬性、SelectionStart屬性——與TextBox控件的屬性功能相同。2)SelectionFont:獲取或設置選中的文本或插入點的字體,例如:
richTextBox1.SelectionFont=fontDialog1.Font; //設置爲字體對話框中選中的字體
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控件中,代碼如下:
- openFileDialog1.Filter="文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
- if(openFileDialog1.ShowDialog()==DialogResult.OK)
- {
- string fName=openFileDialog1.FileName;
- richTextBox1.LoadFile(fName,RichTextBoxStreamType.PlainText );
- }
RichTextBoxStreamType.PlainText爲加載的文件類型,其他可選的枚舉值如下:
5. 保存RichTextBox 對象中的文件
用SaveFile( )方法(1)一般格式
RichTextBox對象名.SaveFile(文件名,文件類型);
(2)使用說明
同LoadSave( )方法。
- //保存RTF格式文件
- saveFileDialog1.Filter="RTF文件(*.rtf)|*.rtf"; saveFileDialog1.DefaultExt="rtf";
- //默認的文件擴展名
- if(saveFileDialog1.ShowDialog()==DialogResult.OK)
- richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.RichText );
6. 插入圖片文件
可藉助剪貼板實現.
- Clipboard.Clear(); //清空剪貼板
- Bitmap bmp = new Bitmap(@"d:\圖片1.jpg"); //創建Bitmap類對象
- Clipboard.SetImage(bmp); //將Bitmap類對象寫入剪貼板
- richTextBox1.Paste(); //將剪貼板中的對象粘貼到RichTextBox1
7. 其它補充內容
TextBox控件用到的所有屬性、事件和方法,RichTextBox控件幾乎都能支持,例如 MaxLength、MultiLine、ScrollBars、SelLength、SelStart 和 SelText。TextBoxBase.Undo 方法不可用於 KeyPress 或 TextChanged 事件。
RichTextBox 控件沒有TextBox控件一樣具有64K字符容量的限制。
RichTextBox 控件提供具有打開和保存文件的功能的方法。
LoadFile 方法使您得以將現有的 RTF 或 ASCII 文本文件加載到控件中。還可以從已打開的數據流加載數據。SaveFile 使您得以將文件保存到 RTF 或 ASCII 文本中。與 LoadFile 方法相似,還可以使用 SaveFile 方法保存到開放式數據流。
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 & ~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 & ~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 & ~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 < <span style="color:rgb(128,0,128);">48</span> || e.KeyChar > <span style="color:rgb(128,0,128);">57</span>) && e.KeyChar != <span style="color:rgb(128,0,128);">8</span> && 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 > <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);
}
}
}
}
富文本,多類型的編碼