blah blah blah is here! blah blah » Close

up0down
link

Hy,
im trying to find the code to clear the graphics i drew in a panel.

Graphics gVenster = panel1.CreateGraphics(); -> is what I used to make the panel ready for graph.


private void wissen_MouseClick(object sender, MouseEventArgs e)
{



}

I tried to put panel.Clear(); gVenster.Clear(); between the curly braces. the only thing that doenst give me an error is tekenpanel.Dispose(); but then my whole panel is gone..

Please help me beceause this is part of een exam in school... I'm new in C# en managed to program a program like paint that works, this is the only thing that doens't work..

THanks in advance!!

last answered one year ago

1 answers

up0down
link

Try something like this:

// declare private field
private Graphics gVenster = null;

// in method where you do the drawing
gVenster = panel1.CreateGraphics(); // now using the field
// etc

private void wissen_MouseClick(object sender, MouseEventArgs e)
{
if (gVenster != null)
{
gVenster.Clear(panel1.BackColor);
gVenster.Dispose();
gVenster = null;
}
}

Notice that gVenster needs to be a form level variable (i.e. a field), not a local variable, for this to work.

Feedback