Friday, 13 February 2015

Making a menu for the console in C# [Part 2]

Issues using the menu

If you have used the source code from part one of "Making a menu for the console" you may have encountered some issues when you have too many items to display, this could be fixed by only displaying a small section of the array at one time, but the program I made this for would never encounter an array longer than five items long so I decided to work on the more complex parts of it instead.

Just be cautious if you plan on using the source I provided in part 1.

 Uses of the menu

I have a few uses of this method, and they have all been in my programming semester one coursework. I would recommend making a class library that includes all methods related to user input validation so that you are able to include some of these useful methods within other programs easily.


I tend to ask the user "yes" or "no" questions a lot which I originally asked them to type "y" or "n" into the program which works but this could confuse some users. So I made a quick method that allows me to give the choice in a menu format. The method returns true if the user selected yes and false is the user selects no.
public static bool YesNo() {
    if (Menu(new string[2] { "Yes""No" }) == 0) {
        return true;
    } else {
        return false;
An example of when I have used this system in my programs is for when I want to ask the user if they want to quit, which would be wrote like this.
bool quit = false;
while (!quit) {
    //Do the program stuff here
    Console.WriteLine("Do you want to quit?");
    quit = YesNo();
As you can see this is very easy to apply to other situations.

Very simple file browser

For my programming coursework we had to make a game, for some reason I thought it would be a good idea to make this game in the console. If you want to see how that went go [Here]. For extra credits I decided to implement a way to save and load the game which involved making a file browser in the console but didn't require anything too drastic.
Here is what I came up with. (you can even see where I added some more menus within the game)

[View on youtube]

To the joy of past me this was insanely easy to implement, in fact it can be done in only three lines, all I had to do was grab all the file names out of the save file directory then feed them into the menu method finally I loaded the file that the user selected.
//Get the files in the save directory 
if (Directory.Exists(@"saves\")) {
    filePaths = Directory.GetFiles(@"saves\""*.tcr");
UserInput.Header("Pick a save file");
//this makes the menu that prints out the available files, and it assigns the selected file to fileSelected
fileSelected = filePaths[UserInput.Menu(filePaths)];
The Directory.GetFiles() method just so happens to return as a string array which is just what we need. Obviously you need to add some tests to handle situations such as having no files within the save directory, this is simply a cut down version of the code.
All you would have to do now would be opening a stream reader with fileSelected and do all your relevant file loading.


I'm sure I am not the first person to do something like this, and I wont be the last, but this menu method managed to save a ton of time with input validations and I would recommend making your own version if you are starting out with programming like me.

I will probably make some more Blog posts discussing parts of how I stumble through my programming, and they definitely will be more complex than a console menu method.


  1. Really interesting content which is unique which provided me the required information.
    Dot Net Training in Chennai | .Net training in Chennai | FITA Training | FITA Velachery .

  2. I really enjoyed while reading your article, the information you have delivered in this post was damn good. Keep sharing your post with efficient news.
    FITA Chennai complaints|Python Training in Chennai|SAS Training in Chennai

  3. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    salesforce training in chennai | salesforce training institute in chennai


Copyright © 2014 Stumbling Through Programming