Close duplicate MDI child form

flynn

Regular
Joined
Jul 28, 2005
Messages
59
I haven't had any luck finding a solution to this problem:

Given an MDI app, the user opens a new document window. In the ID text field, they type an ID (say "123"). They open another child document and type the ID "123" again. What I need to do is close the most recent child document and give focus back to the document that already had "123" as the ID.

The code I am trying to use:

Code:
    Private Sub txtFolioNumber_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFolioNumber.LostFocus
        Dim frm As frmDocument

        For Each frm In Me.MdiParent.MdiChildren
            'no need to check myself (the current form)
            If (Me.Handle.ToInt32 <> frm.Handle.ToInt32) Then
                'if the number just entered is already used on another form, switch to that form
                If (frm.txtFolioNumber.Text = txtFolioNumber.Text) Then
                    frm.TopMost() = True
                    Me.Close()
                End If
            End If
        Next frm

    End Sub

I also posted this problem at VisualBasicForum.com (in the .Net section) but haven't found a solution yet.

http://www.visualbasicforum.com/showthread.php?t=232374

tia,
flynn
 
I would suggest a different approach. Instead of just closing the form without warning, which will probably confuse the user, do something like:

Code:
Private Sub txtFolioNumber_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFolioNumber.LostFocus
        Dim frm As frmDocument

        For Each frm In Me.MdiParent.MdiChildren
            'no need to check myself (the current form)
            If (Me.Handle.ToInt32 <> frm.Handle.ToInt32) Then
                'if the number just entered is already used on another form, switch to that form
                If (frm.txtFolioNumber.Text = txtFolioNumber.Text) Then
                    MessageBox.Show("This number already exists. Please enter a different number."
                    Exit For
                End If
            End If
        Next frm

    End Sub

Another approach would be to use just one form for entering the data and prompting the user if they enter a number that already exists:

If MessageBox.Show("Record already exists. Would you like to go to that record?", "Duplicate Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))

Using one form is likely to be less confusing in the long run.
 
Back
Top