3 Replies Latest reply on Jun 9, 2015 9:00 AM by Deepak Gupta

    WithEvents

    Willie Roelofs

      Hi,

       

      I'm trying to run the following using the VBA that solidworks provides.

       

      Private WithEvents m_oTextBox as TextBox
      Public Property Set TextBox(ByVal oTextBox as TextBox)
         Set m_oTextBox = oTextBox
      End Property
      Private Sub m_oTextBox_Change()
         ' Do something
      End Sub
      Private m_oCollectionOfEventHandlers As Collection
      Private Sub UserForm_Initialise()
          Set m_oCollectionOfEventHandlers = New Collection
          Dim oControl As Control
          For Each oControl In Me.Controls
              If TypeName(oControl) = "TextBox" Then
                  Dim oEventHandler As TextBoxEventHandler
                  Set oEventHandler = New TextBoxEventHandler
                  Set oEventHandler.TextBox = oControl
                   m_oCollectionOfEventHandlers.Add oEventHandler
              End If
          Next oControl
      End Sub
      
        • Re: WithEvents
          Abdullah Erdemir

          Willie Roelofs ,

           

          You must create a class named "TextBoxEventHandler"

          Right click in the project management window -> Insert -> Class Module

          Rename the class module to TextBoxEventHandler

          Copy this text into TextBoxEventHandler class:

          Private WithEvents m_oTextBox As TextBox
          
          Public Property Set TextBox(ByVal oTextBox As TextBox)
              Set m_oTextBox = oTextBox
          End Property
          
          
          Private Sub m_oTextBox_Change()
              ' Do something
              MsgBox "test"
          End Sub
          

          In Userform_Initialize event copy this:

          Private m_oCollectionOfEventHandlers As Collection
          
          Private Sub UserForm_Initialize()
              Set m_oCollectionOfEventHandlers = New Collection
              Dim oControl As Control
              For Each oControl In Me.Controls
                  If TypeName(oControl) = "TextBox" Then
                      Dim oEventHandler As TextBoxEventHandler
                      Set oEventHandler = New TextBoxEventHandler
                      Set oEventHandler.TextBox = oControl
                       m_oCollectionOfEventHandlers.Add oEventHandler
                  End If
              Next oControl
          End Sub
          

           

          Insert some textboxes and run the macro. When you try to change any textbox, a message "test" will appear.

           

          textboxeventhandler_example.PNG

            • Re: WithEvents
              Willie Roelofs

              Thank you so much! Now I don't have to write 934 seperate individual event handlers!

               

              I do have one question though, I expected the code to trigger at the moment someone is done changing the text. Instead the code triggers at the very moment the first character is entered in the textbox.

              Is

              it possible to delay the trigger untill someone is done changing entering characters?