+ Reply to Thread
Results 1 to 11 of 11

class modules how to add events - need working stub

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    class modules how to add events - need working stub

    what is the difference between:

    Public WithEvents and
    Public Events

    can someone write me a template for creating an event in a class module? i have the following code in the class module, but don't know the next step:

    Please Login or Register  to view this content.
    and the following that raises the event (though i don't know how to call that from the userform):

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: class modules how to add events - need working stub

    I have written a example as your calendar one if too messed up to easily add code and get it working.

    When you are declaring an object that has events, and you want to code against then you use WithEvents

    When you are writing code for an class and you want to expose events which could then be used when the object is declared you use Event

    Class1 code
    Please Login or Register  to view this content.
    userform1, with 4 label controls and a scrollbar
    Please Login or Register  to view this content.
    When the userform is displayed you can click the first 3 labels and the captions will update.

    Label4 will update when the scrollbar changes
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    07-26-2020
    Location
    Israel
    MS-Off Ver
    365
    Posts
    16

    Re: class modules how to add events - need working stub

    So I think this project doesn't require the use of classes

    I recommend this video on youtube

    Excel Macro Mastery - How to Build a Trading Simulator using Excel VBA
    https://youtu.be/vUg-NH-Wqss

    It showcases classes brilliantly


    also visit this site his site
    https://excelmacromastery.com/vba-class-modules/

    I am not associated with the creator of this content, but I learned so much from his videos and site

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: class modules how to add events - need working stub

    @robin8114, it may not require them but it would be really useful to capture the click event of all those labels in a single code place.

  5. #5
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: class modules how to add events - need working stub

    thanks andy, will try and dissect your code, will probably have some questions though

    thanks robin, one of the first sites i visit when coding is excelmacromastery, great articles and helpme's.

  6. #6
    Registered User
    Join Date
    07-26-2020
    Location
    Israel
    MS-Off Ver
    365
    Posts
    16

    Re: class modules how to add events - need working stub

    @Andy Pope, So in this specific instance I would just leave all the userform code in the userform.

    But I do like the collection of labels you made.
    "RaiseEvents" And "WithEvents" is new to me and it looks amazing, but...

    For each label you need to create a new instance of Class1... wont this cause problems down the line with over 50 labels
    and then if I add something like:

    Me.MyLabel.Caption = "123"

    in the MyScroll_Change event, I can only access Label1, All other labels are disconnected from scrollBar

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: class modules how to add events - need working stub

    multiple objects should not be an issue.

    If you want multiple controls to update at the same time when scrollbar changes you can add more code in the mMyObj_UpdateScroll event.

    Not sure what you mean by Me.MyLabel.Caption = "123"
    Do you mean change all the label captions at the same time?

  8. #8
    Registered User
    Join Date
    07-26-2020
    Location
    Israel
    MS-Off Ver
    365
    Posts
    16

    Re: class modules how to add events - need working stub

    Never mind, I'm playing with your code and I just realized that this is how a worksheet works AND MY MIND IS RACING WITH POSSIBILITIES

  9. #9
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: class modules how to add events - need working stub

    andy, got the scrollbar change working now (as per your code), however, i can't get the label_click event to fire, it actually worked once but then quit. can you take a peek at my code and tell me what i need to do to fix?

    as you can see, most of the code from the userform has been moved into the class (which is what i wanted). now just need the label click to fire...
    Attached Files Attached Files

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: class modules how to add events - need working stub

    Your file has too many other issues to be the one to work on.
    But the reason for no click capture was you did not create any objects and store them in the collection.


    This version includes another class to act as a single entry point for an event.

    You still need to create an object for each label, there is a new class with only a reference to the label and the event listner.
    The click event in the CMyLabel class then passes the label reference to the event listner. The Event listner then raises an event which is captured by the object in the userform.

    This allows the userform to know which label was clicked. It then changes the BackStyle so the fill colour is visible.
    Assuming you want the previous label to be unselected there is another object that changes the backstyle of the previously clicked label.
    Attached Files Attached Files

  11. #11
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: class modules how to add events - need working stub

    ok, i see where i did not add to the collection, should be easy to do. will check back in a bit

+ 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. [SOLVED] Class modules and Events
    By malcmail in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 04-22-2020, 05:51 AM
  2. Working with several Collection class modules
    By Geetha Gupta in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-19-2013, 12:34 PM
  3. Re: Events, Class Modules and Addins ... oh my!
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-01-2006, 09:50 AM
  4. [SOLVED] Events, Class Modules and Addins ... oh my!
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2006, 09:23 PM
  5. Class Modules
    By MattShoreson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-22-2006, 06:40 AM
  6. [SOLVED] Basic question - modules and class modules - what's the difference?
    By Mark Stephens in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-08-2005, 07:08 AM
  7. [SOLVED] Class Modules
    By Pavlos in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-19-2005, 02:06 PM

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