Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# richtextbox

作者:当过家家——变成现实。   发布日期:2025-12-03   浏览:80

using System;
using System.Windows.Forms;

namespace RichTextBoxExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetupRichTextBox();
        }

        private void SetupRichTextBox()
        {
            // 创建一个新的 RichTextBox 控件
            RichTextBox richTextBox = new RichTextBox
            {
                Location = new System.Drawing.Point(12, 12),
                Size = new System.Drawing.Size(300, 200)
            };

            // 将 RichTextBox 添加到窗体中
            this.Controls.Add(richTextBox);

            // 设置一些初始文本
            richTextBox.Text = "这是一个 RichTextBox 示例。";

            // 设置部分文本的颜色和字体样式
            richTextBox.SelectionStart = 0;
            richTextBox.SelectionLength = 6;
            richTextBox.SelectionColor = System.Drawing.Color.Blue;
            richTextBox.SelectionFont = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);

            // 恢复默认选择
            richTextBox.Select(0, 0);
        }
    }
}

解释说明

  1. 创建 RichTextBox:

    • 使用 new RichTextBox() 创建一个新的 RichTextBox 控件。
    • 设置其位置 (Location) 和大小 (Size)。
  2. 添加控件到窗体:

    • 使用 this.Controls.Add(richTextBox)RichTextBox 添加到当前窗体中。
  3. 设置初始文本:

    • 使用 richTextBox.Text 设置 RichTextBox 的初始文本内容。
  4. 格式化部分文本:

    • 使用 SelectionStartSelectionLength 选择要格式化的文本范围。
    • 使用 SelectionColor 设置选中文本的颜色。
    • 使用 SelectionFont 设置选中文本的字体样式。
    • 最后使用 Select(0, 0) 恢复默认选择,使光标回到文本开头。

这个示例展示了如何在 C# 中使用 RichTextBox 控件,并对其中的部分文本进行格式化。

上一篇:c#时间戳

下一篇:c# assembly

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站