2

I have noticed that when I create a Class Module for a Textbox and use that on my forms - by adding via VBA in the form init event - neither the Enter or Exit methods are available. Of course if I just add a textbox to the form they are.

I can get the DblClick method to work fine so my class is setup correctly and my form.init code is also ok.

When I look in the Object browser for MSForms.TextBox I see it doesn't have Enter or Exit methods and presume this is the reason.

There is no urgency for me on this because I noticed it when building my first form that uses my own classes for textbox - Fortunately for what I'm working on now - I don't actually need the Enter or Exit methods, but thought someone might know if there is a work-around because I may need them in the future and for textboxes they are very useful methods

Here is my Class Code named nxTxtV

Option Explicit

Public WithEvents oTxtV As MSForms.TextBox

Private Sub oTxtV_Enter()
   ' This method never fires
   MsgBox "Hello World from the Enter Method"
End Sub

Private Sub oTxtV_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
   ' This method works fine
   MsgBox "Hello World from the DoubleClick Method for " & oTxtV.Value
End Sub

and here's my form initialise code

Private Sub UserForm_Initialize()
   Dim xItm As Control, i As Long
   Dim dItm As nxTxtV   ' nxTxtV is the name of my class

   For i = 1 To 5
      Set xItm = Me.Controls.Add("Forms.TextBox.1", "Column" & i, True)
      With xItm
         .Value = "P" & i
         .AutoSize = False
         .Font.Size = 9
         .Width = 25
         .Height = 250
         .TextAlign = 2 ' Centred
         .SpecialEffect = 0
         .BackColor = RGB(255, 128, 0)
         .WordWrap = True
         .Left = 200 + (i * 27)
         .Top = 5
         .Enabled = True
         .Visible = True
      End With
      Set dItm = New nxTxtV   ' nxTxtV is the name of my class
      Set dItm.oTxtV = xItm
      CodedObjs.Add dItm   ' CodedObjs is declared at module level (form level) as a Collection 
   Next i
End Sub

In short, can I get my class to react to Enter & Exit events ?

Addendum - the CodedObjs Declaration

Public CodedObjs As New Collection
Tin Bum
  • 1,397
  • 1
  • 8
  • 16
  • 2
    Sadly, no. You'll have to do a workaround using other events, such as `KeyUp` or `MouseDown`, or a combination. – FAB Jun 05 '19 at 12:21
  • 1
    @DarXyde Good to Know, at least I can prepare for that at my leisure :-) Thanks – Tin Bum Jun 05 '19 at 12:25
  • 1
    You could catch the event itself (every event) and look which control fired it. see here for Enter: https://stackoverflow.com/questions/51930517/trigger-enter-field-behaviour-through-class-for-a-control/51936950#51936950 – EvR Jun 05 '19 at 12:56
  • 1
    Background as to why you aren't getting these events: They belong to the more general `Control` class, from which the other controls "inherit". – Cindy Meister Jun 05 '19 at 15:05
  • @Cindy Meister Thanks Cindy was hoping someone might clarify that – Tin Bum Jun 05 '19 at 17:55
  • @EvR Thanks for that - It looks promising will take me some time to check it out – Tin Bum Jun 05 '19 at 18:05
  • 1
    @EvR I tried Siddharths answer first but it hogs the processor, then tried your Answer worked too but no processor hogging - Upvoted your answer. Thanks. – Tin Bum Jun 05 '19 at 20:02

0 Answers0