Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I would like to find how to randomly move the location of a button over time.
Posted
Updated 7-Jan-11 5:08am
v4
Comments
jerrykid 7-Jan-11 8:42am    
not clear :)
Nuri Ismail 7-Jan-11 8:46am    
Please try to be more clear and specific.
#realJSOP 7-Jan-11 8:54am    
This makes absolutely no sense at all.
Yusuf 7-Jan-11 9:35am    
your question is not clear.
OriginalGriff 7-Jan-11 11:03am    
Answer updated

1 solution

Your question is not at all clear: "any control in form can crossing in me form" does not make sense. Please edit you question and try to explain in more detail. Google Translate may be of assistance here.

The random time bit is easier to cope with:

In your form class add two variables:
private int tenthsOfSeconds = 0;
private Random randomGenerator = new Random();

In your Load event (or button click if you want it started by a button):
Timer myTimer = new Timer();
myTimer.Interval = 100;       // 100 milliseconds == 1/10th of a second
tenthsOfSeconds = randomGenerator.Next(100);    // Random time, up to 10 seconds
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Start();
Add the event handler:
C#
void myTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
    if (tenthsOfSeconds > 0)
        {
        tenthsOfSeconds--;
        if (tenthsOfSeconds == 0)
            {
            tenthsOfSeconds = randomGenerator.Next(100);
            // Do your control stuff
            }
        }
    }



"i like to find how button location move in form random with time ."

Use the code above, and where my comment "Do your control stuff" is, add the code:
int xOffset = randomGenerator.Next(-20, 21);
int yOffset = randomGenerator.Next(-20, 21);
myControlIWantToMove.Location = new Point(myControlIWantToMove.Location.X + xOffset, myControlIWantToMove.Location.Y + yOffset);

It will need some fine tuning or it will run off the edge of your form, but that's the idea anyway.
 
Share this answer
 
v2
Comments
Dylan Morley 7-Jan-11 11:09am    
+5 for bothering to answer in detail to a random question!
thatraja 7-Jan-11 11:58am    
Good effort 5!
Sergey Alexandrovich Kryukov 16-Jan-11 17:04pm    
Too good for the question which makes so little sense - a 5.

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