Hi everyone. I have a problem. I need to write a program where I can draw elements just by pressing the buttons on the form and I can do some operations with figures which I drew like (spin, delete, maximize/minimize). The task was given to me: use keys on keyboard in order to spin your object. I have a class:

static public List<object> index = new List<object>();
 public class RightFigure 
            {
               public void Draw(PictureBox picture)
                {
                    num = 60;
                    int pos = random.Next(300);
                    Graphics gr = picture.CreateGraphics();
                    gr.DrawRectangle(mypen, pos, num, num, num);

                }
                
                public void turn()
                {
                    DoubleAnimation da = new DoubleAnimation();
                    da.From = 0;
                    da.To = 360;
                    da.Duration = new Duration(TimeSpan.FromSeconds(3));
                    da.RepeatBehavior = RepeatBehavior.Forever;
                    RotateTransform rt = new RotateTransform();
                    (last objct from index).RenderTransform = rt;
                    rt.BeginAnimation(RotateTransform.AngleProperty, da);
                }
          }

and a method which works when I press form's button:

private void Kvardat_Click(object sender, EventArgs e) //outside Rightfigure class
            {
                RightFigure kvad = new RightFigure();
                index.Add(kvad);
                kvad.Draw(this.pictureBox1);
                
            }

and method which will work on button 'A'. When I press this button the figure is suppose
to spin.

protected override void OnKeyDown(KeyEventArgs e) //outside Rightfigure class
        {
            if (e.KeyCode == Keys.A)
            {
                //I need to get the last object from index
                //and send it to turn method.
            }

The problems are:

1). I don't know how to retrieve the last object from LIST index and how send it to turn() method. I can not see turn() method.
2). I don't know how to write (last_object from LIST index).RenderTransform = rt;

Can you help me please. I am new in programming and in C#.
Thank you for your attention.

you need to change

last objct from index).RenderTransform = rt;

to:

if(index.count>0)
(index[index.count-1]).RenderTransform = rt;

but you will need to cast "index[index.count-1]" to the type that has the "RenderTransform " property

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.