-1

If the user is in the middle of adding a Customer, they may decide that they want to go back for whatever reason. When the user is entering new details, 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. can anyone help?

S92
  • 7
  • 4
  • You need to provide much more code than this, ideally the entire function/routine responsible for your menu and customer creation. – h0r53 Dec 09 '20 at 19:47
  • Sorry dude, is that better? they are both individual methods. so when im adding a patient, at any point i want to be able to press "Esc" to head back to the menu – S92 Dec 09 '20 at 19:52
  • 1
    So unfortunately `Console.ReadLine` won't catch the escape key, because it's basically buffering input until it encounters a newline character `\n`. You could do something like `ck = Console.ReadKey();` and then check `if (ck.Key == ConsoleKey.Escape) return`. The only problem is you'll need to build the strings yourself though and ultimately write your own "ReadLineOrEscape" routine. – h0r53 Dec 09 '20 at 20:00
  • ahh damn. okay thanks, I appreciate the comment. I'm thinking I might just get the user to confirm at the beginning of the method if they want to proceed with adding a patient to the system. – S92 Dec 09 '20 at 20:10
  • I'm working on a proof of concept to give you some more ideas. Will post soon. – h0r53 Dec 09 '20 at 20:11
  • Does this answer your question? [How to implement a console menu having submenus in C#](https://stackoverflow.com/questions/58760184/how-to-implement-a-console-menu-having-submenus-in-c-sharp) –  Dec 09 '20 at 21:08
  • It's not clear whether this is a console app, WPF, Win Forms, Xamarin, ASP.NET etc. You need to provide more context in your question. – mason Dec 29 '20 at 23:55

1 Answers1

2

I would suggest encapsulating all of your prompts and reading input as one method that takes parameters.

public static string PromptUserForInput(string promptMessage, bool checkForEscape = true){...}

Then use the readkey like @h0r53 said. I was going to build out a basic example, but it looks like one can already be found here. Then just tweak as needed.

Dan Csharpster
  • 2,662
  • 1
  • 26
  • 50
  • 1
    I was in the middle of writing something up myself, but the accepted answer on that post looks like a good way to do it. So @Sog92 try that code and just remember to `return` if `readLineWithCancel` returns null. – h0r53 Dec 09 '20 at 20:23
  • thanks guy, much appreciated. I'll get something written up and let you know how it goes (y) – S92 Dec 09 '20 at 20:30
  • okay guys ive been playing around with it. I have the Esc button working, the arrow keys work, however the left arrow doesn't disable when it it reaches the beginning of the string. does anyone have any idea as to why this would be occurring? – S92 Dec 10 '20 at 16:19
  • 1
    I hate to be pedantic, but this seems like a separate question. – Dan Csharpster Dec 10 '20 at 16:24
  • apologies, im new to this...ill create a new post. – S92 Dec 10 '20 at 16:25
  • The left arrow wraps around to the previous line? I would imagine you can check for something like cursor position and ignore the key if 0. It is a new question though. – h0r53 Dec 11 '20 at 14:40