Image drag-drop & passed as variable

Nikolis

New Member
Joined
Sep 12, 2022
Messages
27
Office Version
  1. 2007
Platform
  1. Windows
I have a list of countries in the worksheet, with different things (capital, flag etc.) associated with it. What I would like to do is create a geography "game" where a Userform appears and 10 flags & 10 countries names are randomly selected from the list of 200+ entrances. They are listed in 2 columns of the Userform and the player must drag the flag besides the (appropriate) country name.

What I have thought is create a new object, e.g. named Country, where the properties would be name & capital (as strings), flag (image), population etc.

Where I would need help is 3 points ;
(1) can a circular image (the flag, in our case) have "Drag & Drop" behavior activated or somehow set ?

(2) the selected image to be dragged-dropped is the Country.Image for the "correct country" (which is one of the 10). So, somehow I need to make the code recognize if the specific image is the one linked to the specific country. What kind of criteria would actually help the code recognize what is what and materialize this step ?

(3) because there are over 200+ countries I would need to describe step #2 (above) with a procedure and pass the image value in it, e.g. sub determine_flag, with 2 variables, one for the image and the other one for the country.
Like ;
sub determine_flag(French_flag, Denmark) would be a wrong answer (like "False"), while
sub determine_flag(French_flag, France) would produce a correct answer (like "True").
How can I implement the image in it, e.g. through the filename ? As a form control ? As image ? How do we describe it ?
I mean when the image is clicked (OnClick), with or without Drag&Drop following, ... what sort of information does this produce for us code-wise ?

Maybe my inexperience makes me miss something even from the planning stage. I'm not necessarily expecting to get the code complete but any directions would help, even as food for thought.

Thank you in advance.
 
GameMode could also be one parameter of the Settings.

Another one would be a checkbox where "World", "Europe" etc. could be selected, so that flags from specific continent(s) would be isolated and the rest to be left out for this round of the game.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
This is the result:
MoveableObjects.gif
 
Upvote 0
About the point you mentioned ("if there's already a flag in front of the country name") ;
- a variable e.g. "GameMode" may be declared, can be set to e.g. either straight (0) or multiple (1). Or even Boolean, as there are only 2 potential cases.
Where straight would mean that you get to link a flag to a country only once. Then the flag holder locks this position. "You can't undo", it's "one-off", in other words.
Multiple would be more flexible, thus allowing you to rethink and retry if you feel you made a mistake. So, this game mode would allow the user to undo and change their initial decision about a country.
Ok, that shouldn't be too difficult. It already locks the flag icon in place (and is therefore immovable) if the answer is correct. It won't be a huge hassle to adjust it to allow for the straight/multiple configurations you mention.
 
Upvote 0
It's great. And I appreciate your assistance !Is there also a way to ensure that each flag, taken from the flags "column" may only be dragged to one of the 10 holders of the "holders column" ?
 
Upvote 0
... where's the code ? Did you delete that post, the one with the code ?
 
Upvote 0
GameMode could also be one parameter of the Settings.

Another one would be a checkbox where "World", "Europe" etc. could be selected, so that flags from specific continent(s) would be isolated and the rest to be left out for this round of the game.
I was thinking of something kinda like that - it would be good to have levels of difficulty - and get VBA to randomly select flags from similar / easily mistakeable flags for the more advanced settings (Here, I'm thinking [Insert European country name] or [Insert country of former Commonwealth colony which has the same Southern Cross - eg: Austraila, NZ, Fiji, etc].
 
Upvote 0
Sure thing! I'll make sure I put comments in the final code so that you can follow along, and feel free to ask any questions as you go.

In terms of moving UserForm Controls around the UserForm, my favourite code/routine is set out below. If you put a Label (named Label1) on a fresh userform, and make it big and change it's backcolor property, then just replace everything in the userform with this code:

Excel Formula:
Private mx As Single
Private my As Single
Private bDragging As Boolean
Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    mx = X
    my = Y
    bDragging = True
End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If bDragging Then
        Label1.Left = Label1.Left + X - mx
        Label1.Top = Label1.Top + Y - my
    End If
End Sub

Private Sub Label1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    bDragging = False
End Sub
This is the code to move the label. I need to tidy up the actual code because I was just haphazardly tinkering with it earlier today before dinner.
 
Upvote 0
It's great. And I appreciate your assistance !Is there also a way to ensure that each flag, taken from the flags "column" may only be dragged to one of the 10 holders of the "holders column" ?
Meaning you only get one shot?
 
Upvote 0
... where's the code ? Did you delete that post, the one with the code ?
No no, haven't deleted anything. I've put the routine for moving controls around the Userform above.
 
Upvote 0
Going deeper with the settings I guess would involve ;
- adding a Region and Continent properties to the Country,
- adding tags (cross, lines, number of different colors etc.) to each Flag
Meaning you only get one shot?
Yes, for each time you drag. I mean you have one flag, you drag it and drop it to one holder.

Which holder may be circular, in the same dimensions with the flags. Different picture for the holder when it's empty that to when it contains a flag (like a button which has "normal state" and "pressed state").
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top