📜  C#| RichTextBox类别

📅  最后修改于: 2021-05-29 20:37:35             🧑  作者: Mango

在C#中,RichTextBox控件是一个文本框,可为您提供富文本编辑控件,而高级格式设置功能还包括加载富文本格式(RTF)文件。换句话说,RichTextBox控件允许您显示或编辑流内容,包括段落,图像,表格等。RichTextBox类用于表示Windows RTF文本框,还提供不同类型的属性,方法和事件。它在System.Windows.Forms命名空间下定义。
它没有像TextBox控件一样的64K字符容量限制。它用于提供类似于Microsoft Word之类的文字处理应用程序的文本处理和显示功能。在C#中,您可以使用两种不同的方式在Windows窗体中创建RichTextBox:

1.设计时:这是创建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 Description
RichTextBox() This Constructors is used to initialize a new instance of the RichTextBox class.

特性

Property Description
AutoSize This property is used to get or set a value that indicates whether the control resizes based on its contents.
BackColor This property is used to get or set the background color for the control.
BorderStyle This property indicates the border style for the control.
Font This property is used to get or set the font of the text displayed by the control.
ForeColor This property is used to get or set the foreground color of the control.
Height This property is used to get or set the height of the control.
Location This property is used to get or set the coordinates of the upper-left corner of the RichTextBox control relative to the upper-left corner of its form.
Name This property is used to get or set the name of the control.
TabStop This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the NumericUpDown.
Size This property is used to get or set the height and width of the control.
Text This property is used to get or set the text to be displayed in the RichTextBox control.
Visible This property is used to get or set a value indicating whether the control and all its child controls are displayed.
Width This property is used to get or set the width of the control.
ZoomFactor This property is used to get or set the current zoom level of the RichTextBox.
ShowSelectionMargin This property is used to get or set a value indicating whether a selection margin is displayed in the RichTextBox.
SelectionTabs This property is used to get or set the absolute tab stop positions in a RichTextBox control.
SelectedText This property is used to get or set the selected text within the RichTextBox.
ScrollBars This property is used to get or set the type of scroll bars to display in the RichTextBox control.
Multiline This property is used to get or set a value indicating whether this is a multiline RichTextBox control.