I created a gradient for my form background but Im having this issue now. In winforms, when I minimize and then maximize the form i get this:
imageshack.us/photo/my-images/35/errorbl.png.
I guess the bug is that the form has to redraw itself after maximizing and it doesn’t. Somebody knows how to solve it?
Thanks!
Calling gradient:
public Base()
{
InitializeComponent();
this.Paint += new PaintEventHandler(Form_Background);
}
Gradient method:
public void Form_Background(object sender, PaintEventArgs e)
{
Color c1 = Color.FromArgb(255, 252, 254, 255);
Color c2 = Color.FromArgb(255, 247, 251, 253);
Color c3 = Color.FromArgb(255, 228, 239, 247);
Color c4 = Color.FromArgb(255, 217, 228, 238);
Color c5 = Color.FromArgb(255, 200, 212, 217);
Color c6 = Color.FromArgb(255, 177, 198, 215);
Color c7 = Color.FromArgb(255, 166, 186, 208);
LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, c1, c7, 90, true);
ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0, (float)0.146, (float)0.317, (float)0.439, (float)0.585, (float)0.797, 1 };
cb.Colors = new[] { c1, c2, c3, c4, c5, c6, c7 };
br.InterpolationColors = cb;
// paint
e.Graphics.FillRectangle(br, this.ClientRectangle);
}
The reason it does not work is that width and height of
this.ClientRectangleis zero after being minimized.You need to implement a check of the rectangle before applying any gradient and drawing with it:
So your code would like something like this: