D
D
Danil Tolkachev2018-04-05 19:48:30
C++ / C#
Danil Tolkachev, 2018-04-05 19:48:30

How to make a button on a form move to a random place on the form every 5 seconds?

How to make a button on a form move to a random place on the form every 5 seconds? I just recently started studying seasharp, please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2018-04-05
@fray_danil

Something like this.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace RandomButton
{
    public partial class Form1 : Form
    {

        Point templocation = new Point(0, 0);

        Timer timer = new Timer();
        Random rnd = new Random();

        public Form1()
        {
            InitializeComponent();
            timer.Tick += Timer_Tick;
            timer.Interval = 5000;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            button1.Location = templocation;
            templocation.X = rnd.Next(10,200);
            button1.Location = templocation;
            templocation.Y = rnd.Next(10,200);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Start();
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question