none
How to Exit button's click event? RRS feed

  • Question

  • I have button click event and want to exit at some point, how to write the code? 

    I am not talking about Exit Application ( Application.Exit(); ), I want to application to be opened after exiting the button click event.

    In VBA, it is something like Exit Sub. What is the equivalent code in C#?

    Thanks.


    • Moved by CoolDadTx Thursday, April 18, 2019 8:15 PM Winforms related
    • Edited by VA_er Thursday, April 18, 2019 8:30 PM
    Thursday, April 18, 2019 7:37 PM

Answers

  • The equivalent is return in C#.

    void DoWork ()
    {
       if (SomeCondition)
          return;
    
       if (SomeOtherCondition)
          return;
    
       //Do more work
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by VA_er Thursday, April 18, 2019 9:05 PM
    Thursday, April 18, 2019 8:47 PM

All replies

  • You want to keep the application open?  So you do not want to exit?
    Thursday, April 18, 2019 7:51 PM
  • Please post the code you have now and clarify what you mean by "exit". Explain the behavior you want such as "I want to close the current form but leave the main form open" or "when I click the button it closes my application".


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 18, 2019 8:15 PM
  • You want to keep the application open?  So you do not want to exit?

    In VBA, it is equivalent to Exit Sub, somewhere inside the procedure. That is what I want.

    https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/exit-statement

    Thursday, April 18, 2019 8:29 PM
  • Could you please not moving around so many threads in the future? I have zero intention to post this thread under Window Form forum, since I believe the question has nothing to do with Window Form forum.

    Thanks.

    Thursday, April 18, 2019 8:34 PM
  • The equivalent is return in C#.

    void DoWork ()
    {
       if (SomeCondition)
          return;
    
       if (SomeOtherCondition)
          return;
    
       //Do more work
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by VA_er Thursday, April 18, 2019 9:05 PM
    Thursday, April 18, 2019 8:47 PM
  • Ok, Return is equivalent to Sub Exit. Thanks for the information.
    Thursday, April 18, 2019 8:49 PM
  • It is clear now that it isn't but posting questions in the C# forums about clicking buttons, closing forms, Application.Exit, etc are very much Winforms specific. Hence moving it to the correct forum. A Winforms moderator would have to move it back if you are truly just interested in the VB equivalent of 'exit sub'.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 18, 2019 8:49 PM
  • It is not because there is button, then the question must be Window Form related. Indeed, the question is related to a Window Form button, but the question itself is merely about C# syntax. I posted it under C# forum. I have quite a few threads, which had been moved by you from C# to Window Form.

    I do believe users should have more freedom to choose which forum, as least not moving the threads before they have been resolved.




    • Edited by VA_er Thursday, April 18, 2019 8:56 PM
    Thursday, April 18, 2019 8:54 PM