Detecting when user presses the red x










0














Super new to C#, but ran into a little roadblock on my most recent project. I have a couple window forms that the user is able to click through. In the main program I have a while loop that will keep looping through the forms and will initiate the form depending on which button the user pressed in the previous form (variable is called traverse). Right now I put in an exit button on each form that will break the while loop. What I'm having trouble with is when the user presses the red X at the top right corner, the form will not close (will just keep reloading). I put a ** next to the line of the code that I thought might help with the issue (but obviously did not).



 while (Program.exit != "exit")

**if (Application.Exit = true)

break
else

if (Program.traverse == "form4")

Application.Run(new Form4());

if (Program.traverse == "form1")

Application.Run(new Form1());

if (Program.traverse == "form5")

Application.Run(new Form5());



}


Is there a way C# can detect when the user presses the red X to break out of the loop?










share|improve this question



















  • 1




    There is so much wrong with your code I wouldn't even know where to begin. You don't run forms, you show them. You only run your application, normally through a main window form (if its as windows forms application), that keeps the app alive as long as its open. There is no need of a loop whatsoever.
    – InBetween
    Nov 11 at 13:20







  • 2




    You say you are new to c#. I recommend you get a good book or tutorial on c# with a chapter in windows forms towards the end and start from chapter one. Don't run before you even know how to crawl.
    – InBetween
    Nov 11 at 13:22
















0














Super new to C#, but ran into a little roadblock on my most recent project. I have a couple window forms that the user is able to click through. In the main program I have a while loop that will keep looping through the forms and will initiate the form depending on which button the user pressed in the previous form (variable is called traverse). Right now I put in an exit button on each form that will break the while loop. What I'm having trouble with is when the user presses the red X at the top right corner, the form will not close (will just keep reloading). I put a ** next to the line of the code that I thought might help with the issue (but obviously did not).



 while (Program.exit != "exit")

**if (Application.Exit = true)

break
else

if (Program.traverse == "form4")

Application.Run(new Form4());

if (Program.traverse == "form1")

Application.Run(new Form1());

if (Program.traverse == "form5")

Application.Run(new Form5());



}


Is there a way C# can detect when the user presses the red X to break out of the loop?










share|improve this question



















  • 1




    There is so much wrong with your code I wouldn't even know where to begin. You don't run forms, you show them. You only run your application, normally through a main window form (if its as windows forms application), that keeps the app alive as long as its open. There is no need of a loop whatsoever.
    – InBetween
    Nov 11 at 13:20







  • 2




    You say you are new to c#. I recommend you get a good book or tutorial on c# with a chapter in windows forms towards the end and start from chapter one. Don't run before you even know how to crawl.
    – InBetween
    Nov 11 at 13:22














0












0








0







Super new to C#, but ran into a little roadblock on my most recent project. I have a couple window forms that the user is able to click through. In the main program I have a while loop that will keep looping through the forms and will initiate the form depending on which button the user pressed in the previous form (variable is called traverse). Right now I put in an exit button on each form that will break the while loop. What I'm having trouble with is when the user presses the red X at the top right corner, the form will not close (will just keep reloading). I put a ** next to the line of the code that I thought might help with the issue (but obviously did not).



 while (Program.exit != "exit")

**if (Application.Exit = true)

break
else

if (Program.traverse == "form4")

Application.Run(new Form4());

if (Program.traverse == "form1")

Application.Run(new Form1());

if (Program.traverse == "form5")

Application.Run(new Form5());



}


Is there a way C# can detect when the user presses the red X to break out of the loop?










share|improve this question















Super new to C#, but ran into a little roadblock on my most recent project. I have a couple window forms that the user is able to click through. In the main program I have a while loop that will keep looping through the forms and will initiate the form depending on which button the user pressed in the previous form (variable is called traverse). Right now I put in an exit button on each form that will break the while loop. What I'm having trouble with is when the user presses the red X at the top right corner, the form will not close (will just keep reloading). I put a ** next to the line of the code that I thought might help with the issue (but obviously did not).



 while (Program.exit != "exit")

**if (Application.Exit = true)

break
else

if (Program.traverse == "form4")

Application.Run(new Form4());

if (Program.traverse == "form1")

Application.Run(new Form1());

if (Program.traverse == "form5")

Application.Run(new Form5());



}


Is there a way C# can detect when the user presses the red X to break out of the loop?







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 14:16









John

11.2k31736




11.2k31736










asked Nov 11 at 13:09









Djaenike

506




506







  • 1




    There is so much wrong with your code I wouldn't even know where to begin. You don't run forms, you show them. You only run your application, normally through a main window form (if its as windows forms application), that keeps the app alive as long as its open. There is no need of a loop whatsoever.
    – InBetween
    Nov 11 at 13:20







  • 2




    You say you are new to c#. I recommend you get a good book or tutorial on c# with a chapter in windows forms towards the end and start from chapter one. Don't run before you even know how to crawl.
    – InBetween
    Nov 11 at 13:22













  • 1




    There is so much wrong with your code I wouldn't even know where to begin. You don't run forms, you show them. You only run your application, normally through a main window form (if its as windows forms application), that keeps the app alive as long as its open. There is no need of a loop whatsoever.
    – InBetween
    Nov 11 at 13:20







  • 2




    You say you are new to c#. I recommend you get a good book or tutorial on c# with a chapter in windows forms towards the end and start from chapter one. Don't run before you even know how to crawl.
    – InBetween
    Nov 11 at 13:22








1




1




There is so much wrong with your code I wouldn't even know where to begin. You don't run forms, you show them. You only run your application, normally through a main window form (if its as windows forms application), that keeps the app alive as long as its open. There is no need of a loop whatsoever.
– InBetween
Nov 11 at 13:20





There is so much wrong with your code I wouldn't even know where to begin. You don't run forms, you show them. You only run your application, normally through a main window form (if its as windows forms application), that keeps the app alive as long as its open. There is no need of a loop whatsoever.
– InBetween
Nov 11 at 13:20





2




2




You say you are new to c#. I recommend you get a good book or tutorial on c# with a chapter in windows forms towards the end and start from chapter one. Don't run before you even know how to crawl.
– InBetween
Nov 11 at 13:22





You say you are new to c#. I recommend you get a good book or tutorial on c# with a chapter in windows forms towards the end and start from chapter one. Don't run before you even know how to crawl.
– InBetween
Nov 11 at 13:22













1 Answer
1






active

oldest

votes


















2














First: your approuch is totaly wrong.



Second: befor starting i am totally recommend to you read some books about Windows Forms technology or, what better about Windows Presentation Foundation (WPF) technology. After this you will no ask so wired questions and will improve you development speed and qualty dramaticly.



Third: direct answer to you question:



in you Form code behind you can do next:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
Closed += OnClosed;


private void OnClosed(object sender, EventArgs e)

// DO, what you need when windows closed








share|improve this answer




















  • So typically you run all the subsequent windows forms from your main one?
    – Djaenike
    Nov 11 at 13:30










  • basicly - yes. Each application, usually, but not always, has only one main form/window, which create another by demand. ut again, it's depends from you architecure. And function Application.Run should be called only once in 99.99% cases (if you not create really difficult and complexivity app like Visual Studio).
    – Sergey Shulik
    Nov 11 at 13:34











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249056%2fdetecting-when-user-presses-the-red-x%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














First: your approuch is totaly wrong.



Second: befor starting i am totally recommend to you read some books about Windows Forms technology or, what better about Windows Presentation Foundation (WPF) technology. After this you will no ask so wired questions and will improve you development speed and qualty dramaticly.



Third: direct answer to you question:



in you Form code behind you can do next:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
Closed += OnClosed;


private void OnClosed(object sender, EventArgs e)

// DO, what you need when windows closed








share|improve this answer




















  • So typically you run all the subsequent windows forms from your main one?
    – Djaenike
    Nov 11 at 13:30










  • basicly - yes. Each application, usually, but not always, has only one main form/window, which create another by demand. ut again, it's depends from you architecure. And function Application.Run should be called only once in 99.99% cases (if you not create really difficult and complexivity app like Visual Studio).
    – Sergey Shulik
    Nov 11 at 13:34
















2














First: your approuch is totaly wrong.



Second: befor starting i am totally recommend to you read some books about Windows Forms technology or, what better about Windows Presentation Foundation (WPF) technology. After this you will no ask so wired questions and will improve you development speed and qualty dramaticly.



Third: direct answer to you question:



in you Form code behind you can do next:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
Closed += OnClosed;


private void OnClosed(object sender, EventArgs e)

// DO, what you need when windows closed








share|improve this answer




















  • So typically you run all the subsequent windows forms from your main one?
    – Djaenike
    Nov 11 at 13:30










  • basicly - yes. Each application, usually, but not always, has only one main form/window, which create another by demand. ut again, it's depends from you architecure. And function Application.Run should be called only once in 99.99% cases (if you not create really difficult and complexivity app like Visual Studio).
    – Sergey Shulik
    Nov 11 at 13:34














2












2








2






First: your approuch is totaly wrong.



Second: befor starting i am totally recommend to you read some books about Windows Forms technology or, what better about Windows Presentation Foundation (WPF) technology. After this you will no ask so wired questions and will improve you development speed and qualty dramaticly.



Third: direct answer to you question:



in you Form code behind you can do next:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
Closed += OnClosed;


private void OnClosed(object sender, EventArgs e)

// DO, what you need when windows closed








share|improve this answer












First: your approuch is totaly wrong.



Second: befor starting i am totally recommend to you read some books about Windows Forms technology or, what better about Windows Presentation Foundation (WPF) technology. After this you will no ask so wired questions and will improve you development speed and qualty dramaticly.



Third: direct answer to you question:



in you Form code behind you can do next:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
Closed += OnClosed;


private void OnClosed(object sender, EventArgs e)

// DO, what you need when windows closed









share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 13:24









Sergey Shulik

678824




678824











  • So typically you run all the subsequent windows forms from your main one?
    – Djaenike
    Nov 11 at 13:30










  • basicly - yes. Each application, usually, but not always, has only one main form/window, which create another by demand. ut again, it's depends from you architecure. And function Application.Run should be called only once in 99.99% cases (if you not create really difficult and complexivity app like Visual Studio).
    – Sergey Shulik
    Nov 11 at 13:34

















  • So typically you run all the subsequent windows forms from your main one?
    – Djaenike
    Nov 11 at 13:30










  • basicly - yes. Each application, usually, but not always, has only one main form/window, which create another by demand. ut again, it's depends from you architecure. And function Application.Run should be called only once in 99.99% cases (if you not create really difficult and complexivity app like Visual Studio).
    – Sergey Shulik
    Nov 11 at 13:34
















So typically you run all the subsequent windows forms from your main one?
– Djaenike
Nov 11 at 13:30




So typically you run all the subsequent windows forms from your main one?
– Djaenike
Nov 11 at 13:30












basicly - yes. Each application, usually, but not always, has only one main form/window, which create another by demand. ut again, it's depends from you architecure. And function Application.Run should be called only once in 99.99% cases (if you not create really difficult and complexivity app like Visual Studio).
– Sergey Shulik
Nov 11 at 13:34





basicly - yes. Each application, usually, but not always, has only one main form/window, which create another by demand. ut again, it's depends from you architecure. And function Application.Run should be called only once in 99.99% cases (if you not create really difficult and complexivity app like Visual Studio).
– Sergey Shulik
Nov 11 at 13:34


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249056%2fdetecting-when-user-presses-the-red-x%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20