Reputation: 3916
I am attempting to just draw basic shapes to a panel on my form. so far nothing happens and I dont know why. This method is called in the constructor of my form.
private void doGraphics()
{
Pen p = new Pen(Color.Black);//draws wire frame Shapes
SolidBrush sb = new SolidBrush(Color.Yellow);//draws filled Shapes
Graphics g = panel1.CreateGraphics();
Point[] pointArray = { new Point(100, 20), new Point(100, 0), new Point(120, 0), new Point(120, 20) };
g.FillPolygon(sb, pointArray);
g.DrawPolygon(p, pointArray);
}
Any suggestions would be great!
Upvotes: 2
Views: 225
Reputation: 26468
You need to register to the Paint
event of the panel and use the graphics object that comes with the arguments:
In constructor:
panel1.Paint += new PaintEventHandler(panel1_Paint);
The handler itself:
void panel1_Paint(object sender, PaintEventArgs e) {
{
Pen p = new Pen(Color.Black);//draws wire frame Shapes
SolidBrush sb = new SolidBrush(Color.Yellow);//draws filled Shapes
Graphics g = e.Graphics; // From Arguments
Point[] pointArray = { new Point(100, 20), new Point(100, 0), new Point(120, 0), new Point(120, 20) };
g.FillPolygon(sb, pointArray);
g.DrawPolygon(p, pointArray);
}
Upvotes: 2
Reputation: 50712
When you call this method in the constructor you can not assume that the child controls and/or Graphic object are available. Use the OnPaint method of the form or create a Custom Control and override the OnPaint there.
Upvotes: 0