Results 1 to 2 of 2

Thread: [1.0/1.1] Checking whether the form is disposed or not!

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Resolved [1.0/1.1] Checking whether the form is disposed or not!

    On a mdi's menu click i am calling a child form.

    On the click event I want to check whether the form is already initiated. if not then i want to initiate it first and then call the show method. on closing the form i dispose it.

    How to chk whether the form is disposed or not?

    IsNothing is not working
    Microsoft Techie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/1.1] Checking whether the form is disposed or not!

    I'm not sure whether you have resolved this or not but the code should look something like this:
    C# Code:
    1. private Form2 childForm;
    2.  
    3. private void ShowChildForm()
    4. {
    5.     if (this.childForm == null || this.childForm.IsDisposed)
    6.     {
    7.         // Create a new child form and show it.
    8.         this.childForm = new Form2();
    9.         this.childForm.MdiParent = this;
    10.         this.childForm.Show();
    11.     }
    12.     else
    13.     {
    14.         // Activate the existing child form.
    15.         this.childForm.Activate();
    16.     }
    17. }
    Note that if there is not supposed to be more than one instance of a form then you can also have that form implement the Singleton pattern:
    C# Code:
    1. public partial class Form2 : Form
    2. {
    3.     private static Form2 _instance;
    4.  
    5.     public static Form2 Instance
    6.     {
    7.         get
    8.         {
    9.             if (_instance == null || _instance.IsDisposed)
    10.             {
    11.                 _instance = new Form2();
    12.             }
    13.  
    14.             return _instance;
    15.         }
    16.     }
    17.  
    18.     private Form2()
    19.     {
    20.         InitializeComponent();
    21.     }
    22. }
    Then your code in the parent form becomes:
    C# Code:
    1. private void ShowChildForm()
    2. {
    3.     Form2.Instance.MdiParent = this;
    4.     Form2.Instance.Show();
    5.     Form2.Instance.Activate();
    6. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width