Need help creating a class event

sous2817

Well-known Member
Joined
Feb 22, 2008
Messages
2,276
Hello everyone,

Creating a class has always escaped me in VBA, but I think I'm in a spot that it can help me, so no better time to learn...Here's what I'm trying to do:

I'm working on some userforms and rather than the standard buttons, I'm using two images overlayed to create a "mouse over" effect. So every odd number image has this code:

Code:
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Image2.Visible = True
End Sub

and every even image has code like this:

Code:
Private Sub Image2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Image2.SpecialEffect = fmSpecialEffectSunken
End Sub

Private Sub Image2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Image2.SpecialEffect = fmSpecialEffectFlat
End Sub

Can someone point me in the direction of (or provide me with ;)) some class code that could save me from some redundant typing?

Thanks in advance!
 
You could also use an ImageList control to store the images at design time and then just alter the one in the image control.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Wow, thanks MickG!

The code I posted was sent to my by Smitty about a year ago...just forgot I saved it in my email's in box.

I could split the odd and even images in to different groups, but was having trouble sorting out this bit to isolate the image number:

Code:
Right(Ctrl.Name, Len(Ctrl.Name) - 5)

Nonetheless, I can't thank you enough for the code. I'll tuck this away as I have a feeling I'll be using it quite a bit moving forward.

Thanks again!
 
Upvote 0
You could also use an ImageList control to store the images at design time and then just alter the one in the image control.

Thanks for the tip rorya. The ImageList control is a new one for me. Would you be willing to mock up some code I can take a look at?
 
Upvote 0
Hi, Bits that might help !!!
I assumed all the odd numbered images were underneath and evens on top.
To define the images in the code, I places by images on the sheet (from picture file) and then copied them into the Image Picture Property by Copy (Ctrl+C) Paste (Ctrl+V).
Regards Mick
 
Upvote 0
Here's a rough example:

Class called CImagebutton:
Code:
Option Explicit

Private WithEvents img As msforms.Image
Public objImgList As MSComctlLib.ImageList
Private WithEvents frm As UserForm
Private lngImageNum As Long

Public Property Set Imagebutton(ByVal objImagebutton As msforms.Image)
   Set img = objImagebutton
   Set frm = img.Parent
End Property

Private Sub frm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   lngImageNum = 1
   img.Picture = objImgList.ListImages(1).Picture
End Sub

Private Sub img_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   If lngImageNum <> 2 Then
      lngImageNum = 2
      img.Picture = objImgList.ListImages(2).Picture
   End If
   img.SpecialEffect = fmSpecialEffectSunken
End Sub

Private Sub img_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   img.SpecialEffect = fmSpecialEffectFlat
End Sub

Userform code - assumes userform has an imagelist control with 2 pictures in it (You need a reference to the Windows Common Controls - mscomctl.ocx):
Code:
Option Explicit

Dim colImagebuttons As Collection
Private Sub UserForm_Initialize()
   Dim ctl As msforms.Control
   Dim objIBtn As CImageButton
   Set colImagebuttons = New Collection
   For Each ctl In Me.Controls
      If TypeOf ctl Is msforms.Image Then
         ctl.Picture = Me.ImageList1.ListImages(1).Picture
         Set objIBtn = New CImageButton
         Set objIBtn.Imagebutton = ctl
         Set objIBtn.objImgList = Me.ImageList1
         colImagebuttons.Add objIBtn
      End If
   Next ctl
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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