I’m trying to access the Graphics object of a Panel by passing it as a parameter to a function in a different class but I receive an error saying “Parameter is not valid”.
This is the code I’m using in the panel class:
Graphics g = contentPan.CreateGraphics();//contentPan is a Panel
RectMaker rect_Maker = new RectMaker();
rect_Maker.Draw(g);
This is the function which resides in the RectMaker class:
public void Draw(Graphics g)
{
try
{
g.FillRectangles(Brushes.White, SmallRect);
g.DrawRectangles(Pens.Black, SmallRect);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Is it possible to access the Graphics object of a Panel from a different class?
If the panel is displaying a bitmap, you can generate a graphics object from that and then invalidate the panel to show the update.
Or just invalid the panel and draw the update with whatever new information you have from this other class.
Either way, you really have to do this work in the Panel’s paint event, which gets triggered when you invalidate it.