+ Reply to Thread
Results 1 to 14 of 14

Generic Sub for Image control in Userform

  1. #1
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Generic Sub for Image control in Userform

    My final version will have more than 40 images with all the same code: they will all increment a cooresponding SpinButton with 1
    Please Login or Register  to view this content.
    I would like to limit the need to write that many subs and tried to create a Class Module. This works for my Textboxes & SpinButtons but not for my Images. What did I do wrong?

    Please Login or Register  to view this content.
    I added following code in UserForm1. It works for the textboxes & spinbuttons.
    Please Login or Register  to view this content.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Generic Sub for Image control in Userform

    Are you sure you don't mean aImage_click in your class?

  3. #3
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    Hello Kyle123, I created a class module and changed the name into "clsCommonImage" (see code above). In that module I placed the Private Sub aImage_Change() and added then the code as described in the Private Sub UserForm_Initialize(). This functions perfectly for my TextBoxes ans SpinButtons but doesn't seems to be the right code for Images

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    What is the code meant to do and how isn't it working?

    Also, how are you 'changing' the images?
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    I'm changing the values of the Spinboxes by clicking on the Images. I changed now the Class Module code as shown below (modified Image_Change into Image_Click) and now it works partially.
    I do stiil have following issues:
    1. The start value of all my SpinBoxes (or linked TextBoxes) is now 1 in stead of empty
    2. Clicking on one of the Images increments all of my Spinboxes to 2 (clicking again and everything becomes 3 e.g.)
    3. Clearing the form sets the value of My Spinboxes again to 1 (and not empty as I wanted them to be)
    When I change the pictures into commandbuttons with a picture I get a similar result, so there must still be something wrong
    [CODE]
    Public WithEvents aImage As MSForms.Image

    Private Sub aImage_Click()

    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim ColNum As Integer

    ColNum = Sheet1.Range("Table1").Cells(2, Columns.Count).End(xlToLeft).Column '# columns in Table1
    k = ColNum - 3
    For i = 4 To k
    UserForm1.Controls("SpinButton" & i).Value = UserForm1.Controls("SpinButton" & i).Value + 1
    Next i
    End Sub
    [\CODE]
    Attached Files Attached Files

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Generic Sub for Image control in Userform

    So the images don't actually change?

    If that's the case why not try the Click event in the class module, just like you use here in the code you don't want to repeat.
    Please Login or Register  to view this content.
    By the way, do you really want to increase the value of each spinbutton when you click any of the images?

    Aren't the individual images connected to individual spinbuttons?

  7. #7
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    Indeed, the images do not change. I did try the Sub you suggested but doing this changes all the spinbutton values instead of just the one connected to the spinbutton. Any idea how to solve this?

  8. #8
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    If forgot to anwer your first question: If a click on an image (for example image4), the value of the spinbutton4 and textbox4 should go up with 1. Idem for all the other images/spinbuttons/textboxes.
    Making private sub for all these images works perfectly but I wanted a generic code so if images are added or deleted I do not have to change my codes. It works with textboxes and spinbuttons (tested) but so far not for images or commandbuttons

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Generic Sub for Image control in Userform

    You need to merge your spin button and text box classes with your image class and update the pin button in the class

  10. #10
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    Do you mean that I have to make only 1 class module with 3 private subs named aTextbox_change, aSpinButton_Change and aImage_change?

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Generic Sub for Image control in Userform

    Yes, but you want the image click rather than change wouldn't you?

  12. #12
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    Sure (my mistake). I'll give it a try tomorrow and give you feedback. Thanks a lot for the tips!

  13. #13
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    Unfortunately no change. Clicking on any of the images still increments ALL the TextBoxes and SpinButtons
    I made a new class1 named clsSharedControl and merged as you suggested. Used CommandButtons with images in stead of Images, but this should not make a difference I guess
    [CODE]
    Public WithEvents aTextBox As MSForms.Textbox
    Public WithEvents aSpinButton As MSForms.SpinButton
    Public WithEvents aCommandButton As MSForms.CommandButton


    Private Sub aTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Rem integer entry only

    If Not (Chr(KeyAscii) Like "#") Then
    KeyAscii = 0
    MsgBox "Numbers Only !"
    End If
    End Sub

    Private Sub aTextbox_Change()

    Dim i As Integer ' i = Textbox number indicator, starts at column #6 ( Column F)
    Dim j As Integer
    Dim k As Integer
    Dim ColNum As Integer
    Dim xSum As Double


    ColNum = Sheet1.Range("Table1").Cells(2, Columns.Count).End(xlToLeft).Column '# columns in Table1
    xSum = 0

    k = ColNum - 3

    For i = 4 To k ' Start calculating from Textbox4, stop calculating with last Textbox (in this case PR3)

    xSum = xSum + Val(UserForm1.Controls("TextBox" & i).Object.Value) * Cells(1, i + 2).Value
    UserForm1.Controls("SpinButton" & i).Value = Val(UserForm1.Controls("TextBox" & i).Object.Value)
    Next i

    UserForm1.Label2.Caption = Format(xSum, "0.00 €")


    End Sub

    Private Sub aSpinButton_Change()

    Dim i As Integer ' i = SpinButton number indicator, starts at column #6 ( Sheet1 Column F = the first product)
    Dim j As Integer
    Dim k As Integer
    Dim ColNum As Integer



    ColNum = Sheet1.Range("Table1").Cells(2, Columns.Count).End(xlToLeft).Column '# columns in Table1
    xSum = 0

    k = ColNum - 3

    For i = 4 To k ' Start calculating from SpinButton4, stop calculating with last Textbox (in this case PR3)

    If UserForm1.Controls("SpinButton" & i).Value = 0 Then

    UserForm1.Controls("TextBox" & i).Text = ""

    Else

    UserForm1.Controls("TextBox" & i).Text = UserForm1.Controls("SpinButton" & i).Value

    End If

    Next i



    End Sub


    Private Sub aCommandButton_Click()

    Dim i As Integer ' i = CommandButton number indicator, starts at column #6 ( Sheet1 Column F = the first product)
    Dim j As Integer
    Dim k As Integer
    Dim ColNum As Integer



    ColNum = Sheet1.Range("Table1").Cells(2, Columns.Count).End(xlToLeft).Column '# columns in Table

    k = ColNum - 3

    For i = 4 To k ' Start from CommandButton4, stop calculating with last CommandButton (in this case PR3)

    UserForm1.Controls("SpinButton" & i).Value = UserForm1.Controls("SpinButton" & i).Value + 1


    Next i


    End Sub
    [\CODE]
    And in my Userform I've put following code
    [CODE]
    Option Explicit
    Dim myTextBoxes As Collection
    Dim mySpinButtons As Collection
    Dim myCommandButtons As Collection

    Private Sub UserForm_Initialize()

    Dim aCommonTextBox As clsSharedControl
    Dim aCommonSpinButton As clsSharedControl
    Dim aCommonCommandButton As clsSharedControl
    Dim oneControl As MSForms.Control


    Set myTextBoxes = New Collection

    For Each oneControl In Me.Controls
    If TypeName(oneControl) = "TextBox" Then
    If oneControl.Name <> "TextBox3" Then
    Set aCommonTextBox = New clsSharedControl
    Set aCommonTextBox.aTextBox = oneControl
    myTextBoxes.Add Item:=aCommonTextBox
    End If
    End If
    Next oneControl
    Set aCommonTextBox = Nothing


    Set mySpinButtons = New Collection

    For Each oneControl In Me.Controls
    If TypeName(oneControl) = "SpinButton" Then
    Set aCommonSpinButton = New clsSharedControl
    Set aCommonSpinButton.aSpinButton = oneControl
    mySpinButtons.Add Item:=aCommonSpinButton

    End If
    Next oneControl
    Set aCommonSpinButton = Nothing


    Set myCommandButtons = New Collection

    For Each oneControl In Me.Controls
    If TypeName(oneControl) = "CommandButton" Then
    Set aCommonCommandButton = New clsSharedControl
    Set aCommonCommandButton.aCommandButton = oneControl
    myCommandButtons.Add Item:=aCommonCommandButton

    End If
    Next oneControl
    Set aCommonCommandButton = Nothing


    TextBox1.SetFocus

    End Sub
    [\CODE]

  14. #14
    Registered User
    Join Date
    07-26-2014
    Location
    Brussels
    MS-Off Ver
    MS Office 2010
    Posts
    22

    Re: Generic Sub for Image control in Userform

    Thanks for the tips. It works now

    [SOLVED]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Zoom plot in image control box present in the userform
    By HinaSha in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 05-04-2014, 12:26 AM
  2. [SOLVED] VBA to transfer image FROM userform image control TO a worksheet cell
    By Zoediak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2014, 02:51 PM
  3. [SOLVED] show jpeg picture inside a userform image control box
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 07-29-2013, 03:44 PM
  4. Displaying a random image (or control) on a userform?
    By Jon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2006, 01:25 PM
  5. Userform Image Control and embedded images
    By tim in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-05-2005, 09:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1