Answered How Do I Raise Events From Dynamically Generated List(of class)

Doug

Active member
Joined
Oct 5, 2011
Messages
44
Programming Experience
Beginner
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.

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:
Nevermind. I needed to use addhandler in the subroutine where I created the new object. Everything works perfectly now.

VB.NET:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] addCriteriaRow()[/COLOR]
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]'Dynamically creates the SearchCriteriaClass and adds its controls to the form
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] newSearchCriteria [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]SearchCriteriaClass
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]searchCriteriaList.Add(newSearchCriteria)
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]AddHandler[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] newSearchCriteria.ActiveCriteria, [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] CheckLastCriteria
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]searchCriteriaList[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Last.addToCollection(criteriaFlowLayoutPanel)
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]'addCriteriaRow()[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
 
Last edited:
Back
Top