问题
I've been struggling to get this to work...I have a button on a MDIchild form that opens another MDIchild form, but if the form is already open, it does not recognize it and opens a new one instead of bringing it to front. This is the code i've got:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MDIForm4 As New Form4
MDIForm4.MdiParent = Me
MDIForm4.Show()
End Sub
This works for the button to open the new form, and then I tried adding this:
If Not Form4 Is Nothing Then
Form4.BringToFront()
End If
But with no positive outcome. Does someone have any ideas?
Regards,
Jorge Brito
回答1:
Here is how I typically do that:
For Each f As Form In Application.OpenForms
If TypeOf f Is frmTest Then
f.Activate()
Exit Sub
End If
Next
Dim myChild As New frmTest
myChild.MdiParent = Me
myChild.Show()
Notice this uses Application.OpenForms, you can use your Me.MdiChildren (assuming Me = this MDI form) if you want just the children of your main form.
回答2:
Fixed now!
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each f As Form In Application.OpenForms
If TypeOf f Is Form4 Then
f.Activate()
Exit Sub
End If
Next
Dim MDIForm As New Form4
MDIForm.MdiParent = Form2
MDIForm.Show()
End Sub
I was defining the MDI parent on the wrong form!
来源:https://stackoverflow.com/questions/25248253/bring-mdichild-form-to-front-if-already-open