Click here to Skip to main content
15,301,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I move a picture from top to bottom and then get it in other coordinates?

I will explain my problem more clearly: I want the image to start at the top right corner of the form and go down, then appear at the top in other coordinates, for example (370,0), and go down again.
I wrote the code, but it just goes down and I don't know how to proceed.

What I have tried:

public moving()
{
InitializeComponent();
timer = new System.Threading.Timer(Callback, null, 0, 50);
}
private void Callback(object state)
{
BeginInvoke((MethodInvoker)delegate
{
pictureBox1.Location = new Point(0, pictureBox1.Location.Y + dtY);

// when it touches the bottom of the container
if (pictureBox1.Location.Y + pictureBox1.Size.Height >
pictureBox1.Parent.Height)
{
// we already reset once, so no more going back up: stop the timer
if (resetOnce)
{
timer.Change(Timeout.Infinite, Timeout.Infinite);

}
// we did not reset yet, so go to other coordinates for example(370,0)
else
{
resetOnce = true;
pictureBox1.Location = new Point(370,0);

}
}
});
Posted
Updated 15-Dec-21 21:18pm
v4

1 solution

You have hard-coded the location only if the picture box "bottom" goes below 0 - but then add an increasingly larger number to the Top property to a "magic" variable x.

You should be checking the current Top and Left of the Picture box then making a consistent adjustment - within the confines of the Form.

You will need to compare the relative Top of the picture box (or the bottom) with the size of the Form to determine when to move it from bottom of Form back to the top

I'm not providing specific code because this appears to be an assignment - I'm just going to point out where you are currently going wrong
   
Comments
Member 15461778 15-Dec-21 18:14pm
   
I've learned Visual basic so far, I can write my problem in Visual Basic, but now I want to learn C# and would be happy if you could help me write the code. (if it doesn't take away your time :))
Anyway thanks.
CHill60 16-Dec-21 3:09am
   
Use the Improve Question link to add this comment to your question and post your VB code also and I will see what I can do. If you reply to this comment when you have done that then I will get a notification
Member 15461778 16-Dec-21 3:15am
   
Ok. I did it:)
CHill60 16-Dec-21 7:29am
   
You misunderstood me. You said you had got this working in VB so if you post your working VB code alongside the C# code you have tried I can try to help you see where you have gone wrong

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