Dipose() Doubts

  • Thread starter Thread starter Leandro Martins Berti
  • Start date Start date
L

Leandro Martins Berti

Hi...

I have a main MDI form where I load (in a menu click method) my MDI child
forms, like in example below:

private void tStpPrincipalUsers_Click(object sender, EventArgs e)
{
FormUsers frmUsers = new FormUsers();
frmUsers.MdiParent = this;
frmUsers.WindowState = FormWindowState.Maximized;
frmUsers.Show();
}

In the frmUsers I have this method in a tool bar button:

private void tStpBtnExit_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}

Is right call the dispose method here? If is not right where do I shall the
dispose method?

Other doubt is: I have a form loaded like a Dlg (called by the ShowDialog
method):

private void tStpBtnEdit_Click(object sender, EventArgs e)
{
DlgUsers userDlg = new DlgUsers();
userDlg.userID =
Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
userDlg.LoadUser();
userDlg.ShowDialog();
userDlg.Dispose();
}

Is right call the dispose method here?
 
With regard to your ShowDialog, wrap it all in a using statement and
the form will be automatically disposed for you

using(DlgUsers userDlg = new DlgUsers())
{
userDlg.userID =
Convert.ToInt32(dataGridView1.CurrentRow.Cells["ID"].Value);
userDlg.LoadUser();
userDlg.ShowDialog();
}
 
As Jason, wrap the ShowDialog form in a using and it will be disposed.
For a form shown using .Show, it is automatically disposed when it is
closed.
 
Chris, if the form is a MDI child form is it automatically disposed when it
is closed too?
 
I cannot find any information that an MDI form behaves differently than
any other form. AFAIK, when a form is closed that was shown with just
the Show method, it is disposed.
 
Back
Top