multiple instances of forms, same record

ahbenshaut

Well-known member
Joined
Oct 29, 2004
Messages
62
Location
Alaska
Programming Experience
5-10
Good Day. Kinda banging my head against the wall on this one. I have a Mdi application with the start form(mdiparent) and form1(mdichild). On the start form, there is a textbox and a button. Heres how I want it to work:
1. value is entered into textbox and button is clicked
2. if form1 is open with that value, Bring it to the front
if not, create a new instance of form1

Here's the code I am using. If there is an easier way, please let me know:
VB.NET:
If Not IsNothing(frmForm) Then 'If it is something
            If Not frmForm.IsDisposed Then 'it's not disposed
                'loop through the open form1s and compare the form1s property to the startform textbox
                'if its the same, bring the form1 to the front
                For Each frmForm As Form1 In Me.MdiChildren
                    If frmForm.TestProperty = TextBox1.Text Then
                        frmForm.BringToFront()
                        IsLoaded = True
                    Else
                        IsLoaded = False
                    End If
                Next

                If IsLoaded = False Then
                    frmForm = New Form1()
                    frmForm.MdiParent = Me
                    frmForm.TestProperty = TextBox1.Text
                    frmForm.Show()
                End If
            Else
                frmForm = New Form1()
                frmForm.MdiParent = Me
                frmForm.TestProperty = TextBox1.Text
                frmForm.Show()
            End If
        Else
            frmForm = New Form1()
            frmForm.MdiParent = Me
            frmForm.TestProperty = TextBox1.Text
            frmForm.Show()
        End If
 
You certainly shouldn't have the same chunk of code three times there. Also, you don't call BringToFront on a form.
VB.NET:
Dim f1 As Form1

For Each child As Form In Me.MdiChildren
    If TypeOf child Is Form1 AndAlso DirectCast(child, Form1).SomeCondition Then
        f1 = DirectCast(child, Form1)
        Exit For
    End If
Next

If f1 Is Nothing Then
    f1 = New Form1
    '...
End If

f1.Show()
f1.Activate()
 
Back
Top