September 23, 2007

Modal/Modeless Dialogs

Modal Dialog
    SomeForm sf = new SomeForm();
    DialogResult res = sf.ShowDialog(this);
To close a form
... // within the form class itself
    DialogResult = DialogResult.OK;
    Close();
...
Set the AcceptButton and CancelButton to the correct buttons if required Modeless Dialog
    // Declare the modeless dialog
    private MagnifyingGlassForm m_MagFrm = null;


    // Creating and showing the modeless dialog
    if (m_MagFrm == null)
    {
      m_MagFrm = new MagnifyingGlassForm();
      m_MagFrm.Owner = this; // IMPORTANT Set the owner of the modeless dialog 
      m_MagFrm.Show(); // Show the modeless dialog
    }

    ...

    // To just hide the modeless dialog use Hide() method
    m_MagFrm.Hide()

    ...

    // Terminating the Modeless Dialog
    if (m_MagFrm != null)
    {
      m_MagFrm.Close();
      m_MagFrm.Dispose();
      m_MagFrm = null;
    }

No comments: