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.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
HI - this seems like a fun idea. Is this the sort of thing you had in mind?

Geography.gif
 
Upvote 0
... yes sir ! :love:
What is it behind, plan-wise ?

And, further, maybe another column where (once we finish our linking) a green tick or red X will appear, depending whether the answer is right or wrong, respectively.
 
Upvote 0
Ok. Shouldn't be a problem. This does just become an exercise in matching flags with countries, though, right? Did you want to add some additional functionality? Like a Label up the top where the question/instructions could be added?
 
Upvote 0
Ok. Shouldn't be a problem. This does just become an exercise in matching flags with countries, though, right? Did you want to add some additional functionality? Like a Label up the top where the question/instructions could be added?
Not really, this should be easy even for me. But we can add that, too.
Or even a comment referring to the user's performance, like "5/10, still good but you can do better" etc. ... like an MsgBox or a Frame/Label at the bottom whose caption depends on the user's success.
 
Upvote 0
Now that I get to think more about it, we also need to ensure that each country (and each flag) only appear once every time the game is reset and a new list pops-up.
 
Upvote 0
Yeah - there are a few kinks that I have to work out that never occurred to me before embarking on this. Like what happens if there is already a flag infront of the country name, etc. Leave it with me, I'll have some time on Monday night after work to continue tinkering with it, if that's ok.
 
Upvote 0
Yes, it's certainly fine with me.

But, as much as I appreciate the solution to come, equally important for me as a novice, is the "background" (plan, tactics, mentality) of how to create drag 'n' drop for the images. A few lines would give me great directions and understanding. Thank you in advance !
 
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.
 
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
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
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