PDA

View Full Version : multiple times same control wit same function after click



petroj02
10-02-2016, 06:05 AM
Hello all,
is here a way to make something like for Loop for private Sub Image_click() since i have at one userform multipletimes
Image button and for all of them have same some functions??
For example I have this....


Private Sub Image40_Click()
Dim counter As Integer
counter = 0
For i = 31 To 60
If Me("Image" & i).Visible = False Then
counter = counter + 1
End If
Next
If counter > 1 Then
Image40.Visible = True
Else
Image40.Visible = False
End If
End Sub
Private Sub Image41_Click()
Dim counter As Integer
counter = 0
For i = 31 To 60
If Me("Image" & i).Visible = False Then
counter = counter + 1
End If
Next
If counter > 1 Then
Image41.Visible = True
Else
Image41.Visible = False
End If
End Sub
Private Sub Image42_Click()
Dim counter As Integer
counter = 0
For i = 31 To 60
If Me("Image" & i).Visible = False Then
counter = counter + 1
End If
Next
If counter > 1 Then
Image42.Visible = True
Else
Image42.Visible = False
End If
End Sub
Private Sub Image43_Click()
Dim counter As Integer
counter = 0
For i = 31 To 60
If Me("Image" & i).Visible = False Then
counter = counter + 1
End If
Next
If counter > 1 Then
Image43.Visible = True
Else
Image43.Visible = False
End If
End Sub


and I would like to make one General Loop for These clicks....

Is it even possible?

Paul_Hossler
10-02-2016, 06:49 AM
The easiest way (not the most elegant) would be something like this where the common code is put in a sub that all can call





Option Explicit
Private Sub All_Click(O As Object)
Dim counter As Long, i As Long

counter = 0
For i = 31 To 60
If Me.Shapes("Image" & i).Visible = False Then
counter = counter + 1
End If
Next

O.Visible = (counter > 1)
End Sub

Private Sub Image40_Click()
Call All_Click(Me.Shapes("Image40"))
End Sub
Private Sub Image41_Click()
Call All_Click(Me.Shapes("Image41"))
End Sub

SamT
10-02-2016, 08:47 AM
To Paul: I thinks he wants a Control Event Class Module

To petroj02 (http://www.vbaexpress.com/forum/member.php?60799-petroj02),

Your code says: For each image, If 2 or more others are invisible, then this one is visible, but if 1 or none are invisible, then this one is invisible.

That is not going to work. All images start out visible. The first two images you click will turn invisible, the rest won't.

What are you trying to accomplish? What do you want to happen?

If you want to start with only 2 visible, then turn each next one visible in order

Sub Image32_Click()
Image32.visible = False
Image33.Visible = True
End Sub

If you want to start with all visible, then turn each invisible except the last one

Dim NumVisibleImages as Long

Private Sub UserForm_Intialize()
NumVisibleImages = 30
End Sub

All Image Click subs

Private Sub Image40_Click()

Image40.Visible = NumVisibleImages < 2
NumVisibleImages = NumVisibleImages -1
If NumVisibleImages = 0 then Do_Something_Else
End Sub

Paul_Hossler
10-02-2016, 09:26 AM
To SamT --
To Paul: I thinks he wants a Control Event Class Module

THAT would be the elegant way

petroj02
10-02-2016, 09:29 PM
To samT:
That is not going to work. "All images start out visible. The first two images you click will turn invisible, the rest won't."
I guess this is the exactly i want to happen..
It is going about easy Memory game in our Country known as "pexeso"
I have 30 Cards ("Images") and this Cards have 15 pairs randomly matched Pictures... User can Show just 2 Cards, no more no less... if These two Cards have the same Picture user will get a Point,if not Pictures will be again hidden... (This is in Progress)
The Show and hide Action, I did by adding next 30 Images (image31 to 60) which are located over the first 30 Pictures.... And i make these Pictures visible or invisible after click on them (but There cant be more then two Pictures shown together)...

The reason of my first question was i would like to make simplification of my code ,then just multiple times coppy the part of code to everysingle Image click Action after some Change I would made...
Maybe I dont exactly understand your solution, but in my oponion Paul solution is more suitable for me... (of course it's hard to give me the best advice since I dont give you Detail description of my issue...)

If I am wrong i would be grateful for you next insight

SamT
10-03-2016, 02:18 AM
I understand that you want to start with none visible

Then allow the User to 'turn over' over two. If they match get a point . . .And leave those two showing???
But, if they don't match, then no point and the two images are set not visible.

You have a total of 53 images, one is the card backside.
You need to randomly assign 15 of the faces to two each random Spots on the Play Board or UserForm.

I am thinking that you need the Image Control, then swap images in and out of each control as indicated. The image control has a Click Event and has a Tag Property.

With one card showing, the User must be able to click that card and 'turn it face down.'

The Sub PictureControler might look like:
Where Image Controls are named "Spot1" to "Spot30"

Private Sub SpotControler(SpotName As String)
Static PreviousSpot As Integer
Dim NoMatch As Boolean
Dim SpotIndex As Int

SpotIndex = CInt(Mid(SpotName, 5)))

If Controls("Spot" & SpottIndex).Tag = "Matched" then Exit Sub

If PreviousSpot = 0 Then 'No unmatched cards showing
PreviousSpot = SpotIndex
USerForm.Controls(SpotName).Picture = LoadPicture(Cards(SpotIndex)) 'Where Cards is a array holding Card image names
ElseIf SpotIndex = PreviousSpot Then 'Clicked on single visible card, Flip it over
USerForm.Controls(SpotName).Picture = LoadPicture("BackSide.Gif")
ElseIf Cards(SpotIndex) = Cards(PreviousSpot) Then
Points = Points + 1
Controls("Spot" & PreviousSpot).Tag = "Matched"
Controls("Spot" & SpottIndex).Tag = "Matched"
Else 'no match
NoMatch = True
'This part needs more thought. Nomatch has to work when any card clicked and no match. Maybe Dim ThisSpot As Integer
Controls("Spot" & PreviousSpot).Picture = LoadPicture("BackSide.Gif")
Controls("Spot" & ThisSpot).Picture = LoadPicture("BackSide.Gif")
PreviousSpot = 0
End If

On Card Click, Call SpotControler and pass it the ImageControl (Spot) Name.

That's all for now, It's late.