How can I stop the user being able to edit text in a richtextbox?

57 Views Asked by At

I have a short section of code which dynamically adds a richtextbox to each cell of a tablelayoutpanel, but the user shouldn't be able to edit the text inside.

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), SelectionProtected = true };
                    textList[a, b] = newBox;
                    tableLayoutPanel1.Controls.Add(newBox, a, b);
                }
            }

I have tried using SelectionProtected = true, but it doesn't prevent the user clicking on and editing the text, and Enabled = true makes the text appear greyed out.

1

There are 1 best solutions below

2
Jackdaw On

You can set ReadOnly property to true:

newBox.ReadOnly = true;
textList[a, b] = newBox;

See TextBoxBase.ReadOnly Property