2

Untill now I have created one event for each control in my userform.

Private Sub TextBox_Integrate_Indexes_Change()
    Call LabView.textBoxChange(TextBox_Integrate_Indexes)
End Sub

Private Sub TextBox_Integrate_InputFile_Change()
    Call LabView.textBoxChange(Me.TextBox_Integrate_InputFile)
End Sub

Private Sub TextBox_Integrate_OutputFile_Change()
    Call LabView.textBoxChange(Me.TextBox_Integrate_OutputFile)
End Sub

As seen these events all just send its object to my method which then handles the event(check if it has changed its value, and if so store the updated value in a config.json file)

However instead of making an event for all my userform textboxes, optionbuttons listboxs,checkboxes and comboxes, I wasa wandering if there is a way to detect if any event happens to that userform, get the item that triggered the event and if this one of the above type, then send itself to my method.

braX
  • 11,506
  • 5
  • 20
  • 33
skatun
  • 856
  • 4
  • 17
  • 36
  • 2
    [Does this help](https://stackoverflow.com/questions/1083603/vba-using-withevents-on-userforms) – chris neilsen Aug 24 '18 at 10:03
  • Ah, interesting @Pᴇʜ. But I think the discussion chris links to is more on-target since the controls appear to not be dynamic. Also, the explanation is better IMO. – Cindy Meister Aug 24 '18 at 10:24
  • You could also capture the Change-Event itself (without withevents) for every control on your Form. (without making an event for all separate controls) – EvR Aug 24 '18 at 12:42

1 Answers1

2

Yes, you can create a custom class that holds a private textbox variable. You can then capture the event in that class and pass it to your Labview class.

In the UserForm you can just create a collection of the custom class, and set the userform's textboxes as the private variables in the custom class.

Example code: cTextBox Class:

Private WithEvents p_TextBox As MSForms.TextBox
Public Property Let txtBox(value As MSForms.TextBox)
    Set p_TextBox = value
End Property
Public Property Get txtBox() As MSForms.TextBox
    Set txtBox = p_TextBox
End Property
Private Sub p_TextBox_Change()
    Call Labview.textboxchange(p_TextBox)
End Sub

Labview class:

Public Sub textboxchange(val As MSForms.TextBox)
    MsgBox val.Name
End Sub

Userform code:

Private t As MSForms.Control
Private ctb As cTextBox
Private cTextBoxes As Collection

Private Sub UserForm_Initialize()
    Set cTextBoxes = New Collection
    For Each t In Me.Controls
        If TypeName(t) = "TextBox" Then
            Set ctb = New cTextBox
            ctb.txtBox = t
            cTextBoxes.Add ctb
        End If
    Next t
End Sub

And a routine to test the whole thing:

Public Labview As New Labview
Sub test()
    UserForm1.Show
End Sub
Rik Sportel
  • 2,661
  • 1
  • 14
  • 24
  • Ah obviously there's no error handling etc. in the above example and you should have the Textboxes added to the UserForm1 on forehand. – Rik Sportel Aug 24 '18 at 11:13