1

There is a code that activates the animation when double-clicking on Form1. It is necessary that the animation is triggered by double clicking on the ListBox component. How to change?

public partial class Form1 : Form
{
    Timer tmr;
    public Form1()
    {
        InitializeComponent();
        this.MouseDoubleClick += Form1_MouseDoubleClick;
        this.Paint += Form1_Paint;
        tmr = new Timer();
        tmr.Interval = 10;
        tmr.Tick += tmr_Tick;
    }

    int x;
    int step = 5; 
    void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        tmr.Stop();
        x = 0;
        tmr.Start();
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        x += step;
        if (x > this.Width)
        {
            x = 0;
            (sender as Timer).Stop();
        }
        this.Invalidate();  
    }
    void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.Red, 0, 0, x, 4);
    }

}
Knowledge Cube
  • 990
  • 12
  • 35
Gif
  • 23
  • 3
  • For handling a double-click event on a `ListBox` control, you may want to see [this question](https://stackoverflow.com/questions/4454423/c-sharp-listbox-item-double-click-event). Then, change your code above to attach this event handler to your `ListBox` control instead of `Form1`. – Knowledge Cube May 26 '17 at 17:43

1 Answers1

0

You need to start your timer:

tmr = new Timer();
tmr.Interval = 10;
tmr.Tick += tmr_Tick;
tmr.Start();

Also, a form doesn't need to listen to its own events, you can simply override the methods:

// this.Paint += Form1_Paint;

protected override void OnPaint(PaintEventArgs e) {
  e.Graphics.FillRectangle(Brushes.Red, 0, 0, x, 4);
}

Consider setting your form's DoubleBuffered property to true since you are drawing into that container.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • I do not understand. Can you create a project and send it to me? Sorry. – Gif May 26 '17 at 15:44
  • @Gif Understand what? I added one line of code. You can ignore the event / override issue if you don't understand that. – LarsTech May 26 '17 at 15:50
  • I understand. One more question. How do I make this animation not only on the top, but also on the left, right and bottom? – Gif May 26 '17 at 16:59
  • @Gif You are using an `x` variable for your width at the moment. I'm guessing you should use that for your left instead. Obviously, use a `y` for your top position. Use this.ClientRectangle instead of this.Width or this.Height since you only care about the interior space of your form. – LarsTech May 26 '17 at 17:40
  • Can you add this in response? – Gif May 26 '17 at 17:48
  • @Gif I don't know what you're trying to do. Move a square around the screen? Your code looks like a progress bar at the moment. – LarsTech May 26 '17 at 17:52
  • I just want the usual animation on the edges of the form. Button1 - two running strips on the left and right going to the bottom. Button2 - two running stripes left and right going up. I really need this, but I do not know how to do it. – Gif May 26 '17 at 18:30