Windows Develop Bookmark and Share   
 index > Windows Forms General > if i close the red color (X) box in C# windows forms..i want to show another form ..How can i show while click Red x box on the top of the form?
 

if i close the red color (X) box in C# windows forms..i want to show another form ..How can i show while click Red x box on the top of the form?

Hi reply me asap
kumarvelayutham  Saturday, September 19, 2009 11:48 AM

You probably have a main method in your Program.cs file.

I think you want this:

// Show form 1
Form1 form1 = new Form1();
form1.ShowDialog();

// Now the code waits until the user closes the form (your "close the red x").

// Now the user has closed the first form, show the 2nd
Form2 form2 = new Form2();
form2.ShowDialog();

Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com

Looking for a way to deploy your updates to all your clients? Try Updater!
Geert van Horrik  Saturday, September 19, 2009 11:53 AM

If you really want to open form 1 from form 2 (which I don't recommend), use the FormClosed event. However, just add the code I gave you to the main method (located in Program.cs).


Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com

Looking for a way to deploy your updates to all your clients? Try Updater!
Geert van Horrik  Saturday, September 19, 2009 11:59 AM
This works, but is highly inadvisable.

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

Application.Run(new Form2());

}

}


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Saturday, September 19, 2009 1:58 PM
Project + Add Reference, select Microsoft.VisualBasic. Make your Program.cs source code file look like this:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace WindowsFormsApplication1 {
class Program : WindowsFormsApplicationBase {
public Program() {
this.EnableVisualStyles = true;
this.ShutdownStyle = ShutdownMode.AfterAllFormsClose;
this.MainForm = new Form1();
}
[STAThread]
static void Main(string[] args) {
new Program().Run(args);
}
}
}

The ShutdownStyle property ensures that your program will only exit when there are no active forms left. You could display the 2nd form in the OnFormClosing() method of Form1 for example:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
new Form2().Show();
}
}


Hans Passant.
nobugz  Saturday, September 19, 2009 3:35 PM

You probably have a main method in your Program.cs file.

I think you want this:

// Show form 1
Form1 form1 = new Form1();
form1.ShowDialog();

// Now the code waits until the user closes the form (your "close the red x").

// Now the user has closed the first form, show the 2nd
Form2 form2 = new Form2();
form2.ShowDialog();

Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com

Looking for a way to deploy your updates to all your clients? Try Updater!
Geert van Horrik  Saturday, September 19, 2009 11:53 AM
hi,
i know that one ....but what teh problem is where can i write the code ?
thanks...

can u say where can i write the code ?means in which event can i write?
kumarvelayutham  Saturday, September 19, 2009 11:56 AM

If you really want to open form 1 from form 2 (which I don't recommend), use the FormClosed event. However, just add the code I gave you to the main method (located in Program.cs).


Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com

Looking for a way to deploy your updates to all your clients? Try Updater!
Geert van Horrik  Saturday, September 19, 2009 11:59 AM
This works, but is highly inadvisable.

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

Application.Run(new Form2());

}

}


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Saturday, September 19, 2009 1:58 PM
Project + Add Reference, select Microsoft.VisualBasic. Make your Program.cs source code file look like this:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace WindowsFormsApplication1 {
class Program : WindowsFormsApplicationBase {
public Program() {
this.EnableVisualStyles = true;
this.ShutdownStyle = ShutdownMode.AfterAllFormsClose;
this.MainForm = new Form1();
}
[STAThread]
static void Main(string[] args) {
new Program().Run(args);
}
}
}

The ShutdownStyle property ensures that your program will only exit when there are no active forms left. You could display the 2nd form in the OnFormClosing() method of Form1 for example:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
new Form2().Show();
}
}


Hans Passant.
nobugz  Saturday, September 19, 2009 3:35 PM
ITS WORKS FINE..I WRITE IN THE FORM CLOSED EVENT OF THE SECOND FORM....


{
firstform 0bj=new firstform();

obj.show();




}
kumarvelayutham  Wednesday, September 23, 2009 10:26 AM
What?
Hans Passant.
nobugz  Wednesday, September 23, 2009 10:57 AM

You can use google to search for other answers

Custom Search

More Threads

• C++ HWND -> .Net Control
• Using the PrintPreviewDialog/Printing with GDI+ Scale Transformations - doesn't work?
• Event Calling explicitly
• DetailsView and Visible Property problem
• Grabbing data from a control on a window
• Controlling mdiforms open
• how do disable the buttons in datetime picker
• ShowFocusQues Problem
• Autocomplete on ComboBox in VS 2003?
• How do you make two forms appear to be one form?