I have my own class associated with a control of the MSForms.TextBox type, defined using the WithEvents method. I can handle all TextBox events except AfterUpdate, BeforeUpdate, Enter and Exit. Here's what I do.
- I have a simple user form with two TextBox controls and one CommandButton button
Option ExplicitPrivate thisTextBox1 As Class1Private thisTextBox2 As Class1Private Sub CommandButton1_Click() Set thisTextBox1 = New Class1 Set thisTextBox2 = New Class1 thisTextBox1.Init TextBox1 thisTextBox2.Init TextBox2End SubPrivate Sub UserForm_Terminate() Set thisTextBox1 = Nothing Set thisTextBox2 = NothingEnd Sub
- I have my own Class1 class which should internally handle the events of the TextBox control assigned to it
'@Folder("VBAProject")Option ExplicitPrivate WithEvents thisTextBox As MSForms.TextBoxPrivate thisTextBoxControl As MSForms.ControlPublic Sub Init(ByVal MyTextBox As MSForms.TextBox) Set thisTextBox = MyTextBox Set thisTextBoxControl = MyTextBox.Parent.Controls.Item(MyTextBox.Name)End SubPrivate Sub thisTextBox_Change() Debug.Print "thisTextBox_Change " & thisTextBox.Name & " " & thisTextBox.TextEnd Sub
If the code is written as above, everything works fine, but my class cannot handle the AfterUpdate, BeforeUpdate, Enter and Exit evetnts
- So if I change the declaration of the internal variable thisTextBoxControl using the WithEvents method, the code will compile correctly. However, when I try to assign an object to thisTextBoxControl I end up with an error Object or class does not support the set of the events.
Private WithEvents thisTextBox As MSForms.TextBoxPrivate WithEvents thisTextBoxControl As MSForms.ControlPublic Sub Init(ByVal MyTextBox As MSForms.TextBox) Set thisTextBox = MyTextBox Set thisTextBoxControl = MyTextBox.Parent.Controls.Item(MyTextBox.Name)End SubPrivate Sub thisTextBox_Change() Debug.Print "thisTextBox_Change " & thisTextBox.Name & " " & thisTextBox.TextEnd SubPrivate Sub thisTextBoxControl_Enter() Debug.Print "thisTextBoxControl_Enter " & thisTextBoxControl.NameEnd Sub
Does anyone have an idea how to bypass this and how to handle the AfterUpdate, BeforeUpdate, Enter and Exit events inside the class.