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.
You can set
ReadOnlyproperty totrue:See TextBoxBase.ReadOnly Property