Click here to Skip to main content
15,298,916 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!
I am using C # windows application.
I have one picture in the form. When the program starts, the picture should move from right to left, then from left to right, and that's as long as the program works. The dimensions of my form are: 816; 488.
I have that code BUT it only goes right and stops. :(
That's why I created 2 timers.

What I have tried:

public partial class Form1 : Form
{
int x = 408, y = 244;
void goBack(int x)
{
if (x >= 1)
{
x--;
timerleft.Start();
}
else
return;
goBack(x);
}

private void Form1_Load(object sender, EventArgs e)
{
timeFirst.Interval = 1;
timerleft.Interval = 1;
timeFirst.Start();
timerleft.Start();
}

private void timeFirst_Tick(object sender, EventArgs e)
{
lblanvanum.SetBounds(x, y, 408, 244);
x++;
if (x > 700)
{
timeFirst.Stop();
goBack(x);
}
}
}
Posted
Comments
Richard MacCutchan 11-Dec-21 12:23pm
   
This is quite a simple issue and only needs one loop. Start by displaying the object at the left of the screen. Set the 'move' value to +1, and start the timer. Each timer interrupt, move the object 'move' measures, i.e. add 'move' to the x position of the left edge and redraw. If 'move' is +1, check if the right edge has reached the end of the Window, and if so set the 'move' value to -1. If 'move' is -1 and the left edge has reached the left of the form reset the 'move' value to +1. This will then continue until you stop the program.
Member 15461778 11-Dec-21 18:25pm
   
Thank you very much.
BillWoodruff 12-Dec-21 1:22am
   
As Richard said, this is pretty simple, but, imho, a program that shows a moving picture until it exits is almost always annoying.

If you want the user to be able to do "other things" while the picture is being displayed, doing that in the SAME Form means you'll have to run it in a separate thread.
Member 15461778 13-Dec-21 8:35am
   
Well, thank you very much.
BillWoodruff 13-Dec-21 9:19am
   
Consider using a GIF file.

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