Handler redefinition with more arguments

Viewed 38

Trying to make ball animation on windows forms. The main problem how to store ball's parameters and paint it.

First of all, set timer which each tick will invoke paint event. But for this it's necessary to pass ball's arguments to handler.

So:

private void Animation_Load(object sender, EventArgs e)
{
        Balls ball = new Balls();

        Timer tmr1 = new Timer();
        tmr1.Tick += (sender, e) => moveBall(sender, e, ball);

        tmr1.Interval = 200;
        tmr1.Start();
} 

But I have problem CS0136 because of redeclaration sender and e.

Also, the same problem in new handler:

public void moveBall(object sender, EventArgs e, Balls ball)
{
            ...

            this.Paint += new PaintEventHandler((sender, e) => onPaint(sender, e, ball)); 
}

My paint handler looks like this:

private void onPaint(object sender, PaintEventArgs e, Balls ball)
{
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.Clear(BackColor);
    e.Graphics.FillEllipse(Brushes.Blue, ball.BallX, ball.BallY,
    ball.BallWidth, ball.BallHeight);
    e.Graphics.DrawEllipse(Pens.Black, ball.BallX, ball.BallY,
    ball.BallWidth, ball.BallHeight);
}

I know, it's possible to store coordinates and velocity of ball as static and then just use Refresh(), but it's have to use class.

0 Answers
Related