Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all, I'm new to C# and I'd like to include a new part inside my current project.

Actually I've done a form with a Timer and some buttons that start/pause the time displayed in a label.

Now what I'd like to do is make a 2D canvas with an image that moves from A (left) to B (right) on a line controlled by the timer tick created before.

IE: When I click on the start button, the program starts counting the time in the timer label and moving the image from A to B. When the image arrives at B, the timer stops.


Please can you help me coding this part?

Any help is appreciate, thanks!
Posted
Comments
TheRealSteveJudge 11-Feb-15 6:54am    
This is not a code writing factory.
Show us what you tried so far and where you have problems!

Hi

Just a sample :-

C#
Timer _timer = new Timer();
int _xfinal = 250;

private void btnStart_Click(object sender, EventArgs e)
{
    _timer.Interval = 100;
    _timer.Tick += _timer_Tick;
    _timer.Enabled = true;
    _timer.Start();
}

void _timer_Tick(object sender, EventArgs e)
{
    MoveObject();
}

private void MoveObject()
{
    int x = lblGO.Location.X;
    int y = lblGO.Location.Y;
    lblGO.Location = new Point(x + 1, y);

    if (x == _xfinal) _timer.Stop();
}




Regards
Dominic Abraham
 
Share this answer
 
Comments
Luca Donnaloia 25-Feb-15 5:09am    
Thank you
C#
Timer _timer = new Timer();
        int _xfinal = 250;

        private void btnStart_Click(object sender, EventArgs e)
        {
            _timer.Interval = 100;
            _timer.Tick += _timer_Tick;
            _timer.Enabled = true;
            _timer.Start();
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            MoveObject();
        }

        private void MoveObject()
        {
            int x = lblGO.Location.X;
            int y = lblGO.Location.Y;
            lblGO.Location = new Point(x + 1, y);

            if (x == _xfinal) _timer.Stop();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900