Close (X) button does not work in MDI Child form!!!

  • Thread starter trac trinh via .NET 247
  • Start date
T

trac trinh via .NET 247

Hi, I'm building an MDI application which has a main parent form(ParentForm) and some ChildForm.

On ChildForm, I also call another form, and I also want to add itas a child of ParentForm, it's OK with code below


' ON ChildForm
Public Sub New (ParentForm as Form)
InitializeComponent()
Me.MdiParent = ParentForm
End Sub

' User click Edit to Edit data on a row in datagrid
' Call another form

Private Sub Edit_Click(....)
' To detect whether frmEdit was created or not
Dim fc As Form
For Each fc In Me.MdiParent.MdiChildren
If fc Is frmEdit Then
If fc.WindowState = FormWindowState.MinimizedThen
fc.WindowState = FormWindowState.Normal
End If
fc.Show()
fc.Focus()
Return
End If
Next

' If frmEdit is nothing, create an instance
If frmEdit Is Nothing Then
frmEdit = New frmDatagridRow
frmEdit.MdiParent = Me.MdiParent
frmEdit.Show()
frmEdit.Focus()
End If
End Sub

Everything is OK till I click on X button to edit another row,when I click Edit again, nothing happen, form frmEdit not show.I debugged and see that when we click X button, frmEdit does notrelease resources used. I don't really know why Close() doesn'twork. Anybody help, please !
 
M

Morten Wennevik

Hi trac trinh,

It would help if you showed some code for the closing of frmDataGridRow,
but I would assume that frmDataGridRow isn't shown again because once
created it appears that frmEdit is always set.

To check if frmEdit is set you would just need to check if frmEdit Is
Nothing (I think, my VB isn't too good, if(frmEdit == null)), and remember
to set it to null/Nothing when you close it.

In any way, if frmEdit is nothing there is no point in checking all
MdiParent.MdiChildren, so you should put that code at the top.

Happy coding!
Morten Wennevik [C# MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top