當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# RichTextBox用法及代碼示例

在 C# 中,RichTextBox 控件是一個文本框,它為您提供富文本編輯控件和高級格式化函數,還包括加載富文本格式 (RTF) 文件。或者換句話說,RichTextBox控件允許您顯示或編輯流內容,包括段落、圖像、表格等。RichTextBox類用於表示windows富文本框,還提供不同類型的屬性、方法和事件。它的定義如下係統.Windows.窗體命名空間。
它沒有像 TextBox 控件那樣的 64K 字符容量限製。它用於提供類似於 Microsoft Word 等文字處理應用程序的文本操作和顯示函數。在 C# 中,您可以使用兩種不同的方式在 Windows 窗體中創建RichTextBox:

1. Design-Time:這是創建RichTextBox的最簡單方法,如下步驟所示:

  • 步驟1:創建一個windows窗體,如下圖所示:
    Visual Studio -> 文件 -> 新建 -> 項目 -> WindowsFormApp

  • 第2步:接下來,將 RichTextBox 控件從工具箱拖放到窗體中。
  • 步驟3:拖放後,您將轉到RichTextBox控件的屬性,根據您的要求修改RichTextBox。

    輸出:

2.運行時:它比上麵的方法稍微棘手一些。在此方法中,您可以借助RichTextBox 類提供的語法以編程方式創建RichTextBox 控件。以下步驟顯示如何動態設置創建RichTextBox:

  • 步驟1:使用RichTextBox 類提供的RichTextBox() 構造函數創建RichTextBox 控件。
    // Creating a RichTextBox control
    RichTextBox box = new RichTextBox(); 
    
  • 第2步:創建RichTextBox控件後,設置RichTextBox類提供的RichTextBox控件的屬性。
    // Setting the location 
    // of the RichTextBox
    box.Location = new Point(236, 97); 
    
    // Setting the background
    // color of the RichTextBox
    box.BackColor = Color.Aqua; 
    
    // Setting the text 
    // in the RichTextBox
    box.Text = "!..Welcome to GeeksforGeeks..!"; 
    
  • 步驟3:最後使用以下語句將此 RichTextBox 控件添加到表單中:
    // Adding this RichTextBox
    // in the form 
    this.Controls.Add(box); 
    

    例子:

    
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
      
    namespace WindowsFormsApp30 { 
      
    public partial class Form1 : Form { 
      
        public Form1() 
        { 
            InitializeComponent(); 
        } 
      
        private void Form1_Load(object sender, EventArgs e) 
        { 
            // Creating and setting the 
            // properties of the label 
            Label lb = new Label(); 
            lb.Location = new Point(251, 70); 
            lb.Text = "Enter Text"; 
      
            // Adding this label in the form 
            this.Controls.Add(lb); 
      
            // Creating and setting the 
            // properties of the RichTextBox 
            RichTextBox box = new RichTextBox(); 
            box.Location = new Point(236, 97); 
            box.BackColor = Color.Aqua; 
            box.Text = "!..Welcome to GeeksforGeeks..!"; 
      
            // Adding this RichTextBox in the form 
            this.Controls.Add(box); 
        } 
    } 
    } 

    輸出:

Constructor

構造函數 說明
RichTextBox() 此構造函數用於初始化 RichTextBox 類的新實例。

Properties

屬性 說明
AutoSize 此屬性用於獲取或設置一個值,該值指示控件是否根據其內容調整大小。
BackColor 該屬性用於獲取或設置控件的背景顏色。
BorderStyle 該屬性指示控件的邊框樣式。
Font 該屬性用於獲取或設置控件顯示的文本的字體。
ForeColor 該屬性用於獲取或設置控件的前景色。
Height 該屬性用於獲取或設置控件的高度。
Location 該屬性用於獲取或設置 RichTextBox 控件的左上角相對於其窗體左上角的坐標。
Name 該屬性用於獲取或設置控件的名稱。
TabStop 此屬性用於獲取或設置一個值,該值顯示用戶是否可以按 TAB 鍵為 NumericUpDown 提供焦點。
Size 該屬性用於獲取或設置控件的高度和寬度。
Text 此屬性用於獲取或設置要在 RichTextBox 控件中顯示的文本。
Visible 該屬性用於獲取或設置一個值,該值指示是否顯示該控件及其所有子控件。
Width 該屬性用於獲取或設置控件的寬度。
ZoomFactor 此屬性用於獲取或設置 RichTextBox 的當前縮放級別。
ShowSelectionMargin 此屬性用於獲取或設置一個值,該值指示 RichTextBox 中是否顯示選擇邊距。
SelectionTabs 此屬性用於獲取或設置 RichTextBox 控件中的絕對製表位位置。
SelectedText 此屬性用於獲取或設置 RichTextBox 中選定的文本。
ScrollBars 此屬性用於獲取或設置要在 RichTextBox 控件中顯示的滾動條類型。
Multiline 此屬性用於獲取或設置一個值,該值指示這是否是多行RichTextBox 控件。


相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 C# | RichTextBox Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。