Results 1 to 12 of 12

Thread: [RESOLVED] Can't make image in userform clickable when created dynamically in other userform

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    5

    Resolved [RESOLVED] Can't make image in userform clickable when created dynamically in other userform

    Hi! I have created two userforms. The first one to allow the user to specify how many images to display in the the next userform. When this is done The second userform is started using .show.
    Then however i want to run a short sub when the pictures are clicked in the second user form. But i can't make this work. I get no errors. There is just nothing hapening when i click the images. There might be a very simple solution to this since I'm very new to Visual Basic.

    This is the code in the first userform that loads the images in to the next. I have edited filepaths because of security reasons. Sorry.

    Code:
    Private Sub UserForm_Initialize()
    
    RowBox.Value = 1
    CollumnBox.Value = 1
    
    'Add things to boxes. You can choose up to 6 times 6 images.
    For i = 1 To 6
        RowBox.AddItem i
        CollumnBox.AddItem i
    Next i
    
    End Sub
    Sub OKButton_Click()
    Dim i As Integer, j As Integer, k As Integer, l As Integer, o As Integer
    
    m = RowBox.Value
    n = CollumnBox.Value
    
    SWLTool.Width = n * 54 + 660
    SWLTool.Height = m * 108 + 443
    
    o = 1
    i = 1
    For j = 1 To m
        For k = 1 To n
            For l = 1 To 3
                Dim myImage As MSForms.Image
                Set myImage = otherUserform.COntrols.Add("Forms.Image.1", "Image" & i & l)
                With otherUserform.COntrols("Image" & i & l)
                    .Left = 10 + 54 * (k - 1)
                    .Top = 18 + 108 * (j - 1)
                    .Height = 108
                    .Width = 54
                    .PictureAlignment = 2
                    .PictureSizeMode = 3
                    .Enabled = True
                    .MousePointer = 2
                    If l = 1 Then
                        .picture = LoadPicture("filepath1" & o & ".bmp")
                        o = o + 1
                        .Visible = True
                    ElseIf l = 2 Then
                        .picture = LoadPicture("filepath2")
                        .Visible = False
                    ElseIf l = 3 Then
                        .picture = LoadPicture("filepath3")
                        .Visible = False
                    End If
                End With
            Next l
             i = i + 1
        Next k
    Next j
    
    Unload Me
    otherUserform.Show
    
    End Sub
    The second userform consists of a large amount of code-parts looking like this, but with different names for all different images.

    Code:
    Public Sub Image11_Click()   'with diferent numbers in the name to cover all pictures.
    Call RadioPlacement(1)
    End Sub
    
    ...
    ...
    
    Private Sub Image363_Click()
    Call RadioPlacement(36)
    End Sub
    
    
    Private Sub RadioPlacement(x)
    UnitBox.Value = x
    End Sub
    The images loads fine to the other userform but "Call Radioplacements(nn)" doesn't execute when i click them. I tried replacing that with a measge box to verify, but that wouldn't run either.
    I am very thankfull for any help!

    Best regards
    Granefelt

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Can't make image in userform clickable when created dynamically in other userform

    That's not vb.net code. Looks like classic vb...

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    5

    Re: Can't make image in userform clickable when created dynamically in other userform

    Im sorry. I didn't realize.
    If someone can point me in the right direction i can post it where it should be posted and maybe a moderator can remove this post.

    Thank you!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Can't make image in userform clickable when created dynamically in other userform

    Is it classic vb or office vba?

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    5

    Re: Can't make image in userform clickable when created dynamically in other userform

    It is office vba.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Can't make image in userform clickable when created dynamically in other userform


  7. #7

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    5

    Re: Can't make image in userform clickable when created dynamically in other userform

    Thank you!

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Can't make image in userform clickable when created dynamically in other userform

    Moved to Office Section.

    Welcome to the forums.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Can't make image in userform clickable when created dynamically in other userform

    what is unitbox?

    put a breakpoint in one of the click events, click that image and step through the code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can't make image in userform clickable when created dynamically in other userform

    The problem is that you simply define an object... but you give no indication that events should be listened to. This is normally done by using the WithEvents when defining the variable:
    Private WithEvents SomeObject as SomeType ...
    But it comes with a rerally big caveat:
    Quote Originally Posted by msdn
    You can use WithEvents only at class or module level. This means the declaration context for a WithEvents variable must be a class or module and cannot be a source file, namespace, structure, or procedure.
    Once you have dimedsomething with events, it shows up in the code browser like any other control and you can hook up any supported event.

    BUT... and this is a big but, events are raised to that instance... so if you use in a loop, creating a new instance each time, you'll lose the ability to catch the events of those created before.

    To be honest, I don't know if there's a way around that... be nice if there was. In .NET, I'd know how to do this, but with VBA, ... I'm not sure.

    It's also possible that everything I've written is completely wrong (I'd be OK with that, because this feels like one of those thing that should be possible).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Can't make image in userform clickable when created dynamically in other userform

    I don't know if there's a way around that... be nice if there was
    you must add the all the controls to a collection, so each class is a member of the collection, you can then have a single procedure to run from whichever control is clicked, passing the control to the procedure

    there are several examples in this forum
    i will see if i can find some

    i posted a very basic example at http://www.vbforums.com/showthread.p...=1#post4927699
    this was for checkboxes, but images would be very similar
    Last edited by westconn1; May 3rd, 2016 at 03:47 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    5

    Re: Can't make image in userform clickable when created dynamically in other userform

    Hi! Thank you all for your time and commitment. The problem is solved for me.
    I might not really understand this, but it seems to be as many of you say. When creating controls dynamically you have to create a "class module" that handels these controls, with the help of WithEvent -function.
    This is how my code looks now.

    Code:
    Private Sub OKButton_Click()
    Dim i As Integer, j As Integer, k As Integer, l As Integer, o As Integer, myPath As String
    
    
    m = RailBox.Value
    n = SlotBox.Value
    
    myPath = Application.ActiveWorkbook.Path
    
    Dim collImgs As Collection
    'Create a variable of our events class
    Dim ImgH As ImageHandler
    'Create a new collection to hold the classes
    Set collImgs = New Collection
    
    o = 1
    i = 1
    For j = 1 To m
        For k = 1 To n
            For l = 1 To 3
                Dim myImage As MSForms.Image
                Set myImage = SWLTool02.Controls.Add("Forms.Image.1", "Image1", True)
                With myImage
                    .Name = "Image" & i & l
                    .Left = 422 + 54 * (k - 1)
                    .Top = 10 + 108 * (j - 1)
                    .Height = 108
                    .Width = 54
                    If l = 1 Then
                        .Picture = LoadPicture(myPath & "\Bilder\" & o & ".bmp")
                        o = o + 1
                        .Visible = True
                    ElseIf l = 2 Then
                        .Picture = LoadPicture(myPath & "\Bilder\G2.bmp")
                        .Visible = False
                    ElseIf l = 3 Then
                        .Picture = LoadPicture(myPath & "\Bilder\G1.bmp")
                        .Visible = False
                    End If
                    .PictureAlignment = 2
                    .PictureSizeMode = 3
                    .Enabled = True
                    .MousePointer = 5
                    'Create a new instance of our events class
                    Set ImgH = New ImageHandler
                    'Set the button we have created as the button in the class
                    Set ImgH.myImage = myImage
                    'Add the class to the collection so it is not lost
                    'when this procedure finishes
                    collImgs.Add ImgH
                    
                End With
            Next l
             i = i + 1
        Next k
    Next j
    
    
    SWLTool02.Width = 442 + 54 * n
    SWLTool02.Height = 285
    If m > 2 Then
        SWLTool02.Height = 260 + 108 * (m - 2)
    End If
    
    Unload Me
    SWLTool02.Show
    
    End Sub
    With the addition of the class module:

    Code:
    Option Explicit
    
    Public WithEvents myImage As MSForms.Image
    
    Private Sub myImage_Click()
    
        If Len(myImage.Name) < 8 Then
            SWLTool02.PosBox.Value = Mid(myImage.Name, 6, 1)
        Else
            SWLTool02.PosBox.Value = Mid(myImage.Name, 6, 2)
        End If
        
    End Sub
    The second code-part that i showed in the original post is scrapped all together.

    Again, thank you all for your time!

    I allso found this post very helpfull.
    http://stackoverflow.com/questions/1...mically-in-vba

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width