Hi,

I'm trying to create multiple instances of a child form. This form has an event that feeds a string variable back to the parent so I'm declaring it with WithEvents which is declared outside of the Sub - meaning multiple clicks will not open multiple forms.

Any idea on another method of doing this while keeping the event?:

Dim WithEvents FRestoreDB As New FormRestoreDB()

    Private Sub BtnOpenDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpenDB.Click
        If Not IsNothing(FRestoreDB) Then
            If Not FRestoreDB.IsDisposed Then
                FRestoreDB.StartPosition = FormStartPosition.Manual
                FRestoreDB.Show()
            Else
                FRestoreDB = New FormRestoreDB()
                FRestoreDB.Show()
            End If
        Else
            FRestoreDB = New FormRestoreDB()
            FRestoreDB.Show()
        End If

    End Sub

You can try something like

Dim WithEvents Frm As FormRestoreDB
	Dim OpenForms As FormRestoreDB()

	Private Sub BtnOpenDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpenDB.Click
		'
		'	Create a new instance
		'
		Frm = New FormRestoreDB
		'
		'	Add to the forms collection (if you need it). 
		'
		If OpenForms Is Nothing Then
			ReDim OpenForms(0)
		Else
			ReDim OpenForms(OpenForms.Length)
		End If
		OpenForms(OpenForms.Length - 1) = Frm
		'
		'	Show the form
		'
		Frm.show()
	End Sub

When you create a new instance, the fired event of each instance will go to the same parent handler.

Hope this helps

Ahhh, I see - Your a star!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.