When you make a class that inherits Control or UserControl, and you want to overrides ForeColor or BackColor.
For example, you may want to use a brush to instead of color.
using System.Drawing;
public MyControl : Control
{
private Brush _fBrush = new SolidBrush(Color.Navy);
public overrides Color ForeColor()
{
get { return _fBrush.Color; }
set
{
_fBrush.Color = value;
this.Refresh();
}
}
}
It looks to make sense.
However, when you use "MyControl" in your application and change the ForeColor property to Color.Red in design mode, it might not generate the corresponding code in InitializeComponent() method.
Solution:
Just put the color to the base class, see the example below.
using System.Drawing;
public MyControl : Control
{
private Brush _fBrush = new SolidBrush(Color.Navy);
public overrides Color ForeColor()
{
get { return _fBrush.Color; }
set
{
_fBrush.Color = value;
base.Color = value;
this.Refresh();
}
}
}
It fixed, what the tragedy.
If anyone knows the theory of these, please shares your opinion. Thanks!
No comments:
Post a Comment