Same code over and over : Class module?

  • Thread starter Thread starter CAA
  • Start date Start date
C

CAA

Hello,
I'm struggling with the concept of a class module
i have a click event which i need to add to 30 or so images on a form.

Anybody know a good Class module tutorial or help me along a little, or
even if it is possible to do this!!
some code for you>

Private Sub S1_MouseDown(ByVal Button As Integer, ByVal _Shift As
Integer, ByVal x As Single, ByVal Y As Single)
If Button = 2 Then
ShN = 1
Call shFurn
Else
ShN = 1
Call ShWdt
End If
End Sub

Private Sub S2_MouseDown(ByVal Button As Integer, ByVal Shift_ As
Integer, ByVal x As Single, ByVal Y As Single)
If Button = 2 Then
ShN = 2
Call shFurn
Else
ShN = 2
Call ShWdt
End If
End Sub

Private Sub S3_MouseDown(ByVal Button As Integer, ByVal_ Shift As
Integer, ByVal x As Single, ByVal Y As Single)
If Button = 2 Then
ShN = 3
Call shFurn
Else
ShN = 3
Call ShWdt
End If
End Sub

These go on and on and make my eyes hurt whilst filling a large section
of code.

Thanks for looking
CAA
 
Hi Caa,
Anybody know a good Class module tutorial or help me along a little, or
even if it is possible to do this!!

Yes it is. Create a class module called CImageEvent and add the
following code (where I've used public variables instead of Property
Let/Get/Set for simplicity):

Public WithEvents ImageControl As MSForms.Image
Public sNumber As Long

Private Sub ImageControl_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

'Your code here
End Sub

Then in your userform code, you'd have:


'A collection of the event handler classes
Dim moImageEvents As Collection

Private Sub UserForm_Initialize()

Dim oImageEvent As CImageEvent
Dim oCtl As MSForms.Control

'Initialise the collection
Set moImageEvents = New Collection

'Loop through all the controls
For Each oCtl In Me.Controls

'Is it an image control?
If TypeOf oCtl Is MSForms.Image Then

'Yes, so give us a new event handler class
Set oImageEvent = New CImageEvent

'Initiliase it
Set oImageEvent.ImageControl = oCtl
oImageEvent.sNumber = Val(Mid$(oCtl.Name, 2))

'And add it to the collection
moImageEvents.Add oImageEvent
End If
Next

End Sub

Private Sub S1_MouseDown(ByVal Button As Integer, ByVal _Shift As
Integer, ByVal x As Single, ByVal Y As Single)
If Button = 2 Then
ShN = 1
Call shFurn
Else
ShN = 1
Call ShWdt
End If
End Sub

Personally, I'd avoid the complexity and use a common routine:

Sub ProcessImageClicks(ByVal Button As Integer, ByVal lImage As Long)
ShN = lImage

If Button = 2 Then
Call shFurn
Else
Call ShWdt
End If
End Sub

then call it for each button's click

Private Sub S1_MouseDown(ByVal Button As Integer, ByVal _
Shift As Integer, ByVal x As Single, ByVal Y As Single)

ProcessImageClicks Button, 1
End Sub


Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie
 
Thanks for you help Stephen

I can see what you mean about complexity, I don't really understand the
class module approach and i'm not really keen on using things i don't
understand, although i will give it a good play about with untill i do.
Any suggestions of a good guide?

The second solution worked fine. Although the value ShN did not relate
to the image being clicked.

Sub ProcessImageClicks(ByVal Button As Integer, ByVal lImage As Long)
ShN = lImage <what's this actually do?

The form has a few images and they have different names, i need to set
the value of shN depndant on the current selected image as well as
runninga bit of code to bring up some textboxes and the like.

Thanks again
CAA
 
Hi Caa,
The second solution worked fine. Although the value ShN did not relate
to the image being clicked.

Sub ProcessImageClicks(ByVal Button As Integer, ByVal lImage As Long)
ShN = lImage <what's this actually do?

That's what the lImage is for - passing in the number for the image to
the common routine.

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie
 
OH, i get it now

I need to change the value at the end of the image clicks

ProcessImageClicks Button, 3 <this one to set which button.

Great stuff, it does clean the code up a bit, Thankyou once again.
was a bit worried i'd lost the plot there.


CA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top