Userform image event handler for date picker?

mdeanh5252

New Member
Joined
Sep 5, 2016
Messages
32
Office Version
  1. 365
  2. 2010
  3. 2007
Platform
  1. Windows
I have been trying out different variation of class image event handlers that have not given me good results.

I am using the date picker created by Trevor Eyre and want to use the date picker with 14 separate calendar images (Image1, Image2, etc..) to populate the date variable to the textbox adjacent to the image.
I have for now an event handler for every Image1_mousedown event that works however I hoped to utilize a class event handler that can simplify things.
The textboxes names I am populating are not in sequence from each other (1, 2, 3, etc..) they are all different numbers (textbox9, 33, 50, etc..)

Side note: I modified the date picker to show up center of the screen where workbook is displaying (multiple monitors) however would be nice it was relative to the mouse position for where it clicked the image for example just right of the image size or something. I am sure this is possible just a bit over my head at the moment.

I am hoping someone out there has a code that can be effective to capture the ImageCtrl.name and give the corresponding textbox in my case (textbox9, 33, 50, 55, etc..) the date variable.

Please note: Yes I renamed my textbox controls to Date1, Date2, etc.. for a test, and figured id need figure out a loop similar to the Userform_Initialize code below that will rename my textboxes back to their original name (textbox9, 33, 50, 55, etc..) by utilizing the textbox.tag to recall the old name as I have a loop that runs to save all my controls values to a worksheet)

Any help is very appreciated!

Code example I am currently using to call each image click is:
VBA Code:
Private Sub Image1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim dateVariable As Date
    dateVariable = CalendarForm.GetDate
    If dateVariable <> 0 Then TextBox9.value = dateVariable
End Sub

Class code example that I cannot get to work is:
VBA Code:
Public WithEvents ImageCtrl As MSForms.Image

Private Sub ImageCtrl_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    'Dim currentPage As String
    'currentPage = Me.Parent.SelectedItem.Name
    
    Dim ctrlName As String
    ctrlName = Me.ImageCtrl.Name
    
    Dim txtBoxName As String
    txtBoxName = "Date" & Mid(ctrlName, 6) 'Assuming your textboxes are named Date1, Date2, etc.
    
    'Show the date picker and set the date in the corresponding textbox
    Dim dateVariable As Date
    dateVariable = CalendarForm.GetDate
    If dateVariable <> 0 Then
    Me.Parent.Controls(txtBoxName).value = dateVariable
End Sub

Userform_Initialize code example is:
VBA Code:
Private Sub UserForm_Initialize()
    For i = 1 To 14
        Dim imgHandler      As clsCalendarHandler
        Set imgHandler.ImageCtrl = Me.Controls("Image" & i)
    Next i
End Sub
 
Hi
to classify your Classify controls images
you need to instantiate as many class instances as images
in your example there you loop over the 14 images by classifying the image (i)
and replaces the previous one classified necessarily

it is not good it is therefore not good practice

this is the right method

VBA Code:
dim allsinstanceClass (1 to 14) as new clsCalendarHandler 'in top  module

Private Sub UserForm_Initialize()
    For i = 1 To 14
         Set allsinstanceClass.ImageCtrl = Me.Controls("Image" & i)
    Next i
End Sub
 
Upvote 0
Hi
to classify your Classify controls images
you need to instantiate as many class instances as images
in your example there you loop over the 14 images by classifying the image (i)
and replaces the previous one classified necessarily

it is not good it is therefore not good practice

this is the right method

VBA Code:
dim allsinstanceClass (1 to 14) as new clsCalendarHandler 'in top  module

Private Sub UserForm_Initialize()
    For i = 1 To 14
         Set allsinstanceClass.ImageCtrl = Me.Controls("Image" & i)
    Next i
End Sub
Patricktoulon1,
I have added your suggested revision to test and it returns compile error invalid qualifier.

I will say I am new to adapting code into class modules and the code example I offered above was where I am at thus far, there isn't a lot of information I could find that covers a class module for images. This is where I hoped anyone may have another solution or it could be that its not possible with images.

Thanks
 
Upvote 0
hoh! i'me very sorry excuse me ; its an error by me
the variable is an array
VBA Code:
dim allsinstanceClass (1 to 14) as new clsCalendarHandler 'in top  module

Private Sub UserForm_Initialize()
    For i = 1 To 14
         Set allsinstanceClass(i).ImageCtrl = Me.Controls("Image" & i)
    Next i
End Sub
but do that in the activate of the userform it's still better

excuse me once again:oops:
 
Upvote 0
Just sharing an update as I have gotten revised code to run on my userform and the following code will allow calendar images to run the date picker and return the selected date to the text box. I have tested it with tabs and in frames.

I would like this to by dynamic to reference any userform in which its used instead of having to set the destination to "userform1" in event the userform name is different. I couldn't find a way to reference the active userform with the class to accurately direct the date value to the txtbox name.

Any assistance on that aspect would be very appreciated.

Class Module (cls_CalendarHandler) Code:
VBA Code:
Option Explicit

Private WithEvents calImage As MSForms.Image

Public Sub InitialiseCalendarImage(calimg As MSForms.Image)
    Set calImage = calimg
End Sub

Private Sub calImage_Click()
    
    Dim ctrlName As String
    ctrlName = calImage.Name
    
    Dim txtBoxName As String
    txtBoxName = "Date" & Mid(ctrlName, 6) 'Assuming your textboxes are named Date1, Date2, etc.
    
    'Show the date picker and set the date in the corresponding textbox
    Dim dateVariable As Date
    dateVariable = CalendarForm.GetDate
    If dateVariable <> 0 Then UserForm1.Controls(txtBoxName).value = dateVariable 'Edit userform name as needed 
End Sub

Userform Code:
VBA Code:
Option Explicit

Public colCalImages As Collection

Private Sub UserForm_Initialize()
Dim ctl As control
Dim clsCalImage As cls_CalendarHandler

Set colCalImages = New Collection

For Each ctl In Me.Controls

    If TypeName(ctl) = "Image" Then
        Set clsCalImage = New cls_CalendarHandler
        clsCalImage.InitialiseCalendarImage ctl
        colCalImages.Add clsCalImage
    End If

Next ctl
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,226,829
Messages
6,193,199
Members
453,778
Latest member
RDJones45

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