Hello everyone,

I need help with a program I'm busy working on. I've created a test application, and I need it to move one pictureBox control (pictureBox1) to move to a random location (either 70,233 or 204,146 or 46,46) each time a button (button1) is clicked. Take note again that one of 3 locations should be randomly selected, each time the button's clicked.

I've seriously, for the life of me, been trying to work on this for half a month. I've tried many codes including rand(), so and so but nothings working.

If anyone could give me a hand, it would be very much appreciated.

Thanks in advance.

Recommended Answers

All 7 Replies

Do you know how to move the window? MoveWindow() will do the trick.

Selecting a random number between 1 and 3 is easy -- int n = (rand()%3) + 1

Thanks ancientdragon for your time, but I think maybe I didn't describe well enough :P
You see, I'm trying to move the actual picturebox, not the window. The random part is the computer selecting randomly one of the three locations I specified earlier, and then moving the picturebox to that randomly selected location of the three.

>You see, I'm trying to move the actual picturebox, not the window.
Just use the PictureBox::Location data member (remember, System.Windows.Forms.PictureBox is inherited from the System.Windows.Forms.Control class). Selecting one of three locations is easy -- use AD's random number generator example and run it through a switch or if else statement.

>>You see, I'm trying to move the actual picturebox, not the window
Everything is treated as a window -- combobox, listbox, picturebox, buttons, checkboxes -- all those are windows.

Thanks again, but yes, I know how to move the picturebox with the Picturebox1::Location.

I'm just a total noob, and am having trouble putting the suggestions into the correct code. I tried saying (rand()%3) but the only number I get is 3, nothing else at all.

Is there any way of giving an example of putting all the suggestions together? once again, I'm a total noob.

Take note that this isn't homework, It's a game I'm busy working on.

And one more thing:
>>use AD's random number generator example and run it through a switch or if else statement.

How would I do this? How to I tell the random generator to only put the location in 70,233, 204,146 or 46,46?

Please try to give me something a bit simpler to understand :(

>>How would I do this? How to I tell the random generator to only put the location in 70,233, 204,146 or 46,46?

#include <ctime> // for time() function
int main()
{
    // seed the random number generator
    srand( (unsigned int)time(0));
<snip>
   // somewhere in your program do something like this
   int nms[] = {70233, 204140, 4646};
   int location = nms[ rand() %3];
}

Thanks ancientdragon, now I know what you mean :)

Sorry, I must've sounded like such a retard :-/

I've added to your reputation, thanks again for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.