Results 1 to 6 of 6

Thread: How Do I Close a Console Application?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    How Do I Close a Console Application?

    I have a console application that asks the user to press number 5 to close the application. but i don't know what code to put so when the users press 5 the console application just closes???? any idea

  2. #2
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: How Do I Close a Console Application?

    Put this code in a timer

    vb Code:
    1. If GetAsyncKeyState(Keys.D5) Then
    2.             Me.Close()
    3.         End If

    and this outside a sub:

    vb Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Integer

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    Re: How Do I Close a Console Application?

    in psudocode it says
    while menuSelection != 5
    so when the user press 5 it ends the program how do i write this in VB?

  4. #4
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: How Do I Close a Console Application?

    my post was written in vb ? I don't know what you mean, I'm sorry

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    Re: How Do I Close a Console Application?

    This is the program am trying to run its a console application, I present the user with a menu and I want them when they click 5 it closes the program, what code should I use to allow the user to close the program by pressing 5???

    Code:
    Module Module1
    
        Sub Main()
            Dim menuSelection As Integer
            DisplayMenu(menuSelection)
    
            Select Case menuSelection
                Case 1
                    Mercury()
                Case 2
                    Venus()
                Case 3
                    Earth()
                Case 4
                    Mars()
            End Select
            
        End Sub
    
        Private Sub Mercury()
            Console.WriteLine(" ------------------------------- ")
            Console.WriteLine("Mercury:")
            Console.WriteLine("Average Distance from the Sun: 57.9 million kilometers")
            Console.WriteLine("Mass: 3.31 x 10^23 kg")
            Console.WriteLine("Surface Temperture: -173 to 430 degrees Celsius")
    
    
        End Sub
    
        Private Sub Venus()
            Console.WriteLine(" ------------------------------- ")
            Console.WriteLine("Venus:")
            Console.WriteLine("Average Distance from the Sun: 108.2 million kilometers")
            Console.WriteLine("Mass: 4.87 x 10^24 kg")
            Console.WriteLine("Surface Temperture: 472 degreees Celsius")
        End Sub
    
        Private Sub Earth()
            Console.WriteLine("Earth:")
            Console.WriteLine("Average Distance from the Sun: 149.6 million kilometers")
            Console.WriteLine("Mass: 5.967 x 10^24 kg")
            Console.WriteLine("Surface Temperture: -50 to 50 degrees Celsius")
        End Sub
    
        Private Sub Mars()
            Console.WriteLine("Mars:")
            Console.WriteLine("Average Distance from the Sun: 227.9 million kilometers")
            Console.WriteLine("Mass: 0.6424 x 10^24 kg")
            Console.WriteLine("Surface Temperture: -140 to 20 degrees Celsius")
        End Sub
    
        Private Sub DisplayMenu(ByRef menuSelection As Integer)
            Console.WriteLine(" Select a Planet")
            Console.WriteLine("1. Mercury")
            Console.WriteLine("2. Venus")
            Console.WriteLine("3. Earth")
            Console.WriteLine("4. Mars")
            Console.WriteLine("5. End The Program")
            Console.WriteLine()
            Console.WriteLine("Enter your selection: ")
            menuSelection = CInt(Console.ReadLine())
    
            Do While menuSelection < 1 Or menuSelection > 5
                Console.WriteLine("Invalid Selection.")
                Console.Write("Please Enter 1 2, 3, or 4: ")
                menuSelection = CInt(Console.ReadLine())
            Loop
    
        End Sub
    
    End Module

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

    Re: How Do I Close a Console Application?

    Quote Originally Posted by QuestionPlease View Post
    I have a console application that asks the user to press number 5 to close the application. but i don't know what code to put so when the users press 5 the console application just closes???? any idea
    Just let the program fall to the End Sub line of Sub Main().

    Or if you're feeling particularly brutal towards your own program you can use Application.Exit() which will do a hard-break of the app's thread.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

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

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