Hello! I just started to deal with C #, I want to draw a moving circle. Wrote such methods:
private void draw() { Pen myPen = new Pen(Color.Black); g.DrawRectangle(myPen, 10, 10, 50, 50); g.DrawEllipse(myPen, (float)xStart, (float)yStart, 5, 5); } private void updateCoords(object obj) { if (xStart < 10) dx = -dx; if (xStart > 60) dx = -dx; if (yStart < 10) dy = -dy; if (yStart > 60) dy = -dy; xStart = xStart + dx; yStart = yStart + dy; draw(); } private void button1_Click(object sender, EventArgs e) { int time = 0; TimerCallback tc = new TimerCallback(updateCoords); System.Threading.Timer timer = new System.Threading.Timer(tc, ++time, 0, 100); } When I press a button, the circle starts moving, but every time it is drawn again, the old circles do not disappear, i.e. many circles displayed at once. Tell me, please, how to make it so that at each moment only one circle is displayed in the current position. I use Visual Studio 2015.
Added by
Graphics g; // определяется в классе, но снаружи методов public Form1() // есть у меня такой метод ещё { InitializeComponent(); g = panel1.CreateGraphics(); }
g? - IgorOnRender()method or something like that. - LLENNOnPaint()- LLENN