Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 4th, 2006, 04:46 AM
Authorized User
 
Join Date: Nov 2006
Location: New Delhi, Delhi, India.
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
krashishroa@yahoo.co.in
__________________
Thanks
Kumar Ashish
krashishroa@yahoo.co.in
Reply With Quote
  #2 (permalink)  
Old December 4th, 2006, 09:45 PM
Authorized User
 
Join Date: Sep 2003
Location: Park City, UT, .
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:28 PM.


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