I am dynamically adding controls to a form. The set of controls are part of a class. Since I can add mutliple instance of the class to the form, I am using List(of ) to contain them. As the last set of controls from the SearchCriteriaClass are "activated" a new instance is created and its controls are added to the form.
The problem is that I don't know how to add the events of the dynamically generated classes to the form. It it were a single object, it would be easy since I would just use "WithEvents". I am creating the new classes inside of a subroutine where WithEvents is not allowed.
Below are select parts of the code from the form and the class.
The problem is that I don't know how to add the events of the dynamically generated classes to the form. It it were a single object, it would be easy since I would just use "WithEvents". I am creating the new classes inside of a subroutine where WithEvents is not allowed.
Below are select parts of the code from the form and the class.
VB.NET:
Public Class TextSearchForm
Private searchCriteriaList As New List(Of SearchCriteriaClass)
'I can add WithEvents but that does not recognise the events of the SearchCreiterialClass
Private Sub addCriteriaRow()
'Dynamically creates the SearchCriteriaClass and adds its controls to the form
searchCriteriaList.Add(New SearchCriteriaClass)
searchCriteriaList.Last.addToCollection(criteriaFlowLayoutPanel)
End Sub 'addCriteriaRow()
Public Sub CheckLastCriteria(ByRef criteria As SearchCriteriaClass) Handles ???!
'Adds a new search criteria if the last check box is activated and less than 8 already exists
If searchCriteriaList.IndexOf(criteria) = (searchCriteriaList.Count - 1) Then
If searchCriteriaList.Count < 8 Then
addCriteriaRow()
End If 'searchCriteriaList.Count < 8
End If 'searchCriteriaList.IndexOf(criteria) = (searchCriteriaList.Count - 1)
End Sub 'CheckLastCriteria(ByRef criteria As SearchCriteriaClass)
End Class 'TextSearchForm
Public Class SearchCriteriaClass
Public Event NewCriteria(ByRef criteria As SearchCriteriaClass)
Private Sub onOffCheckBox_CheckStateChanged(sender As Object, e As System.EventArgs) Handles onOffCheckBox.CheckStateChanged
radioPanel.Visible = onOffCheckBox.Checked
criteriaTextPanel.Visible = onOffCheckBox.Checked
RaiseEvent NewCriteria(Me)
End Sub
End Class 'SearchCriteriaClass
Last edited: