C#中richtextbox應用辦法詳解。本站提示廣大學習愛好者:(C#中richtextbox應用辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中richtextbox應用辦法詳解正文
C#中RichTextBox應用辦法和TextBox根本一樣,只不外RichText除TXT外,還支撐RTF格局的文檔。本文具體引見RichTextBox的應用辦法供年夜家參考,詳細以下:
1、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 目次內。
2、RichTextBox實例代碼:
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);
}
}
3、在 RichTextBox 的內容內搜刮文本:
1.重載列表:
在 RichTextBox 控件的文本中搜刮字符列表中某個字符的第一個實例
public int Find(char[]);
上面的示例在 RichTextBox 的內容中搜刮在 text 參數中傳遞到辦法的字符。假如在 RichTextBox 中找到了 text 數組的內容,則該辦法前往所找到值的索引;不然,它將前往 -1。該示例假定此辦法位於 Form 的類中,該窗體包括一個名為 richTextBox1 的 RichTextBox 控件和一個銜接到該示例中界說的單擊事宜處置辦法的 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);
從特定的肇端點開端,在 RichTextBox 控件的文本中搜刮字符列表中某個字符的第一個實例。
public int Find(char[], int);
在對搜刮運用特定選項的情形下,在 RichTextBox 控件的文本中搜刮字符串。
public int Find(string, RichTextBoxFinds);
上面的示例在 RichTextBox 的全部內容中搜刮傳遞到此辦法文本參數中的搜刮字符串的第一個實例。假如在 RichTextBox 中找到搜刮字符串,此辦法將前往 true 值並凸起顯示文本;不然前往 false。本示例還在搜刮中指定婚配指定搜刮字符串的年夜小寫的選項。此示例假定此辦法放置在 Form 的類中,而且該類包括一個名為 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 控件的某個文本規模中搜刮字符列表的某個字符的第一個實例。
public int Find(char[], int, int);
在對搜刮運用特定選項的情形下,在 RichTextBox 控件的文本中搜刮位於控件內特定地位的字符串。
public int Find(string, int, RichTextBoxFinds);
在對搜刮運用特定選項的情形下,在 RichTextBox 控件文本中搜刮控件內某個文本規模內的字符串。