C#中RichTextBox使用方法和TextBox基本一樣,只不過(guò)RichText除了TXT外,還支持RTF格式的文檔。本文詳細(xì)介紹RichTextBox的使用方法供大家參考,具體如下:
一、RichTextBox的使用方法
RichTextBox.Find方法
RichTextBox控件不僅允許輸入和編輯文本,同時(shí)還提供了標(biāo)準(zhǔn) TextBox 控件未具有的、更高級(jí)的指定格式的許多功能。
語(yǔ)法:RichTextBox
說(shuō)明:
RichTextBox 提供了一些屬性,對(duì)于本控件文本的任何部分,用這些屬性都可以指定格式。為了改變文本的格式,首先要選定它。只有選定的文本才能賦予字符和段落格式。使用這些屬性,可把文本改為粗體或斜體,或改變其顏色,以及創(chuàng)建上標(biāo)和下標(biāo)。通過(guò)設(shè)置左右縮進(jìn)和懸掛式縮進(jìn),可調(diào)整段落的格式。
RichTextBox 控件能以 rtf 格式和普通 ASCII 文本格式這兩種形式打開(kāi)和保存文件。可以使用控件的方法(LoadFile 和 SaveFile)直接讀寫文件,或使用與 Visual Basic 文件輸入/輸出語(yǔ)句聯(lián)結(jié)的、諸如 SelRTF 和 TextRTF 之類的控件屬性打開(kāi)和保存文件。
通過(guò)使用 OLEObjects 集合,RichTextBox 控件支持對(duì)象的嵌入。插入到控件中的每個(gè)對(duì)象,都代表 OLEObject 對(duì)象。用這樣的控件,就可以創(chuàng)建包含其它文檔或?qū)ο蟮奈臋n。例如,可創(chuàng)建這樣的文檔,它有一個(gè)嵌入的 Microsoft Excel 電子數(shù)據(jù)表格、或 Microsoft Word 文檔、或其它已在系統(tǒng)中注冊(cè)的 OLE 對(duì)象。為了把一個(gè)對(duì)象插入到 RichTextBox 控件中,只需簡(jiǎn)單地拖動(dòng)一個(gè)文件(例如在Windows 95“資源管理器”中的拖動(dòng)),或拖動(dòng)的是另一應(yīng)用程序(如 Microsoft Word)所用文件的一個(gè)突出顯示的區(qū)域,然后將所拖內(nèi)容直接放入控件。
RichTextBox 控件支持 OLE 對(duì)象的剪貼板和 OLE 拖/放操作。從剪貼板中粘貼進(jìn)一個(gè)對(duì)象時(shí),它被插在當(dāng)前插入點(diǎn)處。一個(gè)對(duì)象被拖放到控件時(shí),插入點(diǎn)將跟蹤著鼠標(biāo)光標(biāo)的移動(dòng),直至鼠標(biāo)按鈕釋放時(shí)該對(duì)象即被插入。這種行為和 Microsoft Word 的一樣。
使用 SelPrint 方法,可以打印 RichTextBox 控件的全部或部分文本。
因?yàn)?RichTextBox 是一個(gè)數(shù)據(jù)綁定控件,通過(guò) Data 控件可以把它綁定到 Microsoft Access 數(shù)據(jù)庫(kù)的 Binary 或 Memo 字段上,也可把它綁定到具有相同容量的其它數(shù)據(jù)庫(kù)字段上(例如 SQL 服務(wù)器中的 TEXT 數(shù)據(jù)類型的字段)。
標(biāo)準(zhǔn) TextBox 控件用到的所有屬性、事件和方法,RichTextBox 控件幾乎都能支持,例如 MaxLength、 MultiLine、 ScrollBars、 SelLength、 SelStart 和 SelText。對(duì)于那些可以使用 TextBox 控件的應(yīng)用程序,也可以很容易地使用 RichTextBox 控件。而且,RichTextBox 控件并沒(méi)有和標(biāo)準(zhǔn) TextBox 控件一樣具有 64K 字符容量的限制。
發(fā)行注意 為了能在應(yīng)用程序中使用 RichTextBox 控件,必須把Richtx32.ocx 文件添加到工程中。因此,在應(yīng)用程序發(fā)行時(shí),Richtx32.ocx 文件就應(yīng)安裝在 Microsoft Windows 的 SYSTEM 目錄內(nèi)。
二、RichTextBox實(shí)例代碼:
private void 打開(kāi)圖形文件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 打開(kāi)文本文件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); } } }//構(gòu)造函數(shù) 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; } //打開(kāi)網(wǎng)頁(yè) private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e) { System.Diagnostics.Process.Start(e.LinkText); } //打開(kāi)文件 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 的內(nèi)容內(nèi)搜索文本:
1.重載列表:
在 RichTextBox 控件的文本中搜索字符列表中某個(gè)字符的第一個(gè)實(shí)例
public int Find(char[]);
下面的示例在 RichTextBox 的內(nèi)容中搜索在 text 參數(shù)中傳遞到方法的字符。如果在 RichTextBox 中找到了 text 數(shù)組的內(nèi)容,則該方法返回所找到值的索引;否則,它將返回 -1。該示例假定此方法位于 Form 的類中,該窗體包含一個(gè)名為 richTextBox1 的 RichTextBox 控件和一個(gè)連接到該示例中定義的單擊事件處理方法的 Button 控件(名為 button1)。
如下代碼:
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;}2.在 RichTextBox 控件的文本中搜索字符串。
public int Find(string);
從特定的起始點(diǎn)開(kāi)始,在 RichTextBox 控件的文本中搜索字符列表中某個(gè)字符的第一個(gè)實(shí)例。
public int Find(char[], int);
在對(duì)搜索應(yīng)用特定選項(xiàng)的情況下,在 RichTextBox 控件的文本中搜索字符串。
public int Find(string, RichTextBoxFinds);
下面的示例在 RichTextBox 的整個(gè)內(nèi)容中搜索傳遞到此方法文本參數(shù)中的搜索字符串的第一個(gè)實(shí)例。如果在 RichTextBox 中找到搜索字符串,此方法將返回 true 值并突出顯示文本;否則返回 false。本示例還在搜索中指定匹配指定搜索字符串的大小寫的選項(xiàng)。此示例假定此方法放置在 Form 的類中,并且該類包含一個(gè)名為 richTextBox1 的 RichTextBox。
具體代碼如下:
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 控件的某個(gè)文本范圍中搜索字符列表的某個(gè)字符的第一個(gè)實(shí)例。
public int Find(char[], int, int);
在對(duì)搜索應(yīng)用特定選項(xiàng)的情況下,在 RichTextBox 控件的文本中搜索位于控件內(nèi)特定位置的字符串。
public int Find(string, int, RichTextBoxFinds);
在對(duì)搜索應(yīng)用特定選項(xiàng)的情況下,在 RichTextBox 控件文本中搜索控件內(nèi)某個(gè)文本范圍內(nèi)的字符串。
新聞熱點(diǎn)
疑難解答
圖片精選