Reputation: 765
I'm using a RichTextBox
in Windows Forms without any specialties.
When I change the textbox's read-only property in the constructor everything works as expected:
public MyForm()
{
...
tbDoc.ReadOnly = true;
}
When I do the same after the first display e.g., on a button click
private void BnDoSth_Click(object sender, EventArgs e)
{
tbDoc.ReadOnly = !tbDoc.ReadOnly;
}
the read only functionality only gets partly applied.
Entering text is no more possible (as expected) but the changed background color does not get reflected in the control.
It either stays gray or white. In the debugger I can verify that the property BackGroundColor
changes accordingly.
Invalidating the text box or the whole form does not help.
Current workaround is to recreate the textbox but that seems a bit over the top to me...
Any suggestions / similar experiences?
Upvotes: 3
Views: 48
Reputation: 109567
This seems to be due to some strange handling of the background colour in a RichTextBox
.
I found a somewhat hacky way of fixing it:
(1) Derive a new class from RichTextBox
as follows:
public sealed class MyRichTextBox : RichTextBox
{
protected override void OnReadOnlyChanged(EventArgs e)
{
base.OnReadOnlyChanged(e);
OnBackColorChanged(EventArgs.Empty);
}
}
(2) In your form's "Designer.cs" file, change all instances of RichTextBox
to MyRichTextBox
.
(3) Now when you change the ReadOnly
status, the background colour will update correctly.
Upvotes: 2