OnPaint problem
-
I am creating a UserControl that is a collection of Panles setup in a grid format. Each element of the grid is a Panel. I allow the user to add text to the panel in one of Nine locations described by the ContentAllingment Structure. The Text object is nothing more than a Label control. I'm using a gradient brush to paint the background of each panel in the grid and am setting the background color or each Label control to transparent. My problem is on the initial paintof the control, all the grid boxes are transparent until I move the window or resize the control. If I choose to not set the Label control's background property to transparent, then all the Panels get painted correctly, but the Label controls llok bad because they have a flat backgournd color while it's parent Panel gas a gradient background color. My question is, why would nothing be getting painted until the control is resized or moved. Here is the OnPaint handler override
protected override void OnPaint(PaintEventArgs e)
{
try
{
//paint the background
LinearGradientBrush brush;
brush = new LinearGradientBrush(
ClientRectangle.Location,
new Point(ClientRectangle.Left,ClientRectangle.Bottom),
this.BackColor,
ControlPaint.Light(this.BackColor));
e.Graphics.FillRectangle(brush, ClientRectangle);
brush.Dispose();//add labels here foreach(PanelLabel PL in LabelArray) { PanelLabel lb=null; bool LabelFound=false; foreach(Control ctrl in this.Controls) { if(ctrl is PanelLabel) { lb = (PanelLabel)ctrl; if(lb!=null) { ContentAlignment CA = (ContentAlignment)lb.SimpleLocation; if(lb.Text==PL.Text && CA==PL.SimpleLocation) { LabelFound=true; break; } } } else continue; } if(!LabelFound) { lb = new PanelLabel(); lb.Font=new Font(lb.Font,PL.TextStyle); lb.Text=PL.Text; lb.SimpleLocation=PL.SimpleLocation; lb.BackColor=Color.Transparent;//comment out this line allows painting } lb.Bounds=SetLabelLocation(lb,PL.SimpleLocation,e.Graphics); if(lb.Bounds!=Rectangle.Empty && lb.Bounds!=ClientRectangle) this.Controls.Add(lb); else throw new Exception("Label bounds are empty"); } base.OnPaint (e); } catch(Exception Err) { MessageBox.Show("On Paint Error:"+Err.Message); }
}
Any ideas are appreciated
-
I am creating a UserControl that is a collection of Panles setup in a grid format. Each element of the grid is a Panel. I allow the user to add text to the panel in one of Nine locations described by the ContentAllingment Structure. The Text object is nothing more than a Label control. I'm using a gradient brush to paint the background of each panel in the grid and am setting the background color or each Label control to transparent. My problem is on the initial paintof the control, all the grid boxes are transparent until I move the window or resize the control. If I choose to not set the Label control's background property to transparent, then all the Panels get painted correctly, but the Label controls llok bad because they have a flat backgournd color while it's parent Panel gas a gradient background color. My question is, why would nothing be getting painted until the control is resized or moved. Here is the OnPaint handler override
protected override void OnPaint(PaintEventArgs e)
{
try
{
//paint the background
LinearGradientBrush brush;
brush = new LinearGradientBrush(
ClientRectangle.Location,
new Point(ClientRectangle.Left,ClientRectangle.Bottom),
this.BackColor,
ControlPaint.Light(this.BackColor));
e.Graphics.FillRectangle(brush, ClientRectangle);
brush.Dispose();//add labels here foreach(PanelLabel PL in LabelArray) { PanelLabel lb=null; bool LabelFound=false; foreach(Control ctrl in this.Controls) { if(ctrl is PanelLabel) { lb = (PanelLabel)ctrl; if(lb!=null) { ContentAlignment CA = (ContentAlignment)lb.SimpleLocation; if(lb.Text==PL.Text && CA==PL.SimpleLocation) { LabelFound=true; break; } } } else continue; } if(!LabelFound) { lb = new PanelLabel(); lb.Font=new Font(lb.Font,PL.TextStyle); lb.Text=PL.Text; lb.SimpleLocation=PL.SimpleLocation; lb.BackColor=Color.Transparent;//comment out this line allows painting } lb.Bounds=SetLabelLocation(lb,PL.SimpleLocation,e.Graphics); if(lb.Bounds!=Rectangle.Empty && lb.Bounds!=ClientRectangle) this.Controls.Add(lb); else throw new Exception("Label bounds are empty"); } base.OnPaint (e); } catch(Exception Err) { MessageBox.Show("On Paint Error:"+Err.Message); }
}
Any ideas are appreciated
Don't I know I understood if well the problem, but and if you force the shot of the event?
protected override void OnResize(EventArgs e)
{
base.OnResize (e);
using (Graphics g = this.CreateGraphics())
{
this.OnPaint(new PaintEventArgs(g,
this.ClientRectangle));
}
}protected override void OnMove(EventArgs e)
{
base.OnMove (e);
using (Graphics g = this.CreateGraphics())
{
this.OnPaint(new PaintEventArgs(g,
this.ClientRectangle));
}
}Marcelo Palladino Brazil