Solved

VBA Probably a simple solution??!! Please help

Posted on 2003-11-13
15
274 Views
Last Modified: 2010-05-03
Hi Folks

I am reasonably new to VB and as a project i am trying to create a game of pairs! So far i have randomly generated 16 images on a form. Each image is covered by a CommandButton. When the button is clicked .visible = false and so the picture is revealed!

Now i am stuck! i don't know the code which I need to check to see if the two visible images on the screen are the same.

Just to confirm what i need, something like this

userform.visible.images = same then....!! (i know the syntax to the left is incorrect but i wanted to demo how i wanted to go about it)

Does anyone have any idea's?? Please

0
Comment
Question by:craigpike
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
What are you storing your picutres in (image control, picturebox, image list...), and how are you randomly displaying them?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
rather than trying to compare the images, you need to think a bit more ABSTRACTLY.  The images are simply a representation of the item, to be compared.  Let's say for instance, that you have a 4 x 4 grid of items.  This is 16 total, or 8 pairs, represented by 8 pairs of images.  But the 8 pairs could just as easily be 8 pairs of NUMBERS, and still be REPRESENTED by the 8 pairs of images.

so a 2 dimensional array  Items(4,4) to hold the 8 pairs   now each button corresponds to one cell in the array, and you simply compare the values associated with the two buttons, so see if the SAME number is in both cells.  The IMAGE that is displayed on the screen is associated with the NUMBER in the corresponding cell in the array.

so you have 8 numbers (1,2,3,4,5,6,7,8), and 8 Images (img1,img2,img3,img4,img5,img6,img7,img8)

you place the 8 NUMBERS randomly in the array, so that you have 2 occurences on any one NUMBER, and then using that array, place the images in the corresponding positions on the screen. so if the NUMBER at Items(1,1) is 8, then the image placed in the UPPER LEFT corner of the screen would be img8, under the Button#1 - I would also use a Control array for the Buttons (so that ALL of the Buttons have the same NAME, but are indexed 0 to 15 - thus button(0) corresponds to Items(1,1), button(3) corresponds to Items(1,4), and button(15) coresponds to Items(4,4))

AW

AW

0
 
LVL 1

Accepted Solution

by:
akari2000 earned 200 total points
Comment Utility
Just give each image its own number. Then check on number

like

image1 = 1
image2 = 2

if image1 = image 2 then

code

end if

something like that
0
 

Author Comment

by:craigpike
Comment Utility
I am actually doing this project in works time so i am using VBA which use imagebox i believe.

The images are 100% random, as you can see below they are not tagged in any way. The code that i have used to generate the images is probably very inefficient, but it works!

10  X1 = Int((8 * Rnd) + 1) ' Fills image frame A with rand pic!
    PicString = "c:\pix\pic" & X1 & ".jpg"
    A.Picture = LoadPicture(PicString)

20  X2 = Int((8 * Rnd) + 1) ' Fills image frame B with rand pic!
        Select Case X2
            Case Is = X1
                GoTo 20
            End Select
    PicString = "c:\pix\pic" & X2 & ".jpg"
    B.Picture = LoadPicture(PicString)

etc....
0
 
LVL 1

Expert Comment

by:InTheWind
Comment Utility
You have....

10  X1 = Int((8 * Rnd) + 1) ' Fills image frame A with rand pic!
    PicString = "c:\pix\pic" & X1 & ".jpg"
    A.Picture = LoadPicture(PicString)

if you added...

A.tag = PicString
.
.
.
B.tag = PicString

Then you just have to compare the .tag properties of the two pictures in a simple "if ... = ... then"
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
so do I understand that you are picked ALL of the images randomly?  and howmany images are possible.  What do you do if there are THREE instances of the same image (this is a possibility with what you have now).  What do you do if there is NO pair (that is an Image that DOES NOT have another copy)?

what you should do is this:

1) set up a single dimensioned array (Vals(8)) to hold the values 1 through 8

then scramble this array (put the 8 values in random order) this is also called SHUUFLING (as in shuffling a deck of cards - which scrambles the 52 cards into a random order - you just have 8 cards to put into a random order)

that way you are sure that you will use ALL 8 images, and then you can also be sure that EVERY image is present EXACTLT twice - hence EVERY image will make a PAIR.

now you go through the Vals array one at a time, and place each value in two distinct locations in the Items array (or place each image in your ImageBox).

This approach will make your overall programming problem MUCH easier to deal with.

AW

0
 
LVL 1

Expert Comment

by:InTheWind
Comment Utility
if you used an "image list" to do what AW is recommending then you would make this simpler.

I would think, in the case of an image list, all you need to do is...

for i = 0 to x (number of image controls)
  select an image (doing that in order would be fine)
  do while j <= 2
    randomly pick an image control
      if there is no image designated
        load image
        increment j
      else
        randomly pick an image control
   loop
next

doing this until you have loaded all the images will allow you to control the amount of time you spend randomly picking image controls to load a picture into. picking more images than you have image controls means that you would only do the first loop for the number of times that you have image controls to handle images, in reverse you might need to kick out of the for if you have reached that point.

hope that works for you.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:craigpike
Comment Utility
Thanks every one for your help, they are all great answers. I have learn't a lot from reading your articles.I am gonna have to digest some of these principles so I will be rewarding the points soon.

Thanks again, they are all very very helpfull so I will increase the points to 200

cheers
0
 
LVL 1

Expert Comment

by:InTheWind
Comment Utility
yeah, but who gets em? or are you going to give "assisted" solutions?

good luck
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
InTheWind, if you take ALL of the points and about $4.50 US, then you can go to Starbucks and get a Medium Mocha Latte!

If it's that important to you, then you can have mine, becuase I ca't use them for anything 'meaningful'

AW
0
 
LVL 1

Expert Comment

by:InTheWind
Comment Utility
haha

you're so right... and you need them so badly so you would feel that way of course! ;-)

Cheers... I mean, slurp.
0
 

Author Comment

by:craigpike
Comment Utility
Sorry, I have been STUPID and allocated the points to the wrong person. No offence akari2000 but i ment to give the points yo AW. Does any one know if this can be rectified??!!

Sorry AW
0
 
LVL 1

Expert Comment

by:InTheWind
Comment Utility
he said he didn't care about the points, so I wouldn't worry about it.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
i appreciate the fact that I *should* hae gotten the points (though, as I indicated, they aren't worth much, so no big deal).

After all, with all those points, can I retire?  Can I buy a new car? Can I even buy a small bag of French Fries at McDonald's .  I think not.  Bit it si the appreciation of 'peers' that IS a big deal.

AW
0
 
LVL 1

Expert Comment

by:InTheWind
Comment Utility
exactly and I thought he was being very nice by offering the points to someone... I thought that your reply to me was a little bit harsh considering his kind offer to increase the points. I was just trying to make the point that he had to actually make the offer into an "accepted answer"...

Enjoy your new "digital car" ;-)

TTFN
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now