成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

詳解C# RichTextBox的使用方法

開發 后端
本文介紹了RichTextBox.Find方法,以及C# RichTextBox控件的使用方法。

RichTextBox.Find方法

RichTextBox控件不僅允許輸入和編輯文本,同時還提供了標準TextBox 控件未具有的、更高級的指定格式的許多功能。

語法:RichTextBox

說明:

C# RichTextBox提供了一些屬性,對于本控件文本的任何部分,用這些屬性都可以指定格式。為了改變文本的格式,首先要選定它。只有選定的文本才能賦予字符和段落格式。使用這些屬性,可把文本改為粗體或斜體,或改變其顏色,以及創建上標和下標。通過設置左右縮進和懸掛式縮進,可調整段落的格式。

C# 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 目錄內。

例子:          

  1.   private void 打開圖形文件ToolStripMenuItem_Click(object sender, EventArgs e)  
  2.          {  
  3.              string NameFile;  
  4.              if (this.openFileDialog1.ShowDialog() == DialogResult.OK)  
  5.              {  
  6.                  NameFile = this.openFileDialog1.FileName;  
  7.                  if (NameFile != "")  
  8.                  {  
  9.                      this.pictureBox1.Image = Image.FromFile(NameFile);  
  10.                  }  
  11.              }  
  12.          }  
  13.        private void 打開文本文件ToolStripMenuItem_Click(object sender, EventArgs e)  
  14.          {  
  15.              string Filename;  
  16.              pictureBox1.Visible = false;  
  17.              if (this.openFileDialog1.ShowDialog() == DialogResult.OK)  
  18.              {  
  19.                  Filename = openFileDialog1.FileName;  
  20.                  if (Filename != "")  
  21.                  {  
  22.                      this.textBox1.Text = Filename;  
  23.                      this.richTextBox1.LoadFile(@Filename, RichTextBoxStreamType.PlainText);  
  24.                  }  
  25.              }  
  26.          }  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. //構造函數  
  33.                this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);  
  34.                this.textBox1.Validating += new CancelEventHandler(textBox1_Validating);  
  35.                this.richTextBox1.LinkClicked += new LinkClickedEventHandler(richTextBox1_LinkClicked);  
  36.          //取消或置為粗體  
  37.          private void button2_Click(object sender, System.EventArgs e)  
  38.          {  
  39.                Font oldFont = this.richTextBox1.SelectionFont;  
  40.                Font newFont;  
  41.                if (oldFont.Bold)  
  42.                    newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Bold);  
  43.                else 
  44.                    newFont = new Font(oldFont,oldFont.Style | FontStyle.Bold);  
  45.                this.richTextBox1.SelectionFont = newFont;  
  46.                this.richTextBox1.Focus();  
  47.          }  
  48.          //取消或置為斜體  
  49.          private void button7_Click(object sender, System.EventArgs e)  
  50.          {  
  51.                Font oldFont = this.richTextBox1.SelectionFont;  
  52.                Font newFont;  
  53.                if (oldFont.Italic)  
  54.                    newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Italic);  
  55.                else 
  56.                    newFont = new Font(oldFont,oldFont.Style | FontStyle.Italic);  
  57.                this.richTextBox1.SelectionFont = newFont;  
  58.                this.richTextBox1.Focus();  
  59.          }  
  60.          //取消或加上下劃線  
  61.          private void button8_Click(object sender, System.EventArgs e)  
  62.          {  
  63.                Font oldFont = this.richTextBox1.SelectionFont;  
  64.                Font newFont;  
  65.                if (oldFont.Underline)  
  66.                    newFont = new Font(oldFont,oldFont.Style & ~FontStyle.Underline);  
  67.                else 
  68.                    newFont = new Font(oldFont,oldFont.Style | FontStyle.Underline);  
  69.                this.richTextBox1.SelectionFont = newFont;  
  70.                this.richTextBox1  
  71.                    .Focus();  
  72.          }  
  73.          //取消或置為居中  
  74.          private void button5_Click(object sender, System.EventArgs e)  
  75.          {  
  76.                if (this.richTextBox1.SelectionAlignment == HorizontalAlignment.Center)  
  77.                    this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;  
  78.                else 
  79.                    this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;  
  80.                this.richTextBox1.Focus();  
  81.          }  
  82.          private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
  83.          {  
  84.                if((e.KeyChar <  48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar !=13)   
  85.                {  
  86.                    e.Handled = true;  
  87.                }  
  88.                else if(e.KeyChar == 13)   
  89.                {  
  90.                    TextBox txt = (TextBox)sender;  
  91.                    if(txt.Text.Length > 0)   
  92.                        ApplyTextSize(txt.Text);  
  93.                    e.Handled = true;  
  94.                    this.richTextBox1.Focus();  
  95.                }  
  96.          }  
  97.          private void textBox1_Validating(object sender, CancelEventArgs e)  
  98.          {  
  99.                TextBox txt = (TextBox)sender;  
  100.                ApplyTextSize(txt.Text);  
  101.                this.richTextBox1.Focus();  
  102.            }  
  103.          //改變字體大小  
  104.          private void ApplyTextSize(string textSize)  
  105.          {  
  106.                float newSize = Convert.ToSingle(textSize);  
  107.                FontFamily currentFontFamily;  
  108.                Font newFont;  
  109.                currentFontFamily = this.richTextBox1.SelectionFont.FontFamily;  
  110.                newFont = new Font(currentFontFamily, newSize);  
  111.                this.richTextBox1.SelectionFont = newFont;  
  112.          }  
  113.          //打開網頁  
  114.          private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)  
  115.          {  
  116.                System.Diagnostics.Process.Start(e.LinkText);  
  117.          }  
  118.          //打開文件  
  119.          private void button1_Click(object sender, System.EventArgs e)  
  120.          {  
  121.                try 
  122.                {  
  123.                    this.richTextBox1.LoadFile(@"..\..\test.txt");  
  124.                }  
  125.                catch(System.IO.FileNotFoundException)  
  126.                {  
  127.                    MessageBox.Show("File not found!");  
  128.                }  
  129.          }  
  130.          //保存文件  
  131.          private void button6_Click(object sender, System.EventArgs e)  
  132.          {  
  133.                try 
  134.                {  
  135.                    this.richTextBox1.SaveFile(@"..\..\test.txt");  
  136.                }  
  137.                catch(System.Exception err)  
  138.                {  
  139.                    MessageBox.Show(err.Message);  
  140.                }  
  141.          } 

在C# RichTextBox的內容內搜索文本:

重載列表

在 RichTextBox 控件的文本中搜索字符列表中某個字符的***個實例

[C#] public int Find(char[]);

示例

[Visual Basic, C#, C++] 下面的示例在 RichTextBox 的內容中搜索在 text 參數中傳遞到方法的字符。如果在 RichTextBox 中找到了 text 數組的內容,則該方法返回所找到值的索引;否則,它將返回 -1。該示例假定此方法位于 Form 的類中,該窗體包含一個名為 richTextBox1 的 RichTextBox 控件和一個連接到該示例中定義的單擊事件處理方法的 Button 控件(名為 button1)。

[C#]

  1. private void button1_Click(object sender, System.EventArgs e)     
  2. {     
  3.      MessageBox.Show(FindMyText(new char[]{'D','e','l','t','a'}).ToString());     
  4. }     
  5. public int FindMyText(char[] text)     
  6. {     
  7.      // Initialize the return value to false by default.     
  8.      int returnValue = -1;     
  9.      // Ensure that a search string has been specified and a valid start point.     
  10.      if (text.Length > 0)      
  11.      {     
  12.          // Obtain the location of the first character found in the control     
  13.          // that matches any of the characters in the char array.     
  14.          int indexToText = richTextBox1.Find(text);     
  15.          // Determine whether the text was found in richTextBox1.     
  16.          if(indexToText >= 0)     
  17.          {     
  18.              // Return the location of the character.     
  19.              returnValue = indexToText;     
  20.          }     
  21.      }     
  22.      return returnValue;     
  23. }   

在C# 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#]

  1. public bool FindMyText(string text)     
  2. {     
  3.    // Initialize the return value to false by default.     
  4.    bool returnValue = false;     
  5.    // Ensure a search string has been specified.     
  6.    if (text.Length > 0)      
  7.    {     
  8.        // Obtain the location of the search string in richTextBox1.     
  9.        int indexToText = richTextBox1.Find(text, RichTextBoxFinds.MatchCase);     
  10.        // Determine if the text was found in richTextBox1.     
  11.        if(indexToText >= 0)     
  12.        {     
  13.          returnValue = true;     
  14.        }     
  15.    }     
  16.    return returnValue;     
  17. }   

在C# RichTextBox控件的某個文本范圍中搜索字符列表的某個字符的***個實例。

[C#] public int Find(char[], int, int);

在對搜索應用特定選項的情況下,在 RichTextBox 控件的文本中搜索位于控件內特定位置的字符串。

[C#] public int Find(string, int, RichTextBoxFinds);

在對搜索應用特定選項的情況下,在 RichTextBox 控件文本中搜索控件內某個文本范圍內的字符串。

【編輯推薦】

  1. 總結C#哈希表的用法
  2. 不一樣的入門:看C# Hello World的17種寫法
  3. 什么是WMI?及其示例
  4. 從C#到C++容易出現的問題解答
  5. 淺議.NET、ASP.NET和C#的關系
責任編輯:book05 來源: csdn
相關推薦

2024-05-21 11:09:17

2009-08-28 16:31:21

C# treeview

2009-08-28 17:01:43

C#構造函數

2009-09-08 14:54:40

C# listBox控

2009-09-01 15:25:04

C# default關

2010-01-25 14:10:21

C++堆棧

2009-12-02 16:04:44

PHP fsockop

2009-12-28 13:28:03

WPF視頻

2011-06-01 15:54:41

log4net

2011-06-01 16:08:29

log4Net

2009-11-30 17:43:54

PHP split()

2009-09-02 13:15:23

C#數組復制

2009-08-20 13:47:57

C#驗證輸入方法

2025-05-12 03:10:00

接口方法代碼

2011-08-10 17:16:01

Objective-C靜態變量

2010-06-01 19:55:30

SVN使用

2010-06-03 17:38:03

Hadoop命令

2010-10-09 10:30:03

JS event

2010-01-28 17:07:03

Android Gal

2009-09-09 10:32:12

C# CheckBox
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区二区在线 | 神马久久久久久久久久 | 玖玖久久| 久久99国产精一区二区三区 | 午夜成人免费视频 | 91天堂网| 免费午夜剧场 | 夜夜操天天艹 | 精品视频一区二区三区在线观看 | 亚洲精品乱码久久久久久9色 | 91亚洲精华国产 | 成人国产精品久久久 | 成人免费共享视频 | 久久视频一区 | 中文字幕av在线 | 色成人免费网站 | 国产激情在线 | 亚洲免费在线观看 | 国产色 | 久久久久网站 | 亚洲男人天堂av | av久久 | 成人亚洲片 | 福利视频1000 | 超碰人人插 | 日本成人福利视频 | 欧美一级在线免费观看 | 久久免费国产 | 中文字幕 国产 | 日本高清aⅴ毛片免费 | 成人国产毛片 | 日本精品视频一区二区 | 一级黄色片免费 | 国产精品久久久久久久久久久久久 | 精品国产99 | 久久精品国产一区二区三区 | 九九色综合 | 婷婷在线免费 | 99国产精品久久久 | 黄免费观看 | 国产精品久久一区 |