Answered Return user to main menu on key press

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
I am currently building a console application in visual studio. If the user is in the middle of adding a Customer, they may decide that they want to go back for whatever reason. If the user is Adding a customer, I want them to be able to press the "Esc" button at any point to go back to the main menu. I've already implemented it in my application where the user can select a number from a menu, but that's only when they reach a certain point within the program. I have done research and the solutions that I've seen online seem way more complicated than it needs to be.
 
Last edited:
What type of project are you using?

Asp.net, Wpf, Winforms, Console What?

What have you tried, researched or considered so far?

You need to provide all the relevant and essential information to us when you open your topic. Don't just assume we can pull out a crystal ball and mystically conclude your thoughts. Remember, we only know as much as you tell us. If you are clear and precise from the start, you will get a clear and direct answer to begin with, which I am sure is what you want. It's also helpful to pose your topic in an appropriate forum relative to your project type.
 
What type of project are you using?

Asp.net, Wpf, Winforms, Console What?

What have you tried, researched or considered so far?

You need to provide all the relevant and essential information to us when you open your topic. Don't just assume we can pull out a crystal ball and mystically conclude your thoughts. Remember, we only know as much as you tell us. If you are clear and precise from the start, you will get a clear and direct answer to begin with, which I am sure is what you want. It's also helpful to pose your topic in an appropriate forum relative to your project type.
Apologies, I was having some issues with my internet. I have altered my post above.
 
Moved to Console Application...
Not meaning to appear bossy, but next time, don't update your opening topic. As someone else may be quoting you on it. And when you edit it, that quote doesn't match what was originally posted and they tend to look a bit nuts. :) So next time, just reply to it with any updates you have made.
Give me a moment to review your updated topic.
 
Moved to Console Application...
Not meaning to appear bossy, but next time, don't update your opening topic. As someone else may be quoting you on it. And when you edit it, that quote doesn't match what was originally posted and they tend to look a bit nuts. :) So next time, just reply to it with any updates you have made.
Give me a moment to review your updated topic.
damn, sorry dude. I will make sure not to do that in the future (y) :)
 
No problem. ;)

The bad way to do it would be to add a while loop to run continuously to check for a keypress as this would consume a lot of resources. The second way is better.
Looks like you may only need to make a slight change if you go the second way, which is to add a background worker. Are you familiar with working with them?
 
No problem. ;)

The bad way to do it would be to add a while loop to run continuously to check for a keypress as this would consume a lot of resources. The second way is better.
Looks like you may only need to make a slight change if you go the second way, which is to add a background worker. Are you familiar with working with them?
I've never actually used them before, but they look very useful. After looking at the link and it says you need to create a windows form application? The program I'm creating is for a college project, and it cant be a windows form application, it can only be a console application :confused:
 
Here is a rough example to get you started, and I suggest you read through on the background worker and how it works. That documentation I have linked above. I suggest making a separate console app and dump this code into it. Then debug it by putting break points on each of the opening methods curly brackets, and seen which one gets hit first, and step through the code so you know how to expect it to operate. If you need a debugging tutorial, there is one in my signature. This code will work for a console application.
C#:
        public static void Main(string[] args)
        {
            BackgroundWorker bgWorker = new BackgroundWorker();
            bgWorker.DoWork += BgWorker_DoWork;
            bgWorker.ProgressChanged += BgWorker_ProgressChanged;
            bgWorker.WorkerReportsProgress = true;
            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.RunWorkerCompleted += BgWorker_RunWorkerCompleted;
            bgWorker.ReportProgress(1); /* Use the overload for userState */
            bgWorker.RunWorkerAsync();
            Console.WriteLine("Press ESC key to stop");
            ConsoleKey key = Console.ReadKey().Key;
            if (key == ConsoleKey.Escape)
            {
                //Add logic to check key is ESC key, and send a cancelation request to your worker.
            }
        }

        private static void BgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            /* Your task  to create your patient was completed */
        }

        private static void BgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            /* Checl your progress here e.ProgressPercentage; */
        }

        private static void BgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            /* Build up your customer here */
            Console.WriteLine("Add a customer name :");
            Console.ReadLine();
        }
 
I am going to mark this as answered as this technically does do as you need with some minor adjustments which you will make yourself based on the inline comments. I'm not going to do all of the work for you, as that defeats the purpose of learning, and I've already given you a head-start and something to play with. However, if you have any more questions, just drop a reply and I or someone else will get back to you. Hope that helps!
 
Back
Top Bottom