Click here to Skip to main content
15,887,331 members
Home / Discussions / C#
   

C#

 
GeneralRe: have a child while parent is not accesible Pin
Fired.Fish.Gmail13-Apr-09 6:01
Fired.Fish.Gmail13-Apr-09 6:01 
GeneralRe: have a child while parent is not accesible Pin
cppwxwidgetsss13-Apr-09 18:25
cppwxwidgetsss13-Apr-09 18:25 
GeneralRe: have a child while parent is not accesible Pin
Fired.Fish.Gmail14-Apr-09 0:03
Fired.Fish.Gmail14-Apr-09 0:03 
GeneralRe: have a child while parent is not accesible Pin
cppwxwidgetsss14-Apr-09 1:12
cppwxwidgetsss14-Apr-09 1:12 
GeneralRe: have a child while parent is not accesible Pin
Vikram A Punathambekar14-Apr-09 7:27
Vikram A Punathambekar14-Apr-09 7:27 
Questionresolving lag connection to remote server? Pin
[c]amilo12-Apr-09 22:28
[c]amilo12-Apr-09 22:28 
QuestionClosing a form Pin
Deepali Khalkar12-Apr-09 22:27
Deepali Khalkar12-Apr-09 22:27 
AnswerRe: Closing a form Pin
Juan1R12-Apr-09 22:58
Juan1R12-Apr-09 22:58 
The reason why the application exits automatically is that the entry point for it is located in a class usually called Program.cs that has the following code:

static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

The last line (Application.Run(new Form1())), starts a message loop that ends as soon as the main form is closed (new Form1()), what causes the end of the application.

To prevent the application from exiting I've found two possible solutions based on your original idea:

1. The first one is not to close the main form but to hide it:

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
this.Hide();
}

2. The second is to invoke the Application.Run() overload that doesn't take a Form as parameter but an ApplicationContext. This object (that always exist), implements an overridable method called OnMainFormClosed() that is responsible of shutting down the application when the main form is closed. To prevent this from happening, you can derive a new class from it that substitutes the original method by another that does nothing:

//New class.
class ApplicationContextOwn : ApplicationContext {
//Overriden method does nothing
protected override void OnMainFormClosed(object sender, EventArgs e) {

}
}
static class Program {
public static ApplicationContext ac = new ApplicationContext();
///
/// The main entry point for the application.
///

[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

//The application is started using an instance of the new class.
ApplicationContext ac = new ApplicationContextNew();
ac.MainForm = new Form1();
Application.Run(ac);
}
}

Hope this helps.
AnswerRe: Closing a form Pin
#realJSOP13-Apr-09 0:19
mve#realJSOP13-Apr-09 0:19 
Questionhow to read the specific tag [modified] Pin
YiXiang_8912-Apr-09 21:28
YiXiang_8912-Apr-09 21:28 
AnswerRe: how to read the specific tag Pin
Jimmanuel13-Apr-09 0:55
Jimmanuel13-Apr-09 0:55 
QuestionComponent to compare codes Pin
Surya Ayyagari12-Apr-09 21:00
Surya Ayyagari12-Apr-09 21:00 
Questionhow to call a webmethod in a Webservice using windows scheduler in windows application? Pin
ravi.vellanky12-Apr-09 20:55
ravi.vellanky12-Apr-09 20:55 
AnswerRe: how to call a webmethod in a Webservice using windows scheduler in windows application? Pin
Vikram A Punathambekar12-Apr-09 21:59
Vikram A Punathambekar12-Apr-09 21:59 
QuestionHTML Help Executable file Pin
Sajjad Leo12-Apr-09 20:52
Sajjad Leo12-Apr-09 20:52 
AnswerRe: HTML Help Executable file Pin
Fired.Fish.Gmail13-Apr-09 5:09
Fired.Fish.Gmail13-Apr-09 5:09 
GeneralRe: HTML Help Executable file Pin
Sajjad Leo13-Apr-09 20:42
Sajjad Leo13-Apr-09 20:42 
QuestionA question about how to fill data into DataSet Pin
harry~12-Apr-09 20:50
harry~12-Apr-09 20:50 
AnswerRe: A question about how to fill data into DataSet Pin
harry~12-Apr-09 21:11
harry~12-Apr-09 21:11 
QuestionCapturing F5 Keypress Pin
ramzg12-Apr-09 20:41
ramzg12-Apr-09 20:41 
AnswerRe: Capturing F5 Keypress Pin
S a n d y12-Apr-09 20:49
S a n d y12-Apr-09 20:49 
GeneralRe: Capturing F5 Keypress Pin
ramzg12-Apr-09 20:57
ramzg12-Apr-09 20:57 
AnswerRe: Capturing F5 Keypress Pin
12Code12-Apr-09 21:03
12Code12-Apr-09 21:03 
GeneralRe: Capturing F5 Keypress Pin
ramzg12-Apr-09 21:09
ramzg12-Apr-09 21:09 
QuestionConvert C++ to C# Pin
yesu prakash12-Apr-09 20:35
yesu prakash12-Apr-09 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.