That code says to use the background color you set, if you had set it otherwise use the Control color if it is read only otherwise use the standard window color. So if you set the background color manually then that overwrites any standard Windows colors.
If you want to change the behavior then you could handle the ReadOnlyChanged
and/or EnabledChanged
to determine when the properties are changed and then adjust the background color accordingly. This would be the best route if you are using the standard TextBox
. However if you need to customize this for all your text boxes then create a derived type from TextBox
and override the BackColor property to suit your needs.
public override Color BackColor
{
get
{
if (Enabled && !ReadOnly)
return _backColor;
return base.BackColor;
}
set => base.BackColor = _backColor = value;
}
private Color _backColor = SystemColors.Window;