Random image selection on a form (1 Viewer)

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
Hi all,
I need help with random image selection from a form on a buton click event.To be more explicit: i have 7 images on form, when i push one buton i want to see how each image take focus (when take focus .BorderColor =vbRed after lose focus .BorderColor =vbWhite) and after lets say 20s focus stop random on one image and keep that image with .BorderColor =vbRed
If anybody have any ideea how to do this please help me.Thank you!
...and if u want to ask me for what i need this, i want to make a little game for my 5years daughter, something like rock-scissor-paper.. she like to play that very much :)
 
Last edited:

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
What i did until now:
1.on click event of buton i go change .BorderColor = vbRed on first image
2.i use this module to pause for 1s with: Call WaitFor(1)
Sub WaitFor(NumOfSeconds As Long)
Dim SngSec as Long
SngSec=Timer + NumOfSeconds

Do while timer < sngsec
DoEvents
Loop

End sub
3. i change back .BorderColor = vbWhite on that image
4. i pause again for 1s : Call WaitFor(1)
... and so on for all images, each one selected at least once

How to make this like a loop and stop randomly ?
 

vbaInet

AWF VIP
Local time
Today, 07:43
Joined
Jan 22, 2010
Messages
26,374
i want to make a little game for my 5years daughter, something like rock-scissor-paper.. she like to play that very much :)
That's nice of you! But why don't you play the rock-paper-scissors game by hand instead of creating a program :)

i have 7 images on form,...
... after lets say 20s focus stop random on one image
Confused! How many images are there, 7 or 20?
 

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
I play a lot with her but sometimes i dont have time and i want to give her a replacement for me :)
I have 7 images , that 20s it means 20 seconds.
 

vbaInet

AWF VIP
Local time
Today, 07:43
Joined
Jan 22, 2010
Messages
26,374
By her a life-size robot... might cost you a bit though :)

What you need is a randomizer between 1 and 7. The following link is how you would do it - swap 50 for 7:

http://www.excel-pratique.com/en/vba_tricks/generate_a_random_number.php

Your image controls will be named "img1", "img2", etc. That way you can refer to the image by doing this:
Code:
Me.Controls("img" & [COLOR="Blue"]TheRandomNumber[/COLOR]).BorderColor = vbRed
 

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
Done with
Code:
Randomize
TheRandomNumber = Int(7 * Rnd) + 1
Me.Controls("img" & TheRandomNumber).BorderColor = vbRed
Thank you very much!

I think would be nice if i will loop with .BorderColor = vbRed between all images for few seconds before Randomize but with my module is very slow waiting 1s between images. Can u help me with this too plz ?
 

vbaInet

AWF VIP
Local time
Today, 07:43
Joined
Jan 22, 2010
Messages
26,374
Sure... do that in the Timer event. For every second, randomize and change colour. Do this Until the second hits 20.
 

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
I did it simple

Code:
img1.BorderColor = vbRed
Call WaitFor(1)
img1.BorderColor = vbWhite
img2.BorderColor = vbRed
Call WaitFor(1)
img2.BorderColor = vbWhite
img3.BorderColor = vbRed
Call WaitFor(1)
img3.BorderColor = vbWhite
img4.BorderColor = vbRed
Call WaitFor(1)
img4.BorderColor = vbWhite
img5.BorderColor = vbRed
Call WaitFor(1)
img5.BorderColor = vbWhite
img6.BorderColor = vbRed
Call WaitFor(1)
img6.BorderColor = vbWhite
img7.BorderColor = vbRed
Call WaitFor(1)
img7.BorderColor = vbWhite
img8.BorderColor = vbRed
Call WaitFor(1)
img8.BorderColor = vbWhite

Randomize
TheRandomNumber = Int(7 * Rnd) + 1
Me.Controls("img" & TheRandomNumber).BorderColor = vbRed
Thank you very much for help!
I will come back if i stuck in something else :)
 

vbaInet

AWF VIP
Local time
Today, 07:43
Joined
Jan 22, 2010
Messages
26,374
Good job... but it's very verbose.

Use a Do Until loop.
 

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
:)
Code:
img1.BorderColor = vbRed
Call WaitFor(1)
Dim i As Long
i = 1
Do Until i > 7
Me.Controls("img" & i).BorderColor = vbWhite
Me.Controls("img" & i + 1).BorderColor = vbRed
Call WaitFor(1)
i = i + 1
Loop
img8.BorderColor = vbWhite

Randomize
TheRandomNumber = Int(7 * Rnd) + 1
Me.Controls("img" & TheRandomNumber).BorderColor = vbRed
 

vbaInet

AWF VIP
Local time
Today, 07:43
Joined
Jan 22, 2010
Messages
26,374
Good job! If you want to make it more fun to look at, Randomize it too ;)
And perhaps put a message saying, "daddy loves you" as well :) Just kidding on the last bit!
 

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
I made a screenshot of form, look atachment. U choose one pic from left side push start and after a loop on all images from right side one is selected.Now became interesting ...step 2: i will make the button start invisible and in center make appear those 2 pictures selected and the loser with one big X over image :)
 

Attachments

  • bingbangbum.jpg
    bingbangbum.jpg
    27.5 KB · Views: 62

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
I tried to move images with
Me!img1.Move Left:=[14,101], Top:=[9,497]
but i take error
Help me plz :)
I tried with changing .Left and .Top property
Me!img1.Left = "14,101"
Me!img1.Top = "9,497"
Something is wrong again, image go in the upper left corner :confused:
 
Last edited:

vbaInet

AWF VIP
Local time
Today, 07:43
Joined
Jan 22, 2010
Messages
26,374
Left and Top are Long data type but you're assigning it a string.
 

rvsebi

Registered User.
Local time
Today, 09:43
Joined
Jun 1, 2015
Messages
77
Thank you!
I read about twips unit of measure for Left and Top Property and i transformed my values in twips and all is ok now.
 

Users who are viewing this thread

Top Bottom