Results 1 to 6 of 6

Thread: Simple button?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Simple button?

    I am a newbie to coding. But I have learned so much already jsut by reading post and other documents on line. I have not gotten a book yet for VB 2005

    I am looking for the code to create a cancel button to close the window and cancel any process that is running. Thanks

  2. #2
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: Simple button?

    You can use Me.Close to close a form, or End to end the whole program. In terms of cancelling processes, if you mean within your program, ending the program may do what you want. If you have code that takes a long time to execute, then you could declare a global boolean variable, and if it is true keep going in the loop/code, otherwise exit the program. You would change it to false only when you want the program to exit (this is not the best way - search google and msdn for other solutions, see jmcilhinney's signature for many other resources, and finally, I suggest this site).
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

  3. #3
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: Simple button?

    It depends what you mean by "cancel any process that is running". If you want a cancel button to close a form then place:

    VB Code:
    1. Me.Close

    ...in the buttons Click event. You can also designate the button as the forms Cancel button using the form's CancelButton property. Just select the button in the drop-down list that appears.
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  4. #4
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: Simple button?

    If you want to exit an application it is better practice to use:

    VB Code:
    1. Application.Exit()

    ... rather than End.
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Simple button?

    the best way is to use Me.Close to close the main form, or to code it so that Sub Main hits the "End Sub" line

    using "End" is the worst way to end your app and Application.Exit does terminate the app, but it doesnt allow for any cleanup code to run
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple button?

    Quote Originally Posted by JuggaloBrotha
    Application.Exit does terminate the app, but it doesnt allow for any cleanup code to run
    Actually, that's not the case in .NET 2.0. The FormClosing and FormClosed events were specifically added for that reason. If you call Application.Exit then the Closing and Closed events are not raised. As they were all you had in .NET 1.x you were scuppered. The FormClosing and FormClosed events ARE raised on open forms when Application.Exit is called, which is how it should be. The old events have been retianed and the new ones added for backward-compatibility, rather than simply changing the behaviour of the existing events and possibly breaking existing code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width