Click here to Skip to main content
13,247,674 members (19,423 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 25 Jan 2003

Text-based menu class for Console Applications

, 25 Jan 2003
Rate this:
Please Sign up or sign in to vote.
This class helps creating and using menus in console applications by using delegates. It will show you the basics of array lists and delegates.


Firstly, this is my very first article and it will probably contain mistakes and the language will not be perfect, but I will keep it updated and I will edit it until it gets good, so, I need your feedback.

Almost every console-based program written in C# will make use of Console.ReadLine() to read in data. Mostly, the programmer will use a if-else statement to handle the data input, specially when inputting choices from menus. This interesting class will teach you the basics of collections and delegates while providing a good framework for a more powerful menu class.

The Menu class and its interface

The menu class is very simple to use; just instantiate it and use the Add method to add new options to menu, and finally, use the Show method to print it and let it handle the user's choice.

Here is how it works: each menu option is a MenuItem class. It is nothing but an object containing the option's descriptions and a delegate, an object that points to a function, that will be called when and if the option is choosen. This delegate will point to a function that will handle the choice.

The MenuItems are stored in an ArrayList, a very useful collection class. A collection class is self-explainatory: it holds a collection of objects, often providing search, remove and object manipulation functionalities. The ArrayList is pretty simple; the name Array comes from that fact it can be transformed into an Array of any type, and I will use it because the Menu class does not need the power of a Hashtable collection or a Dictionary collection, more enhanced collection classes. ArrayList is just fine for us.

Creating and showing a menu

Firstly, write the functions that will handle the user's choice and the delegates. You will see how simple is that. Our Menu class uses the MenuCallback delegate, which returns nothing and has no parameters. Whenever you use a delegate, you need to

  • Create a new delegate type and
  • Use the new keyword to store a new delegate.
private static MenuCallback mcOption1 = new MenuCallback(Option1);
private static MenuCallback mcOption2 = new MenuCallback(Option2);

private static void Option1()
  Console.WriteLine("Option 1 choosen.");

private static void Option2()
  Console.WriteLine("Option 2 choosen.");

Secondly, create a new Menu instance and add the delegates and options to it. Then, show it. This is simple, too.

Menu m = new Menu();
m.Add("Option 1", mcOption1);
m.Add("Option 2", mcOption2);

Now, it is over. The Menu class will handle the user's input and call the desired function using the delegate you've created. The sample will show everything better.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Brazil Brazil
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralDemo Pin
spencepk31-Mar-11 12:25
memberspencepk31-Mar-11 12:25 
Questionwhats the point? Pin
llitsanylg14-Aug-07 7:42
memberllitsanylg14-Aug-07 7:42 
AnswerRe: whats the point? Pin
spencepk31-Mar-11 12:02
memberspencepk31-Mar-11 12:02 
QuestionAnd where's the demo project??? Pin
Chopper27-Jan-03 0:13
memberChopper27-Jan-03 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 26 Jan 2003
Article Copyright 2003 by Kiff
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid