Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2006, 04:46 AM
Authorized User
 
Join Date: Nov 2006
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Close a Form when already Opened in C#

Hello Expert
I have already opened a page in C#. but then i don't want to open that page again when that page is already open.
I Used coding like this

static int x;
/////////Using C# Codes
Form_Load()
{
if (x==1)
this.close();
x=1
}

but this code gives error. What I do to solve this problem.

Mr Kumar
[email protected]
__________________
Thanks
Kumar Ashish
[email protected]
 
Old December 4th, 2006, 09:45 PM
Authorized User
 
Join Date: Sep 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mr Kumar,

How about something like the following:

Code:
  public partial class MyDialog : Form
  {
    //------------------------------
    // Static Member Variables
    //------------------------------

    public static int InstanceCount = 0;

    //------------------------------
    // Constructors
    //------------------------------

    public MyDialog()
    {
      InitializeComponent();
      InstanceCount++;
    }

    //------------------------------
    // Overrides
    //------------------------------

    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }

      InstanceCount = Math.Max(InstanceCount - 1, 0);
      System.Diagnostics.Debug.WriteLine(string.Format("Disposing of a MyDialog object.  Instance count = {0}", InstanceCount));

      base.Dispose(disposing);
    }
  }

  //========================================

  public partial class MainForm : Form
  {
    //------------------------------
    // Member Variables
    //------------------------------

    protected MyDialog m_myDialog = null;

    //------------------------------
    // Constructors
    //------------------------------

    public MainForm()
    {
      InitializeComponent();
    }

    //------------------------------
    // Overrides
    //------------------------------

    protected override void OnDoubleClick(EventArgs e)
    {
      base.OnDoubleClick(e);

      System.Diagnostics.Debug.WriteLine("MainForm double-clicked...");

      if (MyDialog.InstanceCount == 0)
      {
        System.Diagnostics.Debug.Write("Instantiating a new instance of MyDialog...");

        try
        {
          m_myDialog = new MyDialog();
          m_myDialog.Show();

          System.Diagnostics.Debug.WriteLine(string.Format("instance #{0} created.", MyDialog.InstanceCount));
        }
        catch
        {
          if (m_myDialog != null)
            m_myDialog.Dispose();

          System.Diagnostics.Debug.WriteLine("failed.");
        }
      }

      else
        System.Diagnostics.Debug.WriteLine("Choosing NOT to instantiate a new instance of MyDialog.");
    }

    //------------------------------

    protected override void OnClosing(CancelEventArgs e)
    {
      base.OnClosing(e);

      if ((!e.Cancel) && (m_myDialog != null))
      {
        m_myDialog.Dispose();
        m_myDialog = null;
      }
    }
  }

Cheers.

- Roger Nedel





Similar Threads
Thread Thread Starter Forum Replies Last Post
Close all MdiChield form from open one form/Button salman .NET Framework 2.0 6 December 10th, 2007 03:21 AM
Close all MdiChield form from open one form salman .NET Framework 1.x 0 November 8th, 2007 12:32 AM
How to know a form is opened by a particular form johnsonlim026 ASP.NET 1.0 and 1.1 Basics 1 July 13th, 2005 12:39 PM
How to close database opened by adodc aa_azadeh Pro VB Databases 1 September 28th, 2004 08:04 AM
Message Appears Only Once When Form Is Opened Ben Horne Access VBA 2 July 5th, 2004 05:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.