You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁止用户编辑动态添加到TableLayoutPanel中的RichTextBox文本?

解决RichTextBox禁止编辑且不灰显的问题

你可以通过设置ReadOnly属性为true来实现禁止用户编辑文本,同时保持控件外观正常(不会灰显)。无需保留SelectionProtected,因为ReadOnly已经能完全阻止编辑操作。

修改后的代码如下:

RichTextBox[,] textList = new RichTextBox[4, 4];
for (int a = 0; a < 4; a++)
{
    for (int b = 0; b < 4; b++)
    {
        RichTextBox newBox = new RichTextBox 
        { 
            Dock = DockStyle.Fill, 
            Text = "2", 
            BackColor = Color.AntiqueWhite, 
            BorderStyle = BorderStyle.None, 
            SelectionAlignment = HorizontalAlignment.Center, 
            Font = new Font("Arial", 36, FontStyle.Bold),
            ReadOnly = true // 关键设置:禁止编辑且保持正常显示
        };
        textList[a, b] = newBox;
        tableLayoutPanel1.Controls.Add(newBox, a, b);
    }
}

说明

  • ReadOnly = true:使RichTextBox进入只读状态,用户无法输入或修改文本,但控件的字体、背景色等样式不受影响,不会像Enabled = false那样出现灰显。
  • 移除SelectionProtected = true:该属性仅用于保护部分选定内容,无法完全阻止编辑,因此不需要保留。

内容的提问来源于stack exchange,提问作者forefie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 01:08:14